-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Jest fails to load jest.config.ts in a ESM project using ts-node 10 #11453
Comments
Hi, I'm the In
I see that when jest installs This could be exposed as an API flag, used like this:
If it were possible to programmatically install Do you think it's important to support when someone is using |
Another option is a config that says "treat files matching these globs as .cjs" Something like This might be more intuitive, since it maps cleanly to a |
Workaround for anyone running into this with ESM + TypeScript + Jest v27 (until this issue is addressed):
|
If anyone from the jest team has a minute to offer some feedback on the solution proposed in #11453 (comment) and #11453 (comment), I would greatly appreciate it. See also: TypeStrong/ts-node#1342 tracking this feature on the ts-node side. |
I wonder if we should just spawn A flag allowing us to truly force CJS is all well and good until the user imports an ESM native package. I assume we cannot use |
Yeah, that works. You are correct that I think it depends on philosophy: in the general sense, I want On the other hand, if jest is already overriding When jest loads the config, is that happening in the very first CLI process? Or has jest already spawned a subprocess at that point? This might technically be possible in a worker_thread, too? I think worker_threads let you add |
This is essentially the first thing that happens yeah. I'm happy to keep using the programmatic API, I'm just afraid as mentioned that it's just pushing the issue in front of us (like ESM source JS, which will break if loaded via some transpiled |
Yeah, it won't impact startup much if users are smart and enable To get a sense for timing:
|
We can probably pass |
For TS_NODE_TRANSPILE_ONLY=true node --loader ts-node/esm ...
# or
node --loader ts-node/esm-transpile-only ... |
Semi-related: microsoft/TypeScript#44501. How that shakes out might affect how ts-node does things and also how we should deal with it in Jest? |
TypeStrong/ts-node#1371 should give users a way to force CJS when they need it, solving this use-case. I just finished the implementation minutes ago and haven't reviewed it, but it has basic tests that are passing. If anyone is feeling adventurous, they can install from git:
Unfortunately files like |
Applying this workaround, plus:
Was a go for me. Still missing my |
@vahdet You can also downgrade to ts-node 9, wait for TypeStrong/ts-node#1371 to be released, and then upgrade to ts-node 10. This should save on effort and allow you to use .ts configs the entire time. |
ts-node v10.1.0 adds a new |
@cspotcode So if I understand the release notes and docs correctly, the following configuration would allow loading an ESM
{
"ts-node": {
"moduleTypes": {
"jest.config.ts": "cjs"
}
},
"compilerOptions": {
"module": "es2020",
"target": "es2020"
}
}
import type { Config } from '@jest/types';
const config: Config.InitialOptions = {
moduleFileExtensions: ['ts', 'tsx', 'js'],
moduleNameMapper: {
'^(.*)\\.js$': '$1',
},
testEnvironment: 'jest-environment-node',
transformIgnorePatterns: [
'node_modules/(?!aggregate-error|clean-stack|escape-string-regexp|indent-string|p-map)',
],
};
export default config; |
Or does the
import type { Config } from '@jest/types';
const config: Config.InitialOptions = {
moduleFileExtensions: ['ts', 'tsx', 'js'],
moduleNameMapper: {
'^(.*)\\.js$': '$1',
},
testEnvironment: 'jest-environment-node',
transformIgnorePatterns: [
'node_modules/(?!aggregate-error|clean-stack|escape-string-regexp|indent-string|p-map)',
],
};
module.exports = config; |
@cspotcode Would be nice to see the content of the I'd be happy to do a PR for this as soon as I know how this is working :) |
Ah from the Opened a PR: TypeStrong/ts-node#1394 |
See #12397 for a working example. So that's B from your use cases. C is free since people can just enable that as well in their config. And A I think can wait for more stabilization in typescript itself (at some point we can just spawn |
Yeah, makes sense to me. I wasn't sure quite how jest handles bootstrapping itself and if it was able to pass Re: delegation, I see that jest forces CommonJS emit. In this case, seems like a good thing: pair it with the correct Based on that integration test, are you planning to recommend that users enable that flag in their tsconfigs, or are you planning for jest to pass that option as an override similar to how it is forcing CJS emit? If the latter, probably best to make the override a catch-all |
I tried passing EDIT: Oh, from the types we shouldn't have the |
Yeah, no |
I was fooled by not getting a type error when passing |
Yeah, no These docs are also outdated because they say TS can't use cts nor mts, which is wrong. You didn't get a type error due to dynamic require()? Or is it a bug in ts-node's declarations? |
The |
See related issue jestjs/jest#11453
See related issue jestjs/jest#11453
I'm bumping this issue because it doesn't seem to have been solved. Since a lot of people are probably going to be using ES6 modules, and they are no longer considered experimental by the NodeJS team, it is important that this issue is resolved. |
ESM loaders are still considered experimental by the NodeJS team. This means if you want to write a jest config file that requires transpilation, and you're unwilling to use the configs that allow it to be executed as CommonJS, then you're relying on an experimental feature of node. Nothing that Jest can do about that. |
I see.
ES6 *modules* are stable.
ES6 module *loaders* are experimental
Source:
https://nodejs.org/dist/latest-v16.x/docs/api/esm.html#loaders
…On Wed, Mar 30, 2022 at 10:32 AM Andrew Bradley ***@***.***> wrote:
ESM loaders are still considered experimental by the NodeJS team. This
means if you want to write a jest config file that requires transpilation,
you're relying on an experimental features of node.
—
Reply to this email directly, view it on GitHub
<#11453 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACD2II426KHEATNWOWH74RLVCR62PANCNFSM45SB7IFQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
--
*Montana Burr*
*iOS Application Developer*
*Find Me on the App Store
<https://apps.apple.com/us/developer/montana-burr/id1483345630> *
|
It is solved, you can try the reproduction in the OP and it works correctly. If it's still not working for you, please create a new issue with a minimal reproduction |
But why not just rename it to
But why not just change it to |
@SimenB am I understanding correctly that "use case A" ( |
This works for me and even though I have "compilerOptions": {
"module": "es2020",
"target": "es2020"
} when I use // ESM workaround for the missing `__dirname`:
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); I get an error: Error: Jest: Failed to parse the TypeScript config file /Users/mprinc/data/development/colabo-zontik/colabo/src/services/puzzles/flow/go/jest.config.ts
TSError: ⨯ Unable to compile TypeScript:
jest.config.ts:16:34 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', or 'nodenext'.
16 const __filename = fileURLToPath(import.meta.url);
~~~~~~~~~~~
at readConfigFileAndSetRootDir (/Users/mprinc/.config/yarn/global/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:118:13)
at async readConfig (/Users/mprinc/.config/yarn/global/node_modules/jest-config/build/index.js:233:18)
at async readConfigs (/Users/mprinc/.config/yarn/global/node_modules/jest-config/build/index.js:420:26)
at async runCLI (/Users/mprinc/.config/yarn/global/node_modules/@jest/core/build/cli/index.js:132:59)
at async Object.run (/Users/mprinc/.config/yarn/global/node_modules/jest-cli/build/cli/index.js:155:37) @cspotcode, @SimenB could you please investigate and help? Thanks a lot! If it is not straightforward, please let me know if I need to create a fully reproducible repo? |
This comment was marked as spam.
This comment was marked as spam.
I'm still facing this issue. Please find a reproduction on this repository |
Hi, I feel like I am missing something between this thread, TypeStrong/ts-node#1342 (linked in one of the comments here) and the docs for 29.5 of import type {Config} from 'jest';
const config: Config = {
verbose: true,
};
export default config; I have the following error when running jest: To be honest, I have no clues if this is related or not. The thing is if we write TS files with ESM syntax, but I made a stackblitz to show what I am saying. When adding |
I found this thread experiencing the exact same issue, nothing I tried has worked :/ |
🐛 Bug Report
In a project using TypeScript, Jest and setup as ESM (the output of the transpiler is ESM so Node will run ESM instead of CJS) JEST is failing with ts-node 10 but works with ts-node 9.
Error:
To Reproduce
Steps to reproduce the behavior:
npm i -D ts-node typescript jest
"type": "module"
to your package.jsonnpx jest
Expected behavior
The config file should be loaded.
Link to repl or repo (highly encouraged)
I have reproduced the issue in a StackBlitz:
In order to run the code please run
npm test
in the console and the error above will be raised.Both projects are identical aside from the ts-node version.
Keep in mind that this project is using StackBlitz WebContainers and Turbo package manager (which is not NPM, though it have an alias) if you want to tweak my example but not go to learn about specifics about Turbo or WebContainers it may be better to just clone the repo.
envinfo
This is my local environment:
The text was updated successfully, but these errors were encountered: