Skip to content

Commit 7e87380

Browse files
committed
feat(config/jest): add swc_mut_cjs_exports plugin to SWC configuration
In an effort to make the SWC-based Jest compilation as backward compatible as possible, add the `swc_mut_cjs_exports` plugin so that Jest spies work in all the places they used to work. See: https://github.com/magic-akari/swc_mut_cjs_exports
1 parent b4c1fde commit 7e87380

File tree

6 files changed

+83
-5
lines changed

6 files changed

+83
-5
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ dist
1414
!.yarn/plugins
1515
!.yarn/sdks
1616
!.yarn/versions
17-
.pnp.*
17+
.pnp.*
18+
19+
.swc/

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
"@commitlint/config-conventional": "^17.8.1",
5252
"@commitlint/prompt": "^17.8.1",
5353
"@swc-node/jest": "^1.5.6",
54-
"@swc/core": "^1.3.38",
55-
"@swc/helpers": "^0.4.14",
54+
"@swc/core": "^1.3.102",
55+
"@swc/helpers": "^0.5.3",
5656
"@types/jest": "^29.5.4",
5757
"@types/lodash.has": "^4.5.8",
5858
"@types/mkdirp": "^1.0.2",
@@ -85,10 +85,12 @@
8585
"jest-watch-typeahead": "^2.2.2",
8686
"lint-staged": "^15.1.0",
8787
"lodash.has": "^4.5.2",
88+
"lodash.merge": "^4.6.2",
8889
"mkdirp": "^2.1.3",
8990
"prettier": "^2.8.8",
9091
"read-pkg-up": "^7.0.1",
9192
"rimraf": "^4.1.1",
93+
"swc_mut_cjs_exports": "^0.86.17",
9294
"tslib": "^2.6.2",
9395
"typescript": "^4.9.5",
9496
"which": "^3.0.0",
@@ -130,6 +132,7 @@
130132
"@babel/core": "^7.23.2",
131133
"@babel/preset-env": "^7.23.2",
132134
"@types/cross-spawn": "^6.0.4",
135+
"@types/lodash.merge": "^4",
133136
"depcheck": "^1.4.7",
134137
"eslint-config-kentcdodds": "^20.5.0",
135138
"husky": "^8.0.3",

src/api/test.js

Whitespace-only changes.

src/config/jest.config.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/** @typedef {import('@jest/types').Config.InitialOptions} JestConfig */
2+
/** @typedef {import('@swc-node/core').Options} SwcNodeOptions */
23

4+
const {dirname} = require('path')
5+
const merge = require('lodash.merge')
6+
const {
7+
readDefaultTsConfig,
8+
tsCompilerOptionsToSwcConfig,
9+
} = require('@swc-node/register/read-default-tsconfig')
310
const {ifAnyDep, hasFile, fromRoot, hasDevDep} = require('../utils')
411

512
const {
@@ -17,6 +24,23 @@ const ignores = [
1724
'__mocks__',
1825
]
1926

27+
/**
28+
* Get the path at which `@hover/javascript/jest` is installed in a dependent
29+
* project in order to resolve the Jest preset as sometimes package managers
30+
* nest the preset installation within the `@hover/javascript` installation.
31+
*
32+
* @returns
33+
*/
34+
const getResolvePaths = () => {
35+
try {
36+
const nested = require.resolve('@hover/javascript/jest')
37+
38+
return {paths: [dirname(nested)]}
39+
} catch {
40+
return undefined
41+
}
42+
}
43+
2044
/** @type JestConfig */
2145
const jestConfig = {
2246
roots: [fromRoot('.')],
@@ -50,7 +74,33 @@ const jestConfig = {
5074
],
5175
),
5276
)
53-
: {'^.+\\.(t|j)sx?$': [require.resolve('@swc-node/jest')]},
77+
: {
78+
'^.+\\.(t|j)sx?$': [
79+
require.resolve('@swc-node/jest', getResolvePaths()),
80+
/** @type {SwcNodeOptions} */ (
81+
merge(tsCompilerOptionsToSwcConfig(readDefaultTsConfig(), ''), {
82+
esModuleInterop: true,
83+
module: 'commonjs',
84+
swc: {
85+
jsc: {
86+
target: 'es2020',
87+
experimental: {
88+
plugins: [[require.resolve('swc_mut_cjs_exports'), {}]],
89+
},
90+
parser: {
91+
syntax: 'typescript',
92+
tsx: true,
93+
decorators: false,
94+
dynamicimport: true,
95+
},
96+
loose: true,
97+
externalHelpers: false,
98+
},
99+
},
100+
})
101+
),
102+
],
103+
},
54104
coveragePathIgnorePatterns: [
55105
...ignores,
56106
'src/(umd|cjs|esm)-entry.js$',

src/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"declarationMap": true,
99
"emitDeclarationOnly": true,
1010
"outDir": "../dist",
11-
"rootDir": "."
11+
"rootDir": ".",
12+
"skipLibCheck": true
1213
}
1314
}

