Skip to content

Commit 2f5cc0c

Browse files
committed
fix(compiler): support Node16/NodeNext value for target
Fixes #4198
1 parent 7201375 commit 2f5cc0c

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

e2e/native-esm-ts/jest.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { pathsToModuleNameMapper } from '../../dist/index.js'
1+
import { pathsToModuleNameMapper, createDefaultEsmPreset } from '../../dist/index.js'
22
import { createRequire } from 'module'
33

44
const require = createRequire(import.meta.url)
55
const tsConfig = require('./tsconfig.json')
6+
const esmPreset = createDefaultEsmPreset()
67

78
/** @type {import('../../dist').JestConfigWithTsJest} */
89
export default {
9-
extensionsToTreatAsEsm: ['.ts'],
10+
...esmPreset,
1011
resolver: '<rootDir>/mjs-resolver.ts',
1112
moduleNameMapper: pathsToModuleNameMapper(tsConfig.compilerOptions.paths, {
1213
prefix: '<rootDir>',
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "./tsconfig.spec.json",
33
"compilerOptions": {
4+
"module": "ESNext",
45
"esModuleInterop": true
56
}
67
}

src/legacy/compiler/__snapshots__/ts-compiler.spec.ts.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes
2020
{
2121
"allowSyntheticDefaultImports": true,
2222
"esModuleInterop": true,
23-
"module": 99,
23+
"module": 1,
2424
}
2525
`;
2626

2727
exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": true, "supportsStaticESM": false, "useESM": true} 1`] = `
2828
{
29-
"allowSyntheticDefaultImports": true,
29+
"allowSyntheticDefaultImports": undefined,
3030
"esModuleInterop": true,
31-
"module": 99,
31+
"module": 1,
3232
}
3333
`;
3434

@@ -52,14 +52,14 @@ exports[`TsCompiler getCompiledOutput isolatedModules true should transpile code
5252
{
5353
"allowSyntheticDefaultImports": true,
5454
"esModuleInterop": true,
55-
"module": 99,
55+
"module": 1,
5656
}
5757
`;
5858

5959
exports[`TsCompiler getCompiledOutput isolatedModules true should transpile code with config {"babelConfig": true, "supportsStaticESM": false, "useESM": true} 1`] = `
6060
{
61-
"allowSyntheticDefaultImports": true,
61+
"allowSyntheticDefaultImports": undefined,
6262
"esModuleInterop": true,
63-
"module": 99,
63+
"module": 1,
6464
}
6565
`;

src/legacy/compiler/ts-compiler.ts

+32-25
Original file line numberDiff line numberDiff line change
@@ -146,34 +146,41 @@ export class TsCompiler implements TsCompilerInstance {
146146
return importedModulePaths
147147
}
148148

149-
getCompiledOutput(fileContent: string, fileName: string, options: TsJestCompileOptions): CompiledOutput {
150-
let moduleKind = this._initialCompilerOptions.module
151-
let esModuleInterop = this._initialCompilerOptions.esModuleInterop
152-
let allowSyntheticDefaultImports = this._initialCompilerOptions.allowSyntheticDefaultImports
153-
const currentModuleKind = this._compilerOptions.module
154-
const isEsmMode = this.configSet.useESM && options.supportsStaticESM
155-
if (
156-
(this.configSet.babelJestTransformer || (!this.configSet.babelJestTransformer && options.supportsStaticESM)) &&
157-
this.configSet.useESM
158-
) {
159-
moduleKind =
160-
!moduleKind ||
161-
(moduleKind &&
162-
![this._ts.ModuleKind.ES2015, this._ts.ModuleKind.ES2020, this._ts.ModuleKind.ESNext].includes(moduleKind))
163-
? this._ts.ModuleKind.ESNext
164-
: moduleKind
165-
// Make sure `esModuleInterop` and `allowSyntheticDefaultImports` true to support import CJS into ESM
166-
esModuleInterop = true
167-
allowSyntheticDefaultImports = true
168-
} else {
169-
moduleKind = this._ts.ModuleKind.CommonJS
149+
private fixupCompilerOptionsForModuleKind(compilerOptions: CompilerOptions, isEsm: boolean): CompilerOptions {
150+
if (!isEsm) {
151+
const moduleKind = compilerOptions.module ?? this._ts.ModuleKind.CommonJS
152+
const moduleKindValue = [
153+
this._ts.ModuleKind.CommonJS,
154+
this._ts.ModuleKind.Node16,
155+
this._ts.ModuleKind.NodeNext,
156+
].includes(moduleKind)
157+
? moduleKind
158+
: this._ts.ModuleKind.CommonJS
159+
160+
return {
161+
...compilerOptions,
162+
module: moduleKindValue,
163+
}
170164
}
171-
this._compilerOptions = {
172-
...this._compilerOptions,
173-
allowSyntheticDefaultImports,
174-
esModuleInterop,
165+
166+
const moduleKind = compilerOptions.module ?? this._ts.ModuleKind.ESNext
167+
const esModuleInterop = [this._ts.ModuleKind.Node16, this._ts.ModuleKind.NodeNext].includes(moduleKind)
168+
? true
169+
: compilerOptions.esModuleInterop
170+
171+
return {
172+
...compilerOptions,
175173
module: moduleKind,
174+
esModuleInterop: esModuleInterop ?? false,
175+
allowSyntheticDefaultImports: esModuleInterop ?? compilerOptions.allowSyntheticDefaultImports,
176176
}
177+
}
178+
179+
getCompiledOutput(fileContent: string, fileName: string, options: TsJestCompileOptions): CompiledOutput {
180+
const moduleKind = this._initialCompilerOptions.module
181+
const currentModuleKind = this._compilerOptions.module
182+
const isEsmMode = this.configSet.useESM && options.supportsStaticESM
183+
this._compilerOptions = this.fixupCompilerOptionsForModuleKind(this._initialCompilerOptions, isEsmMode)
177184
if (this._languageService) {
178185
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling using language service')
179186

0 commit comments

Comments
 (0)