How to add fixtures for Strapi unit tests

Viewed 159

How can I add fixtures for testing Strapi project with jest.

and is there a way to add such testing data in Strapi, like fixtures in ruby on rails?

this is an example of rubyOnRail fixtures:

# lo & behold!  I am a YAML comment!
david:
 name: David Heinemeier Hansson
 birthday: 1979-10-15
 profession: Systems development
 
steve:
 name: Steve Ross Kellock
 birthday: 1974-09-27
 profession: guy with keyboard
1 Answers

strapi-plugin-import-export-content has been updated for Strapi4 but requires some work to inject exported data to its apis (Authentication mainly). Also, single types have no export/import buttons by default so I don't know if the import/export apis work with them.

For now, the easiest workaround I found is to set a dedicated sqlite db for tests with seed data.

!!! Beware of the credentials of your super admin account if you do that, DO NOT PUSH TRUE CREDENTIALS ONLINE, they could remain in old commits for ever !!!

Here are some scripts I use to export/import fixtures:

#!/usr/bin/env bash
# tests/export_sqlite_fixtures.sh
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

sqlite3 "$SCRIPT_DIR/../.tmp/data.db" ".output $SCRIPT_DIR/fixtures.sql.dump" ".dump"
#!/usr/bin/env bash
# tests/import_sqlite_fixtures.sh
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

sqlite3 "$SCRIPT_DIR/../.tmp/data.db" ".output $SCRIPT_DIR/fixtures.sql.dump" ".dump"

Then in my CI pipe i just add (Github action here):

    - name: Populate sqlite db with fixtures
      run: ./tests/import_sqlite_fixtures.sh

    - name: build Strapi
      run: yarn build

    ...

NB: The extension of the export is .sql.dump to keep the *.sql rule in the .gitignore

Related