-
Notifications
You must be signed in to change notification settings - Fork 455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't use top-level await #2057
Comments
Just to confirm, are you using node 14.3.0+ ? |
A source I found
This means it only works within ESM, so ya it has to wait for jest full support ESM first #1709 |
Yes, I was using Node v14.10.1. BTW, the bug template could ask for that, so I've sent a PR. |
I put on hold for now while waiting for jestjs/jest#9860 (your issue 😃) because there is a need for guidance from |
I'm experiencing the same issue. The error message requires that "the 'module' option is set to 'esnext' or 'system'", but as per @ahnpnl your comment here, "jest expects module in your tsconfig.spec.ts to set to commonjs". Don't these requirements conflict which each other? How can I get this working? |
When turning on ESM mode, Whether or not Jest can run it depends on Node version. In general, |
Thank you for clarification. I'm already using the latest node version. Looks as if my problem comes from another source ... But I have no clue from what. 😅 |
I'm now able to use top-level |
I'm also getting this issue. Based on the discussion in this thread, this is what my module.exports = {
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
moduleFileExtensions: [ ...defaults.moduleFileExtensions, 'ts' ],
globals: {
'ts-jest': {
tsconfig: {
module: 'ESNext',
target: 'ES2017'
},
useESM: true
}
}
} envinfo
|
@mayacoda: I used the ESM preset too. The manual configuration led to some error. module.exports = {
// https://jestjs.io/docs/ecmascript-modules
transform: {},
// https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/#use-esm-presets; 'manual configuration' didn't work
preset: 'ts-jest/presets/default-esm',
globals: {
'ts-jest': {
useESM: true,
},
},
testEnvironment: 'node',
testRegex: '.*.test.ts',
}; |
Unfortunately, @dandv's solution didn't work for me either. I found a workaround in the meantime, but would still be very interested in getting top-level await working for tests. |
@mayacoda did you manage to test top level await? |
Isn't top level await a |
I was not able to get it to work with the above described settings, unfortunately. As for support in TypeScript 4.5, the link @fernandopasik provided also says
Which makes me think it should already work with the configuration above and something else is preventing it. Hopefully, I'm wrong and it does get fixed with the new version of TypeScript. |
Thanks for clarifying. I tried as well and couldn't make it work. I got a empty and unknown error in jest |
@mayacoda @brendonco I struggled with this for a bit and finally got it working — see my fork of @dandv's repo here: https://github.com/HerbCaudill/ts-jest-tla The key things to make sure of are:
Hope this helps others who run into this. |
This does help, but using ESM creates a corollary issue that imports don't work as expected anymore (as described here: nodejs/node#32137), which require you to change a bunch of import statements. |
This does works. Another thing to note: if you had them transformed into CommonJS, you should clean all jest cache. |
@Creative-Difficulty not sure why you mentioned me; I'm not a contributor to this repo. |
I migrated to bun just today, sry for the ping |
This is very much still a problem. |
@dandv Please reopen |
After a lot of spinning wheels tonight, I found a solution that may work for more recent versions. In order to allow a top-level await, you'll need to do something slightly different in your jest config. In jest.config.cjs I used: /** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
extensionsToTreatAsEsm: ['.ts'],
transform: {
// '^.+\\.[tj]sx?$' to process js/ts with `ts-jest`
// '^.+\\.m?[tj]sx?$' to process js/ts/mjs/mts with `ts-jest`
'^.+\\.[tj]sx?$': [
'ts-jest',
{
useESM: true,
},
],
},
}; I'm use ts-jest and @types/jest in addition. This combined with my tsconfig allowed a top level await to work. tsconfig.json: {
"compilerOptions": {
"target": "es2022",
"moduleResolution": "node16",
"module":"node16",
"outDir": ".",
"strict": true,
"esModuleInterop": true,
},
"include": ["_worker.ts"],
"exclude": ["node_modules"]
} Edit: forgot to add I changed my test command in package.json to the following:
|
Actually the default ESM preset works for me with ts-jest 29.1.2, but only after a bunch of fiddling around that might have cleared the Jest cache, AND with the jest.config.ts: import type { JestConfigWithTsJest } from 'ts-jest';
const jestConfig: JestConfigWithTsJest = {
preset: 'ts-jest/presets/default-esm',
};
export default jestConfig; tsconfig.json has |
This one was a life savior. |
🐛 Bug Report
Not sure if I'm doing something wrong, or if TLA isn't supposed to work due to #1709.
To Reproduce
cd
into the cloned repro repo,npm install
,npm test
.Expected behavior
The test should pass.
Link to repo (highly encouraged)
https://github.com/dandv/ts-jest-tla
envinfo
The text was updated successfully, but these errors were encountered: