Skip to content

Commit

Permalink
fix: don't show warning message with Node16/NodeNext
Browse files Browse the repository at this point in the history
fixes #4266
  • Loading branch information
ahnpnl committed Jun 17, 2024
1 parent 9062e07 commit 99c4f49
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
56 changes: 46 additions & 10 deletions src/legacy/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import * as ts from 'typescript'

import { createConfigSet } from '../../__helpers__/fakers'
import { logTargetMock } from '../../__helpers__/mocks'
import type { RawCompilerOptions } from '../../raw-compiler-options'
import type { AstTransformerDesc, TsJestGlobalOptions } from '../../types'
import { stringify } from '../../utils'
import * as _backports from '../../utils/backports'
import { getPackageVersion } from '../../utils/get-package-version'
import { Errors } from '../../utils/messages'
import { normalizeSlashes } from '../../utils/normalize-slashes'
import { sha1 } from '../../utils/sha1'

Expand Down Expand Up @@ -54,7 +56,7 @@ describe('parsedTsConfig', () => {
})

it('should include compiler config from base config', () => {
expect(get({ tsconfig: { target: 'esnext' } }).options.target).toBe(ts.ScriptTarget.ESNext)
expect(get({ tsconfig: { target: 'ESNext' } }).options.target).toBe(ts.ScriptTarget.ESNext)
})

it('should fallback to ES2015 as default target and CommonJS as default module when no target or module defined in tsconfig', () => {
Expand Down Expand Up @@ -98,29 +100,63 @@ describe('parsedTsConfig', () => {
allowSyntheticDefaultImports: true,
esModuleInterop: false,
})
expect(target.lines.warn.join()).toMatchInlineSnapshot(`
"[level:40] message TS151001: If you have issues related to imports, you should consider setting \`esModuleInterop\` to \`true\` in your TypeScript configuration file (usually \`tsconfig.json\`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.
"
`)
expect(target.lines.warn.join()).toEqual(expect.stringContaining(Errors.ConfigNoModuleInterop))
})

it('should not warn neither set synth. default imports if using babel', () => {
it.each([
{
moduleString: 'CommonJS',
expectedConfig: {
module: ts.ModuleKind.CommonJS,
esModuleInterop: false,
},
},
{
moduleString: 'Node16',
expectedConfig: {
module: ts.ModuleKind.Node16,
esModuleInterop: false,
},
},
{
moduleString: 'NodeNext',
expectedConfig: {
module: ts.ModuleKind.NodeNext,
esModuleInterop: false,
},
},
])('should not warn with module is $moduleString when not using babel', ({ moduleString, expectedConfig }) => {
const target = logTargetMock()
target.clear()
const cs = createConfigSet({
tsJestConfig: {
tsconfig: { module: 'amd', esModuleInterop: false },
tsconfig: { module: moduleString as RawCompilerOptions['module'], esModuleInterop: false },
diagnostics: { warnOnly: true, pretty: false },
babelConfig: { babelrc: false },
},
resolve: null,
})

expect(cs.parsedTsConfig.options).toMatchObject(expectedConfig)
expect(target.lines.warn.join()).toEqual(expect.not.stringContaining(Errors.ConfigNoModuleInterop))
})

it('should not warn neither set synth. default imports when using babel', () => {
const target = logTargetMock()
target.clear()
const cs = createConfigSet({
tsJestConfig: {
tsconfig: { module: 'CommonJS', esModuleInterop: false },
diagnostics: { warnOnly: true, pretty: false },
babelConfig: { babelrc: true },
},
resolve: null,
})

expect(cs.parsedTsConfig.options).toMatchObject({
module: ts.ModuleKind.AMD,
module: ts.ModuleKind.CommonJS,
esModuleInterop: false,
})
expect(cs.parsedTsConfig.options.allowSyntheticDefaultImports).toBeFalsy()
expect(target.lines.warn.join()).toEqual(expect.not.stringContaining(Errors.ConfigNoModuleInterop))
})
}) // parsedTsConfig

Expand Down
7 changes: 6 additions & 1 deletion src/legacy/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,14 @@ export class ConfigSet {
? this.compilerModule.ModuleKind.CommonJS
: this.compilerModule.ModuleKind.ESNext
const moduleValue = finalOptions.module ?? defaultModule
const warningModulesForEsmInterop = [
this.compilerModule.ModuleKind.CommonJS,
this.compilerModule.ModuleKind.Node16,
this.compilerModule.ModuleKind.NodeNext,
]
if (
!this.babelConfig &&
moduleValue !== this.compilerModule.ModuleKind.CommonJS &&
!warningModulesForEsmInterop.includes(moduleValue) &&
!(finalOptions.esModuleInterop || finalOptions.allowSyntheticDefaultImports)
) {
result.errors.push({
Expand Down

0 comments on commit 99c4f49

Please sign in to comment.