-
Notifications
You must be signed in to change notification settings - Fork 460
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
feat: unforce module CommonJS
when testing with ESM
#2199
Conversation
CommonJS
when resolving tsconfig
Pull Request Test Coverage Report for Build 419102349
💛 - Coveralls |
CommonJS
when testing with ESM
When `extensionsToTreatAsEsm` option is not an empty array, `CommonJS` enforcement will be no longer applicable BREAKING CHANGE To run tests with ESM support, one needs to make sure that: - `module` in tsconfig should be either `es2015` or `es2020` or `esnext` - Use `ts-jest` ESM presets or define value for `extensionsToTreatAsEsm` option properly
this._overriddenCompilerOptions.module = this.jestConfig.extensionsToTreatAsEsm.length | ||
? undefined | ||
: this.compilerModule.ModuleKind.CommonJS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think ts-jest
should care about this new option. Transformers will be called with supportsStaticESM: true
if import
statements can be used and supportsDynamicImport: true
if import
expressions can be used.
Expressions can be used in both ESM and CJS, but statements only work in ESM mode. I don't think you need to look at the stuff Jest uses to figure out what mode to run, you can just look at those flags
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aw thanks for the info. Actually I was looking for a single flag to decide to unforce. With TypeScript compiler, it's not possible to transform codes with only supportsStaticESM: true
or supportsDynamicImport: true
.
What do you suggest because it is possible that supportsDynamicImport: false
and supportsStaticESM: true
or vice versa. When will all ESM options be true ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm seem like I need to configure preset manually to enable all ESM options. I thought extensionsToTreatAsEsm
can automatically enable all ESM options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no single option to force either mode, as most likely CJS will always be a part of the test run (from node_modules
). So it's always per file transformed. That said, it will be all .ts
files for instance. But yeah, you should probably have separate compilers for CJS vs non-CJS in memory or something so you can switch between them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so you suggest that CJS should be compiled separately from non-CJS. Any extensions in extensionsToTreatAsEsm
should be compiled with non-CJS path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, sorta. I don't think you should look at extensionsToTreatAsEsm
at all - you should look at the options that is passed to process
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will ESM options be decided only via jest config or they can be also changed by runtime ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
runtime, as we lookup package.json
to check for type
field on .js
files https://nodejs.org/api/esm.html#esm_enabling
Summary
When
extensionsToTreatAsEsm
option is not an empty array,CommonJS
enforcement will be no longer applicable.Test plan
Adjusted unit tests and e2e tests, green CI
Does this PR introduce a breaking change?
To run tests with ESM support, one needs to make sure that:
module
in tsconfig should be eitheres2015
ores2020
oresnext
ts-jest
ESM presets or define value forextensionsToTreatAsEsm
option properlyOther information
N.A