Skip to content

Commit c3a0b57

Browse files
authored
feat: support Babel 8 (#5466)
Closes #5412
1 parent 5fb44c4 commit c3a0b57

15 files changed

Lines changed: 6218 additions & 2 deletions
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { runJest } from '../run-jest'
2+
import { getFixturePath, runCommand, runNpmCi } from '../utils'
3+
4+
const fixtureName = 'babel-8-compat'
5+
const fixturePath = getFixturePath(fixtureName)
6+
const nodeMajor = Number(process.versions.node.split('.')[0])
7+
const supportsBabel8 = nodeMajor >= 22
8+
9+
const itWithBabel8Node = (...args: Parameters<typeof it>) => {
10+
if (supportsBabel8) {
11+
// eslint-disable-next-line jest/valid-title,jest/expect-expect,jest/no-disabled-tests
12+
return it(...args)
13+
}
14+
15+
// eslint-disable-next-line jest/valid-title,jest/expect-expect,jest/no-disabled-tests
16+
return it.skip(...args)
17+
}
18+
19+
beforeAll(async () => {
20+
if (supportsBabel8) {
21+
await runNpmCi(fixtureName)
22+
}
23+
})
24+
25+
describe('Babel 8 compatibility', () => {
26+
itWithBabel8Node('uses Babel 8 from the fixture', async () => {
27+
const result = await runCommand(
28+
process.execPath,
29+
['-p', "require('@babel/core/package.json').version"],
30+
fixturePath,
31+
)
32+
33+
// eslint-disable-next-line jest/no-standalone-expect
34+
expect(result).toMatchObject({ exitCode: 0 })
35+
// eslint-disable-next-line jest/no-standalone-expect
36+
expect(result.stdout.trim()).toMatch(/^8\./)
37+
})
38+
39+
for (const configFile of ['jest-compiler-cjs.config.ts', 'jest-compiler-esm.config.ts']) {
40+
itWithBabel8Node(`runs ${configFile} with Babel 8`, async () => {
41+
const result = await runJest(fixtureName, configFile)
42+
43+
// eslint-disable-next-line jest/no-standalone-expect
44+
expect(result).toMatchObject({ exitCode: 0 })
45+
})
46+
}
47+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { babelVersionMarker } from '../src/cjs-message'
2+
3+
describe('Babel 8 CommonJS compatibility', () => {
4+
it('processes JavaScript through ts-jest and Babel', () => {
5+
expect(babelVersionMarker).toBe('Babel 8')
6+
})
7+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { babelVersionMarker } from '../src/esm-message'
2+
3+
describe('Babel 8 ESM compatibility', () => {
4+
it('processes TypeScript through ts-jest and Babel', () => {
5+
expect(babelVersionMarker).toBe('Babel 8')
6+
})
7+
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = (api) => {
2+
api.assertVersion('^8.0.0')
3+
api.cache(true)
4+
5+
return {
6+
plugins: [
7+
() => ({
8+
visitor: {
9+
StringLiteral(path) {
10+
if (path.node.value === '__BABEL_8__') {
11+
path.node.value = 'Babel 8'
12+
}
13+
},
14+
},
15+
}),
16+
],
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineConfig } from 'jest'
2+
3+
import { TS_JS_TRANSFORM_PATTERN } from '../../dist/constants.js'
4+
import type { TsJestTransformerOptions } from '../../src'
5+
6+
export default defineConfig({
7+
displayName: 'babel-8-compat-compiler-cjs',
8+
testMatch: ['<rootDir>/__tests__/cjs.spec.ts'],
9+
transform: {
10+
[TS_JS_TRANSFORM_PATTERN]: [
11+
'<rootDir>/../../dist/index.js',
12+
{
13+
babelConfig: true,
14+
tsconfig: '<rootDir>/tsconfig-cjs.spec.json',
15+
} satisfies TsJestTransformerOptions,
16+
],
17+
},
18+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { defineConfig } from 'jest'
2+
3+
import { TS_TRANSFORM_PATTERN } from '../../dist/constants.js'
4+
import type { TsJestTransformerOptions } from '../../src'
5+
6+
export default defineConfig({
7+
displayName: 'babel-8-compat-compiler-esm',
8+
extensionsToTreatAsEsm: ['.ts'],
9+
testMatch: ['<rootDir>/__tests__/esm.spec.ts'],
10+
transform: {
11+
[TS_TRANSFORM_PATTERN]: [
12+
'<rootDir>/../../dist/index.js',
13+
{
14+
babelConfig: true,
15+
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
16+
useESM: true,
17+
} satisfies TsJestTransformerOptions,
18+
],
19+
},
20+
})

0 commit comments

Comments
 (0)