Description
Link to the documentation page or resource
https://docs.strapi.io/cms/testing
Describe the bug
Jest tests fail to run because Strapi won't load .ts config files.
Additional context
I published an example app that shows the issue to save you time, but I took the following steps to create the app:
I set up a new Strapi project using:
npx create-strapi-app@rc --skip-cloud --typescript --use-yarn --dbclient sqlite strapi-typescript-test-bug-repro
I then added Jest and its support libraries:
yarn add --dev jest ts-jest @types/jest
I initialized the Jest config using:
yarn ts-jest config:init
I then added the testPathIgnorePatterns to the Jest config as shown in the Strapi jest setup guide.
I added a strapi.ts helper file as explained in the Strapi jest setup guide. I converted it to TypeScript and used the new createStrapi function as well.
I added a config/env/test/database.ts and a config/env/production/database.ts file.
I added the following lines to my .env file:
DATABASE_FILENAME=.tmp/data.db
TEST_DATABASE_FILENAME=.tmp/test_data.db
I added tests/app.test.ts as described in the Strapi jest setup guide. This file has been converted to TypeScript as well.
Run tests with yarn test.
Expand this to see the test output...
As you can see, there are a lot of warnings like Config file not loaded, extension must be one of .js,.json): database.ts and then the tests fails because something internal to Strapi can't destructure config values.
Interestingly, if I pass the value { distDir: "dist" } to createStrapi in strapi.ts, and run yarn build before I run yarn test, the tests pass. Presumably this is because it's running the tests from the dist dir and yarn build is transpiling those files to .js files.
Suggested improvements or fixes
I would expect there to be some way to run tests under TypeScript with Strapi that didn't require a build first. This is also broken under Strapi 4 in the same way and I was trying out Strapi 5 to see if I could make it work.
Node.js version: v20.16.0
NPM version: 10.8.1 (yarn 1.22.22)
Strapi version: 5.0.0-rc.8
Database: sqlite
Operating system: macOS Sonoma 14.5
Is your project Javascript or Typescript: TypeScript
❯ yarn strapi report
yarn run v1.22.22
$ strapi report
Launched In: 141 ms
Environment: development
OS: darwin-arm64
Strapi Version: 5.0.0-rc.8
Node/Yarn Version: yarn/1.22.22 npm/? node/v20.16.0 darwin arm64
Edition: Community
Database: sqlite
[2024-08-04 18:40:08.311] info: Shutting down Strapi
[2024-08-04 18:40:08.312] info: Strapi has been shut down
✨ Done in 1.69s.
Related issue(s)/PR(s)
No response