Open corasaurus-hex opened 1 month ago
I'm not sure this is a documentation issue, @derrickmehaffy. Strapi is specifically dynamically loading .js
and .json
configuration files at runtime to configure the server: https://github.com/strapi/strapi/blob/v5.0.0-rc.10/packages/core/core/src/configuration/config-loader.ts#L5 -- I believe it would need to be a core framework change to also allow loading .ts
files.
Unless, I guess, the docs say to use something else, like vitest or something like it that can incrementally compile and run tests.
This issue has been mentioned on Strapi Community Forum. There might be relevant details there:
https://forum.strapi.io/t/how-can-i-add-vitest-to-a-strapi-5-app/41255/1
running
I can make it it run by add jest.config.ts and ts-jest, but I still get this error message: TypeError: Cannot destructure property 'client' of 'db.config.connection' as it is undefined
const {defaults: tsjPreset} = require('ts-jest/presets');
const {pathsToModuleNameMapper} = require('ts-jest/dist');
const {compilerOptions} = require('./tsconfig.test.json');
export default async () => {
return {
roots: ["<rootDir>/src/", "<rootDir>/config/", "<rootDir>/tests/"],
testMatch: [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
testEnvironment: 'node',
transform: {
...tsjPreset.transform
},
testPathIgnorePatterns: [
"<rootDir>/__mocks__/*",
"node_modules",
"\\.tmp",
"\\.cache",
"<rootDir>.*/public",
"<rootDir>/dist/*",
],
moduleNameMapper: {
...pathsToModuleNameMapper(compilerOptions.paths, {prefix: '<rootDir>/'})
},
setupFilesAfterEnv: [
"<rootDir>/tests/helpers/strapi.ts" // Path to your setup file
],
testTimeout: 60000,
verbose: false,
};
};
Bug report
Required System information
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 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 newcreateStrapi
function as well.I added a
config/env/test/database.ts
and aconfig/env/production/database.ts
file.I added the following lines to my
.env
file: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...
``` ❯ 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 (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" }
tocreateStrapi
instrapi.ts
, and runyarn build
before I runyarn test
, the tests pass. Presumably this is because it's running the tests from thedist
dir andyarn 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!!