Closed Bjoren closed 6 months ago
can you also post your jest.config.ts
Sure!
import type { Config } from '@jest/types'
export default async (): Promise<Config.InitialOptions> => {
return {
extensionsToTreatAsEsm: [".ts"],
globals: {
"ts-jest": {
useESM: true
}
},
verbose: true,
modulePaths: ['<rootDir>/src', '<rootDir>/node_modules'],
globalSetup: './src/pre-test.ts'
}
}
The error message is indicating that your jest.config.ts
file is being treated as an ES module because it's a .ts
file and the nearest parent package.json
contains "type": "module"
. However, Jest is trying to use require()
to load it, which is not supported for ES modules.
To fix this issue, you have two options:
Change the jest.config.ts
to a CommonJS module by renaming it to jest.config.js
and using module.exports
instead of export default
.
Remove "type": "module"
from your package.json
file. This will make Node.js treat .ts
files as CommonJS modules by default.
Here's how you can do the first option:
// jest.config.js
module.exports = {
// your jest configuration
};
And here's how you can do the second option:
// package.json
{
"name": "your-package-name",
"version": "1.0.0",
// remove "type": "module"
// other package.json properties
}
Please choose the option that best fits your project's needs.
i closed this issue because i think its related to your config, not to the extension. if you need more help just re-open or reply.
Hi!
When using Jest Runner to execute my Jest tests in an ES2022 TypeScript environment with ECMA-style modules, Jest failes to parse the configuration file. I'm guessing it's because Jest neglects to transform CommonJS into ECMA for some contextual reason.
Jest Runner executes:
node "node_modules/jest/bin/jest.js" "c:<absolute path>/<some test file>.spec.ts" -c "c:/<absolute path>/jest.config.ts" -t "<some test name>"
which produces:
However, if I modify the command to either use the relative path to the config file, or leave it out altogether and let Jest resolve the config location itself, it runs without error.
These work:
node "node_modules/jest/bin/jest.js" "c:<absolute path>/<some test file>.spec.ts" -c "jest.config.ts" -t "<some test name>"
node "node_modules/jest/bin/jest.js" "c:<absolute path>/<some test file>.spec.ts" -t "<some test name>"
Can I set up Jest Runner to behave in this way, somehow? Is there an alternative solution I am not aware of, or am I doing something wrong in my repo?