Skip to content

Commit

Permalink
feat(config/test): use **@swc/jest** as default Jest transform
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrolfs committed Sep 7, 2022
1 parent 639f4fa commit bf86aac
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 137 deletions.
10 changes: 0 additions & 10 deletions jest.config.js
@@ -1,6 +1,5 @@
const {
globals,
transform,
transformIgnorePatterns,
...config
} = require('./src/config/jest.config')
Expand All @@ -11,13 +10,4 @@ module.exports = {
roots: ['<rootDir>/src'],
coverageThreshold: null,
transformIgnorePatterns: [...transformIgnorePatterns, '.prettierrc.js'],
// Specifying ts-jest options via `global` in Jest configuration has been
// deprecated so we have to do this in order to add the `exclude` option to
// the transform in our Jest configuration that already has `transform` ☹
transform: Object.fromEntries(
Object.entries(transform).map(([glob, [transformer, options]]) => [
glob,
[transformer, {...options, exclude: '**/*'}],
]),
),
}
11 changes: 10 additions & 1 deletion package.json
Expand Up @@ -53,6 +53,8 @@
"@commitlint/cli": "^16.1.0",
"@commitlint/config-conventional": "^16.2.1",
"@commitlint/prompt": "^16.1.0",
"@swc/core": "^1.2.248",
"@swc/jest": "^0.2.22",
"@types/jest": "^29.0.0",
"@types/lodash.has": "^4.5.6",
"@types/mkdirp": "^1.0.2",
Expand Down Expand Up @@ -89,7 +91,6 @@
"prettier": "^2.5.1",
"read-pkg-up": "^7.0.1",
"rimraf": "^3.0.2",
"ts-jest": "^29.0.0-next.1",
"tslib": "^2.4.0",
"typescript": "^4",
"which": "^2.0.2",
Expand Down Expand Up @@ -136,5 +137,13 @@
"jest-in-case": "^1.0.2",
"npm-run-all": "^4.1.5",
"slash": "^3.0.0"
},
"peerDependencies": {
"ts-jest": "^29.0.0"
},
"peerDependenciesMeta": {
"ts-jest": {
"optional": true
}
}
}
14 changes: 0 additions & 14 deletions src/api/__tests__/test.js

This file was deleted.

1 change: 0 additions & 1 deletion src/api/test.js

This file was deleted.

33 changes: 18 additions & 15 deletions src/config/jest.config.js
@@ -1,8 +1,6 @@
/** @typedef {import('@jest/types').Config.InitialOptions} JestConfig */

const {jsWithTs: preset} = require('ts-jest/presets')

const {ifAnyDep, hasFile, fromRoot} = require('../utils')
const {ifAnyDep, hasFile, fromRoot, hasDevDep} = require('../utils')

const {
testMatch,
Expand Down Expand Up @@ -33,14 +31,20 @@ const jestConfig = {
testMatch,
testPathIgnorePatterns: [...ignores, '<rootDir>/dist'],
testLocationInResults: true,
transform: Object.fromEntries(
// Ensure we can resolve the preset even when
// it's in a nested `node_modules` installation
Object.entries(preset.transform).map(([glob, transformer]) => [
glob,
[require.resolve(transformer), {diagnostics: {warnOnly: true}}],
]),
),
// The default transform is now SWC, however, `ts-jest` will
// still be used if it is installed in the host project
transform: hasDevDep('ts-jest')
? Object.fromEntries(
// Ensure we can resolve the preset even when
// it's in a nested `node_modules` installation
Object.entries(require('ts-jest/presets').transform).map(
([glob, transformer]) => [
glob,
[require.resolve(transformer), {diagnostics: {warnOnly: true}}],
],
),
)
: {'^.+\\.(t|j)sx?$': ['@swc/jest']},
coveragePathIgnorePatterns: [
...ignores,
'src/(umd|cjs|esm)-entry.js$',
Expand All @@ -63,10 +67,9 @@ const jestConfig = {
require.resolve('jest-watch-typeahead/filename'),
require.resolve('jest-watch-typeahead/testname'),
],
}

if (hasFile('tests/setup-env.js')) {
jestConfig.setupFilesAfterEnv = [fromRoot('tests/setup-env.js')]
setupFilesAfterEnv: hasFile('tests/setup-env.js')
? [fromRoot('tests/setup-env.js')]
: undefined,
}

module.exports = jestConfig
1 change: 1 addition & 0 deletions src/utils.js
Expand Up @@ -244,6 +244,7 @@ module.exports = {
getConcurrentlyArgs,
getPkgName,
hasAnyDep,
hasDevDep,
hasFile,
hasLocalConfig,
hasPkgProp,
Expand Down

0 comments on commit bf86aac

Please sign in to comment.