Skip to content
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

Merged
merged 1 commit into from Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions e2e/__helpers__/test-case/runtime.ts
Expand Up @@ -27,7 +27,7 @@ import * as Paths from '../../../scripts/lib/paths'

import RunResult from './run-result'
import type { PreparedTest, RunTestOptions } from './types'
import { enableOptimizations, templateNameForPath } from './utils'
import { enableOptimizations, nodeWithESMSupport, templateNameForPath } from './utils'

const TEMPLATE_EXCLUDED_ITEMS = ['node_modules', 'package-lock.json']

Expand Down Expand Up @@ -55,7 +55,7 @@ function hooksSourceWith(vars: Record<string, any>): string {
}

export function run(name: string, options: RunTestOptions = {}): RunResult {
const { env = {}, template, inject, writeIo, noCache, jestConfigPath: configFile = 'jest.config.js' } = options
const { env = {}, template, inject, writeIo, noCache, jestConfigPath: configFile = 'jest.config.js', jestConfig } = options
const { workdir: dir, sourceDir } = prepareTest(
name,
template || templateNameForPath(join(Paths.e2eSourceDir, name)),
Expand Down Expand Up @@ -89,8 +89,8 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
}

// extends config
if (options.jestConfig) {
merge(extraConfig, options.jestConfig)
if (jestConfig) {
merge(extraConfig, jestConfig)
}
if (options.tsJestConfig) {
const globalConfig: any = extraConfig.globals || (extraConfig.globals = {'ts-jest': {}})
Expand All @@ -108,6 +108,11 @@ export function run(name: string, options: RunTestOptions = {}): RunResult {
extraConfig.cacheDirectory = join(Paths.cacheDir, `e2e-${template}`)
}

// Run with ESM support
if (nodeWithESMSupport && jestConfig?.extensionsToTreatAsEsm?.length) {
shortCmd = 'node --experimental-vm-modules node_modules/.bin/jest'
}

// build final config and create dir suffix based on it
const finalConfig = merge({}, baseConfig, extraConfig)
const digest = createHash('sha1')
Expand Down
2 changes: 1 addition & 1 deletion e2e/__helpers__/test-case/types.ts
Expand Up @@ -9,7 +9,7 @@ export interface RunTestOptions {
env?: Record<string, unknown>
inject?: (() => any) | string
writeIo?: boolean
jestConfig?: Config.ProjectConfig | Record<string, unknown>
jestConfig?: Config.InitialOptions
tsJestConfig?: TsJestGlobalOptions
noCache?: boolean
jestConfigPath?: string
Expand Down
2 changes: 2 additions & 0 deletions e2e/__helpers__/test-case/utils.ts
Expand Up @@ -55,3 +55,5 @@ export function escapeRegex(s: string): string {
export function enableOptimizations(): boolean {
return !!process.env.TS_JEST_E2E_OPTIMIZATIONS
}

export const nodeWithESMSupport = process.version.startsWith('v12') || process.version.startsWith('v14')
4 changes: 1 addition & 3 deletions e2e/__tests__/allow-js.test.ts
Expand Up @@ -3,7 +3,7 @@ import { configureTestCase } from '../__helpers__/test-case'

describe('using babel-jest for js files', () => {
const testCase = configureTestCase('allow-js/with-outDir', {
jestConfig: { testMatch: null, testRegex: '(foo|bar)\\.spec\\.[jt]s$' },
jestConfig: { testRegex: '(foo|bar)\\.spec\\.[jt]s$' },
})

testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => {
Expand All @@ -19,7 +19,6 @@ describe('using ts-jest for js files with outDir', () => {
const testCase = configureTestCase('allow-js/with-outDir', {
jestConfig: {
preset: 'ts-jest/presets/js-with-ts',
testMatch: null,
testRegex: 'esm\\.spec\\.[jt]s$',
},
})
Expand All @@ -37,7 +36,6 @@ describe('using ts-jest for js files without outDir', () => {
const testCase = configureTestCase('allow-js/without-outDir', {
jestConfig: {
preset: 'ts-jest/presets/js-with-ts',
testMatch: null,
testRegex: 'esm\\.spec\\.[jt]s$',
},
})
Expand Down
10 changes: 5 additions & 5 deletions e2e/__tests__/module-kinds/__snapshots__/amd.test.ts.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`run with options: {"module":"amd","allowSyntheticDefaultImports":false} 1`] = `
exports[`Module amd run with options: {"module":"amd","allowSyntheticDefaultImports":false} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -44,7 +44,7 @@ exports[`run with options: {"module":"amd","allowSyntheticDefaultImports":false}
================================================================================
`;

exports[`run with options: {"module":"amd","allowSyntheticDefaultImports":true} 1`] = `
exports[`Module amd run with options: {"module":"amd","allowSyntheticDefaultImports":true} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -78,7 +78,7 @@ exports[`run with options: {"module":"amd","allowSyntheticDefaultImports":true}
================================================================================
`;

exports[`run with options: {"module":"amd","esModuleInterop":false} 1`] = `
exports[`Module amd run with options: {"module":"amd","esModuleInterop":false} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -113,7 +113,7 @@ exports[`run with options: {"module":"amd","esModuleInterop":false} 1`] = `
================================================================================
`;

exports[`run with options: {"module":"amd","esModuleInterop":true} 1`] = `
exports[`Module amd run with options: {"module":"amd","esModuleInterop":true} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -158,7 +158,7 @@ exports[`run with options: {"module":"amd","esModuleInterop":true} 1`] = `
================================================================================
`;

exports[`run with options: {"module":"amd"} 1`] = `
exports[`Module amd run with options: {"module":"amd"} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down
10 changes: 5 additions & 5 deletions e2e/__tests__/module-kinds/__snapshots__/commonjs.test.ts.snap
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`run with options: {"module":"commonjs","allowSyntheticDefaultImports":false} 1`] = `
exports[`Module commonjs run with options: {"module":"commonjs","allowSyntheticDefaultImports":false} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -43,7 +43,7 @@ exports[`run with options: {"module":"commonjs","allowSyntheticDefaultImports":f
================================================================================
`;

exports[`run with options: {"module":"commonjs","allowSyntheticDefaultImports":true} 1`] = `
exports[`Module commonjs run with options: {"module":"commonjs","allowSyntheticDefaultImports":true} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -77,7 +77,7 @@ exports[`run with options: {"module":"commonjs","allowSyntheticDefaultImports":t
================================================================================
`;

exports[`run with options: {"module":"commonjs","esModuleInterop":false} 1`] = `
exports[`Module commonjs run with options: {"module":"commonjs","esModuleInterop":false} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -120,7 +120,7 @@ exports[`run with options: {"module":"commonjs","esModuleInterop":false} 1`] = `
================================================================================
`;

exports[`run with options: {"module":"commonjs","esModuleInterop":true} 1`] = `
exports[`Module commonjs run with options: {"module":"commonjs","esModuleInterop":true} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down Expand Up @@ -165,7 +165,7 @@ exports[`run with options: {"module":"commonjs","esModuleInterop":true} 1`] = `
================================================================================
`;

exports[`run with options: {"module":"commonjs"} 1`] = `
exports[`Module commonjs run with options: {"module":"commonjs"} with template default 1`] = `
× jest --no-cache
↳ exit code: 1
===[ STDOUT ]===================================================================
Expand Down