How to generate test data in Strapi?

Hello everyone,

I am developing an app with 3 main services using Docker to build everything for tests, staging and production :

  • a Strapi 4.6 backend
  • 2 NextJS apps

I’m starting to implement E2E tests with Playwright for my NextJS apps, but I find it hard to generate and manage my test data in Strapi. More specifically, I manage to generate initial data using the new Strapi import / export feature, but two things bother me :

  • it’s very painful to update when the DB schema is changed (due to the strict policy used in export feature)
  • it doesn’t allow me to generate test-specific data during the execution of a test, for example using the beforeAll feature of Playwright.

One temporary solution I found was to use the strapi-plugin-import-export-entries plugin, generate my test data in a clean Strapi, then export JSONs and import them using ‘E2E’. Example :

test.beforeAll(async () => {
  const browser = await chromium.launch()
  const page = await browser.newPage()

  await page.goto('http://localhost:1337/admin/content-manager/collectionType/api::ppe.ppe?page=1&pageSize=10')
  await page.getByRole('button', { name: 'Import' }).click()
  await page.locator('label').filter({ hasText: 'Drag & drop your file into this area or browse for a file to upload' }).locator('path').click()
  await page.locator('input[type="file"]').setInputFiles('tests/fixtures/generators/export_ppes.json')
  await page.getByRole('dialog', { name: 'Import' }).getByRole('button', { name: 'Import' }).click()
  await page.waitForTimeout(1000)

  await page.close()
  await browser.close()
})

I am obviously missing something, as this cannot be the best solution.

Is anyone here facing the same situation ? What would be the best solution to generate test-specific data right before the test is executed ?

I’m coming from Rails, where we have strong fixture support using FactoryBot and Faker for example, allowing us to generate test data very easily. Is there some equivalent in Strapi ?

Thanks a lot, have a good day !

Hugs

Hello
I wanna follow this thread
I hope someone will help you with this issue

1 Like

What about using Strapi Entity Service API or Query Engine API in order to manage the data, instead of import / export ?
You can read about it here , Query Engine API | Strapi Documentation
Entity Service API | Strapi Documentation