Skip to content

Commit bf86aac

Browse files
committed
feat(config/test): use **@swc/jest** as default Jest transform
BREAKING CHANGE: **@swc/jest** is now the default Jest transform and **ts-jest** is no longer installed by default. To preserve the old transform behavior, install **ts-jest** as a \`devDependency\` in you project. ∙ BREAKING CHANGE: the \`@hover/javascript/api/test\` API has been removed as **ts-jest** is no longer installed by default. If you're still using **ts-jest**, import its helpers directly from **ts-jest** instead of the \`/api/test\` entrypoint.
1 parent 639f4fa commit bf86aac

File tree

7 files changed

+210
-137
lines changed

7 files changed

+210
-137
lines changed

jest.config.js

-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const {
22
globals,
3-
transform,
43
transformIgnorePatterns,
54
...config
65
} = require('./src/config/jest.config')
@@ -11,13 +10,4 @@ module.exports = {
1110
roots: ['<rootDir>/src'],
1211
coverageThreshold: null,
1312
transformIgnorePatterns: [...transformIgnorePatterns, '.prettierrc.js'],
14-
// Specifying ts-jest options via `global` in Jest configuration has been
15-
// deprecated so we have to do this in order to add the `exclude` option to
16-
// the transform in our Jest configuration that already has `transform` ☹
17-
transform: Object.fromEntries(
18-
Object.entries(transform).map(([glob, [transformer, options]]) => [
19-
glob,
20-
[transformer, {...options, exclude: '**/*'}],
21-
]),
22-
),
2313
}

package.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
"@commitlint/cli": "^16.1.0",
5454
"@commitlint/config-conventional": "^16.2.1",
5555
"@commitlint/prompt": "^16.1.0",
56+
"@swc/core": "^1.2.248",
57+
"@swc/jest": "^0.2.22",
5658
"@types/jest": "^29.0.0",
5759
"@types/lodash.has": "^4.5.6",
5860
"@types/mkdirp": "^1.0.2",
@@ -89,7 +91,6 @@
8991
"prettier": "^2.5.1",
9092
"read-pkg-up": "^7.0.1",
9193
"rimraf": "^3.0.2",
92-
"ts-jest": "^29.0.0-next.1",
9394
"tslib": "^2.4.0",
9495
"typescript": "^4",
9596
"which": "^2.0.2",
@@ -136,5 +137,13 @@
136137
"jest-in-case": "^1.0.2",
137138
"npm-run-all": "^4.1.5",
138139
"slash": "^3.0.0"
140+
},
141+
"peerDependencies": {
142+
"ts-jest": "^29.0.0"
143+
},
144+
"peerDependenciesMeta": {
145+
"ts-jest": {
146+
"optional": true
147+
}
139148
}
140149
}

src/api/__tests__/test.js

-14
This file was deleted.

src/api/test.js

-1
This file was deleted.

src/config/jest.config.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/** @typedef {import('@jest/types').Config.InitialOptions} JestConfig */
22

3-
const {jsWithTs: preset} = require('ts-jest/presets')
4-
5-
const {ifAnyDep, hasFile, fromRoot} = require('../utils')
3+
const {ifAnyDep, hasFile, fromRoot, hasDevDep} = require('../utils')
64

75
const {
86
testMatch,
@@ -33,14 +31,20 @@ const jestConfig = {
3331
testMatch,
3432
testPathIgnorePatterns: [...ignores, '<rootDir>/dist'],
3533
testLocationInResults: true,
36-
transform: Object.fromEntries(
37-
// Ensure we can resolve the preset even when
38-
// it's in a nested `node_modules` installation
39-
Object.entries(preset.transform).map(([glob, transformer]) => [
40-
glob,
41-
[require.resolve(transformer), {diagnostics: {warnOnly: true}}],
42-
]),
43-
),
34+
// The default transform is now SWC, however, `ts-jest` will
35+
// still be used if it is installed in the host project
36+
transform: hasDevDep('ts-jest')
37+
? Object.fromEntries(
38+
// Ensure we can resolve the preset even when
39+
// it's in a nested `node_modules` installation
40+
Object.entries(require('ts-jest/presets').transform).map(
41+
([glob, transformer]) => [
42+
glob,
43+
[require.resolve(transformer), {diagnostics: {warnOnly: true}}],
44+
],
45+
),
46+
)
47+
: {'^.+\\.(t|j)sx?$': ['@swc/jest']},
4448
coveragePathIgnorePatterns: [
4549
...ignores,
4650
'src/(umd|cjs|esm)-entry.js$',
@@ -63,10 +67,9 @@ const jestConfig = {
6367
require.resolve('jest-watch-typeahead/filename'),
6468
require.resolve('jest-watch-typeahead/testname'),
6569
],
66-
}
67-
68-
if (hasFile('tests/setup-env.js')) {
69-
jestConfig.setupFilesAfterEnv = [fromRoot('tests/setup-env.js')]
70+
setupFilesAfterEnv: hasFile('tests/setup-env.js')
71+
? [fromRoot('tests/setup-env.js')]
72+
: undefined,
7073
}
7174

7275
module.exports = jestConfig

src/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ module.exports = {
244244
getConcurrentlyArgs,
245245
getPkgName,
246246
hasAnyDep,
247+
hasDevDep,
247248
hasFile,
248249
hasLocalConfig,
249250
hasPkgProp,

0 commit comments

Comments
 (0)