Skip to content

Commit

Permalink
feat(config/jest): add swc_mut_cjs_exports plugin to SWC configuration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrolfs committed Jan 8, 2024
1 parent b4c1fde commit 7e87380
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ dist
!.yarn/plugins
!.yarn/sdks
!.yarn/versions
.pnp.*
.pnp.*

.swc/
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
"@commitlint/config-conventional": "^17.8.1",
"@commitlint/prompt": "^17.8.1",
"@swc-node/jest": "^1.5.6",
"@swc/core": "^1.3.38",
"@swc/helpers": "^0.4.14",
"@swc/core": "^1.3.102",
"@swc/helpers": "^0.5.3",
"@types/jest": "^29.5.4",
"@types/lodash.has": "^4.5.8",
"@types/mkdirp": "^1.0.2",
Expand Down Expand Up @@ -85,10 +85,12 @@
"jest-watch-typeahead": "^2.2.2",
"lint-staged": "^15.1.0",
"lodash.has": "^4.5.2",
"lodash.merge": "^4.6.2",
"mkdirp": "^2.1.3",
"prettier": "^2.8.8",
"read-pkg-up": "^7.0.1",
"rimraf": "^4.1.1",
"swc_mut_cjs_exports": "^0.86.17",
"tslib": "^2.6.2",
"typescript": "^4.9.5",
"which": "^3.0.0",
Expand Down Expand Up @@ -130,6 +132,7 @@
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@types/cross-spawn": "^6.0.4",
"@types/lodash.merge": "^4",
"depcheck": "^1.4.7",
"eslint-config-kentcdodds": "^20.5.0",
"husky": "^8.0.3",
Expand Down
Empty file added src/api/test.js
Empty file.
52 changes: 51 additions & 1 deletion src/config/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/** @typedef {import('@jest/types').Config.InitialOptions} JestConfig */
/** @typedef {import('@swc-node/core').Options} SwcNodeOptions */

const {dirname} = require('path')
const merge = require('lodash.merge')
const {
readDefaultTsConfig,
tsCompilerOptionsToSwcConfig,
} = require('@swc-node/register/read-default-tsconfig')
const {ifAnyDep, hasFile, fromRoot, hasDevDep} = require('../utils')

const {
Expand All @@ -17,6 +24,23 @@ const ignores = [
'__mocks__',
]

/**
* Get the path at which `@hover/javascript/jest` is installed in a dependent
* project in order to resolve the Jest preset as sometimes package managers
* nest the preset installation within the `@hover/javascript` installation.
*
* @returns
*/
const getResolvePaths = () => {
try {
const nested = require.resolve('@hover/javascript/jest')

return {paths: [dirname(nested)]}
} catch {
return undefined
}
}

/** @type JestConfig */
const jestConfig = {
roots: [fromRoot('.')],
Expand Down Expand Up @@ -50,7 +74,33 @@ const jestConfig = {
],
),
)
: {'^.+\\.(t|j)sx?$': [require.resolve('@swc-node/jest')]},
: {
'^.+\\.(t|j)sx?$': [
require.resolve('@swc-node/jest', getResolvePaths()),
/** @type {SwcNodeOptions} */ (
merge(tsCompilerOptionsToSwcConfig(readDefaultTsConfig(), ''), {
esModuleInterop: true,
module: 'commonjs',
swc: {
jsc: {
target: 'es2020',
experimental: {
plugins: [[require.resolve('swc_mut_cjs_exports'), {}]],
},
parser: {
syntax: 'typescript',
tsx: true,
decorators: false,
dynamicimport: true,
},
loose: true,
externalHelpers: false,
},
},
})
),
],
},
coveragePathIgnorePatterns: [
...ignores,
'src/(umd|cjs|esm)-entry.js$',
Expand Down
3 changes: 2 additions & 1 deletion src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"declarationMap": true,
"emitDeclarationOnly": true,
"outDir": "../dist",
"rootDir": "."
"rootDir": ".",
"skipLibCheck": true
}
}
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2223,6 +2223,7 @@ __metadata:
"@types/cross-spawn": ^6.0.4
"@types/jest": ^29.5.4
"@types/lodash.has": ^4.5.8
"@types/lodash.merge": ^4
"@types/mkdirp": ^1.0.2
"@types/node": ^18.18.6
"@types/rimraf": ^3.0.2
Expand Down Expand Up @@ -2257,12 +2258,14 @@ __metadata:
jest-watch-typeahead: ^2.2.2
lint-staged: ^15.1.0
lodash.has: ^4.5.2
lodash.merge: ^4.6.2
mkdirp: ^2.1.3
npm-run-all: ^4.1.5
prettier: ^2.8.8
read-pkg-up: ^7.0.1
rimraf: ^4.1.1
slash: ^3.0.0
swc_mut_cjs_exports: ^0.86.17
tslib: ^2.6.2
typescript: ^4.9.5
which: ^3.0.0
Expand Down Expand Up @@ -3269,6 +3272,15 @@ __metadata:
languageName: node
linkType: hard

"@types/lodash.merge@npm:^4":
version: 4.6.9
resolution: "@types/lodash.merge@npm:4.6.9"
dependencies:
"@types/lodash": "*"
checksum: d0dd6654547c9d8d905184d14aa5c2a37a1ed1c3204f5ab20b7d591a05f34859ef09d3b72c065e94ca1989abf9109eb8230f67c4d64a5768b1d65b9ed8baf8e7
languageName: node
linkType: hard

"@types/lodash@npm:*":
version: 4.14.181
resolution: "@types/lodash@npm:4.14.181"
Expand Down Expand Up @@ -11358,6 +11370,16 @@ __metadata:
languageName: node
linkType: hard

"swc_mut_cjs_exports@npm:^0.86.17":
version: 0.86.17
resolution: "swc_mut_cjs_exports@npm:0.86.17"
peerDependencies:
"@swc/core": ^1.3.58
"@swc/jest": ^0.2.26
checksum: 709e0b3d32ea6a70f12c29d6455454eb9b27f27708f57b0fb9eb76ce55c1876acec65a26f53d27b00a5e3a114afd28dad472e1b32be1d6d89d60563ec1cf9bc5
languageName: node
linkType: hard

"symbol-tree@npm:^3.2.4":
version: 3.2.4
resolution: "symbol-tree@npm:3.2.4"
Expand Down

0 comments on commit 7e87380

Please sign in to comment.