-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Bug report
Required System information
- 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.
Describe the bug
Jest tests fail to run because Strapi won't load .ts config files.
Steps to reproduce the behavior
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
testPathIgnorePatternsto the Jest config as shown in the Strapi jest setup guide. -
I added a
strapi.tshelper file as explained in the Strapi jest setup guide. I converted it to TypeScript and used the newcreateStrapifunction as well. -
I added a
config/env/test/database.tsand aconfig/env/production/database.tsfile. -
I added the following lines to my
.envfile:
DATABASE_FILENAME=.tmp/data.db
TEST_DATABASE_FILENAME=.tmp/test_data.db
-
I added
tests/app.test.tsas 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...
❯ yarn test
yarn run v1.22.22
$ jest --forceExit --detectOpenHandles
console.warn
Config file not loaded, extension must be one of .js,.json): admin.ts
6 | export const setupStrapi = async () => {
7 | if (!instance) {
> 8 | await createStrapi().load();
| ^
9 | instance = strapi;
10 |
11 | instance.server.mount();
at logWarning (node_modules/@strapi/core/src/configuration/config-loader.ts:57:11)
at node_modules/@strapi/core/src/configuration/config-loader.ts:76:7
at Array.reduce (<anonymous>)
at loadConfigDir (node_modules/@strapi/core/src/configuration/config-loader.ts:65:32)
at Module.loadConfigDir [as loadConfiguration] (node_modules/@strapi/core/src/configuration/index.ts:74:38)
at new loadConfiguration (node_modules/@strapi/core/src/Strapi.ts:47:28)
at createStrapi (node_modules/@strapi/core/src/index.ts:11:18)
at setupStrapi (tests/helpers/strapi.ts:8:23)
at Object.<anonymous> (tests/app.test.ts:4:20)
console.warn
Config file not loaded, extension must be one of .js,.json): api.ts
6 | export const setupStrapi = async () => {
7 | if (!instance) {
> 8 | await createStrapi().load();
| ^
9 | instance = strapi;
10 |
11 | instance.server.mount();
at logWarning (node_modules/@strapi/core/src/configuration/config-loader.ts:57:11)
at node_modules/@strapi/core/src/configuration/config-loader.ts:76:7
at Array.reduce (<anonymous>)
at loadConfigDir (node_modules/@strapi/core/src/configuration/config-loader.ts:65:32)
at Module.loadConfigDir [as loadConfiguration] (node_modules/@strapi/core/src/configuration/index.ts:74:38)
at new loadConfiguration (node_modules/@strapi/core/src/Strapi.ts:47:28)
at createStrapi (node_modules/@strapi/core/src/index.ts:11:18)
at setupStrapi (tests/helpers/strapi.ts:8:23)
at Object.<anonymous> (tests/app.test.ts:4:20)
console.warn
Config file not loaded, extension must be one of .js,.json): database.ts
6 | export const setupStrapi = async () => {
7 | if (!instance) {
> 8 | await createStrapi().load();
| ^
9 | instance = strapi;
10 |
11 | instance.server.mount();
at logWarning (node_modules/@strapi/core/src/configuration/config-loader.ts:57:11)
at node_modules/@strapi/core/src/configuration/config-loader.ts:76:7
at Array.reduce (<anonymous>)
at loadConfigDir (node_modules/@strapi/core/src/configuration/config-loader.ts:65:32)
at Module.loadConfigDir [as loadConfiguration] (node_modules/@strapi/core/src/configuration/index.ts:74:38)
at new loadConfiguration (node_modules/@strapi/core/src/Strapi.ts:47:28)
at createStrapi (node_modules/@strapi/core/src/index.ts:11:18)
at setupStrapi (tests/helpers/strapi.ts:8:23)
at Object.<anonymous> (tests/app.test.ts:4:20)
console.warn
Config file not loaded, extension must be one of .js,.json): middlewares.ts
6 | export const setupStrapi = async () => {
7 | if (!instance) {
> 8 | await createStrapi().load();
| ^
9 | instance = strapi;
10 |
11 | instance.server.mount();
at logWarning (node_modules/@strapi/core/src/configuration/config-loader.ts:57:11)
at node_modules/@strapi/core/src/configuration/config-loader.ts:76:7
at Array.reduce (<anonymous>)
at loadConfigDir (node_modules/@strapi/core/src/configuration/config-loader.ts:65:32)
at Module.loadConfigDir [as loadConfiguration] (node_modules/@strapi/core/src/configuration/index.ts:74:38)
at new loadConfiguration (node_modules/@strapi/core/src/Strapi.ts:47:28)
at createStrapi (node_modules/@strapi/core/src/index.ts:11:18)
at setupStrapi (tests/helpers/strapi.ts:8:23)
at Object.<anonymous> (tests/app.test.ts:4:20)
console.warn
Config file not loaded, extension must be one of .js,.json): plugins.ts
6 | export const setupStrapi = async () => {
7 | if (!instance) {
> 8 | await createStrapi().load();
| ^
9 | instance = strapi;
10 |
11 | instance.server.mount();
at logWarning (node_modules/@strapi/core/src/configuration/config-loader.ts:57:11)
at node_modules/@strapi/core/src/configuration/config-loader.ts:76:7
at Array.reduce (<anonymous>)
at loadConfigDir (node_modules/@strapi/core/src/configuration/config-loader.ts:65:32)
at Module.loadConfigDir [as loadConfiguration] (node_modules/@strapi/core/src/configuration/index.ts:74:38)
at new loadConfiguration (node_modules/@strapi/core/src/Strapi.ts:47:28)
at createStrapi (node_modules/@strapi/core/src/index.ts:11:18)
at setupStrapi (tests/helpers/strapi.ts:8:23)
at Object.<anonymous> (tests/app.test.ts:4:20)
console.warn
Config file not loaded, extension must be one of .js,.json): server.ts
6 | export const setupStrapi = async () => {
7 | if (!instance) {
> 8 | await createStrapi().load();
| ^
9 | instance = strapi;
10 |
11 | instance.server.mount();
at logWarning (node_modules/@strapi/core/src/configuration/config-loader.ts:57:11)
at node_modules/@strapi/core/src/configuration/config-loader.ts:76:7
at Array.reduce (<anonymous>)
at loadConfigDir (node_modules/@strapi/core/src/configuration/config-loader.ts:65:32)
at Module.loadConfigDir [as loadConfiguration] (node_modules/@strapi/core/src/configuration/index.ts:74:38)
at new loadConfiguration (node_modules/@strapi/core/src/Strapi.ts:47:28)
at createStrapi (node_modules/@strapi/core/src/index.ts:11:18)
at setupStrapi (tests/helpers/strapi.ts:8:23)
at Object.<anonymous> (tests/app.test.ts:4:20)
console.warn
Config file not loaded, extension must be one of .js,.json): database.ts
6 | export const setupStrapi = async () => {
7 | if (!instance) {
> 8 | await createStrapi().load();
| ^
9 | instance = strapi;
10 |
11 | instance.server.mount();
at logWarning (node_modules/@strapi/core/src/configuration/config-loader.ts:57:11)
at node_modules/@strapi/core/src/configuration/config-loader.ts:76:7
at Array.reduce (<anonymous>)
at loadConfigDir (node_modules/@strapi/core/src/configuration/config-loader.ts:65:32)
at Module.loadConfigDir [as loadConfiguration] (node_modules/@strapi/core/src/configuration/index.ts:77:21)
at new loadConfiguration (node_modules/@strapi/core/src/Strapi.ts:47:28)
at createStrapi (node_modules/@strapi/core/src/index.ts:11:18)
at setupStrapi (tests/helpers/strapi.ts:8:23)
at Object.<anonymous> (tests/app.test.ts:4:20)
FAIL tests/app.test.ts
● strapi is defined
TypeError: Cannot destructure property 'client' of 'db.config.connection' as it is undefined.
6 | export const setupStrapi = async () => {
7 | if (!instance) {
> 8 | await createStrapi().load();
| ^
9 | instance = strapi;
10 |
11 | instance.server.mount();
at getDialect (node_modules/@strapi/database/src/dialects/index.ts:40:11)
at new Database (node_modules/@strapi/database/src/index.ts:80:20)
at node_modules/@strapi/core/src/Strapi.ts:275:11
at Strapi.get (node_modules/@strapi/core/src/container.ts:27:35)
at Strapi.get db [as db] (node_modules/@strapi/core/src/Strapi.ts:77:17)
at Object.register (node_modules/@strapi/core/src/providers/registries.ts:38:12)
at Strapi.register (node_modules/@strapi/core/src/Strapi.ts:400:13)
at Strapi.load (node_modules/@strapi/core/src/Strapi.ts:387:5)
at setupStrapi (tests/helpers/strapi.ts:8:5)
at Object.<anonymous> (tests/app.test.ts:4:3)
● Test suite failed to run
TypeError: Cannot destructure property 'client' of 'db.config.connection' as it is undefined.
23 |
24 | // close the connection to the database before deletion
> 25 | await strapi.db.connection.destroy();
| ^
26 |
27 | //delete test database after all tests have completed
28 | if (dbSettings?.connection?.filename) {
at getDialect (node_modules/@strapi/database/src/dialects/index.ts:40:11)
at new Database (node_modules/@strapi/database/src/index.ts:80:20)
at node_modules/@strapi/core/src/Strapi.ts:275:11
at Strapi.get (node_modules/@strapi/core/src/container.ts:27:35)
at Strapi.get db [as db] (node_modules/@strapi/core/src/Strapi.ts:77:17)
at cleanupStrapi (tests/helpers/strapi.ts:25:16)
at Object.<anonymous> (tests/app.test.ts:8:22)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 1.697 s, estimated 2 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
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.
Expected behavior
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.
Screenshots
N/A
Code snippets
N/A
Additional context
I'm really happy Strapi is getting better TypeScript support! I'd love if it could get better testing support as well. Thanks so much for the great software!!