yarn.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,7 @@ __metadata:
22232223
"@types/cross-spawn": ^6.0.4
22242224
"@types/jest": ^29.5.4
22252225
"@types/lodash.has": ^4.5.8
2226+
"@types/lodash.merge": ^4
22262227
"@types/mkdirp": ^1.0.2
22272228
"@types/node": ^18.18.6
22282229
"@types/rimraf": ^3.0.2
@@ -2257,12 +2258,14 @@ __metadata:
22572258
jest-watch-typeahead: ^2.2.2
22582259
lint-staged: ^15.1.0
22592260
lodash.has: ^4.5.2
2261+
lodash.merge: ^4.6.2
22602262
mkdirp: ^2.1.3
22612263
npm-run-all: ^4.1.5
22622264
prettier: ^2.8.8
22632265
read-pkg-up: ^7.0.1
22642266
rimraf: ^4.1.1
22652267
slash: ^3.0.0
2268+
swc_mut_cjs_exports: ^0.86.17
22662269
tslib: ^2.6.2
22672270
typescript: ^4.9.5
22682271
which: ^3.0.0
@@ -3269,6 +3272,15 @@ __metadata:
32693272
languageName: node
32703273
linkType: hard
32713274

3275+
"@types/lodash.merge@npm:^4":
3276+
version: 4.6.9
3277+
resolution: "@types/lodash.merge@npm:4.6.9"
3278+
dependencies:
3279+
"@types/lodash": "*"
3280+
checksum: d0dd6654547c9d8d905184d14aa5c2a37a1ed1c3204f5ab20b7d591a05f34859ef09d3b72c065e94ca1989abf9109eb8230f67c4d64a5768b1d65b9ed8baf8e7
3281+
languageName: node
3282+
linkType: hard
3283+
32723284
"@types/lodash@npm:*":
32733285
version: 4.14.181
32743286
resolution: "@types/lodash@npm:4.14.181"
@@ -11358,6 +11370,16 @@ __metadata:
1135811370
languageName: node
1135911371
linkType: hard
1136011372

11373+
"swc_mut_cjs_exports@npm:^0.86.17":
11374+
version: 0.86.17
11375+
resolution: "swc_mut_cjs_exports@npm:0.86.17"
11376+
peerDependencies:
11377+
"@swc/core": ^1.3.58
11378+
"@swc/jest": ^0.2.26
11379+
checksum: 709e0b3d32ea6a70f12c29d6455454eb9b27f27708f57b0fb9eb76ce55c1876acec65a26f53d27b00a5e3a114afd28dad472e1b32be1d6d89d60563ec1cf9bc5
11380+
languageName: node
11381+
linkType: hard
11382+
1136111383
"symbol-tree@npm:^3.2.4":
1136211384
version: 3.2.4
1136311385
resolution: "symbol-tree@npm:3.2.4"

0 commit comments

Comments
 (0)