Skip to content

Commit 2e60585

Browse files
committed
feat(config/jest): add debug information available via DEBUG=hover:jest:*
1 parent b17a059 commit 2e60585

File tree

4 files changed

+163
-49
lines changed

4 files changed

+163
-49
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"cosmiconfig": "^8.3.6",
6767
"cross-env": "^7.0.3",
6868
"cross-spawn": "^7.0.3",
69+
"debug": "^4.4.0",
6970
"doctoc": "^2.2.1",
7071
"eslint": "^8.57.1",
7172
"eslint-config-airbnb": "19.0.4",
@@ -134,6 +135,7 @@
134135
"@babel/preset-env": "^7.26.0",
135136
"@babel/preset-typescript": "^7.23.3",
136137
"@types/cross-spawn": "^6.0.6",
138+
"@types/debug": "^4",
137139
"depcheck": "^1.4.7",
138140
"eslint-config-kentcdodds": "^20.5.0",
139141
"husky": "^8.0.3",

src/config/jest.config.js

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ const {
77
readDefaultTsConfig,
88
tsCompilerOptionsToSwcConfig,
99
} = require('@swc-node/register/read-default-tsconfig')
10-
const {ifAnyDep, hasFile, fromRoot, hasDevDep} = require('../utils')
10+
11+
const {ifAnyDep, hasFile, fromRoot, hasDevDep, getDebug} = require('../utils')
1112

1213
const {
1314
testMatch,
1415
testMatchGlob,
1516
testMatchExtensions,
1617
} = require('./helpers/test-match')
1718

19+
const debug = getDebug('jest')
20+
1821
const ignores = [
1922
'/node_modules/',
2023
'/__fixtures__/',
@@ -24,12 +27,48 @@ const ignores = [
2427
'__mocks__',
2528
]
2629

30+
/**
31+
* @type {SwcNodeOptions}
32+
*/
33+
const DEFAULT_SWC_OPTIONS = {
34+
esModuleInterop: true,
35+
module: 'commonjs',
36+
react: {runtime: 'automatic'},
37+
swc: {
38+
jsc: {
39+
target: 'es2020',
40+
experimental: {
41+
plugins: [[require.resolve('swc_mut_cjs_exports'), {}]],
42+
},
43+
parser: {
44+
syntax: 'typescript',
45+
tsx: true,
46+
decorators: false,
47+
dynamicimport: true,
48+
},
49+
loose: true,
50+
externalHelpers: false,
51+
transform: {
52+
react: {
53+
runtime: 'automatic',
54+
},
55+
},
56+
},
57+
},
58+
}
59+
60+
const tsConfig = readDefaultTsConfig()
61+
const swcConfig = merge(
62+
tsCompilerOptionsToSwcConfig(tsConfig, ''),
63+
DEFAULT_SWC_OPTIONS,
64+
)
65+
66+
debug.prefix('tsconfig:paths')(tsConfig.paths)
67+
2768
/**
2869
* Get the path at which `@hover/javascript/jest` is installed in a dependent
2970
* project in order to resolve the Jest preset as sometimes package managers
3071
* nest the preset installation within the `@hover/javascript` installation.
31-
*
32-
* @returns
3372
*/
3473
const getResolvePaths = () => {
3574
try {
@@ -41,6 +80,54 @@ const getResolvePaths = () => {
4180
}
4281
}
4382

83+
/**
84+
* The default transform is now SWC, however, `ts-jest` will
85+
* still be used if it is installed in the host project
86+
*
87+
* @returns {JestConfig['transform']}
88+
*/
89+
const getTransform = () => {
90+
const log = debug.prefix('transform')
91+
92+
if (hasDevDep('ts-jest')) {
93+
log('Detected `ts-jest` package, using ts-jest transform')
94+
95+
const transform = Object.fromEntries(
96+
// Ensure we can resolve the preset even when
97+
// it's in a nested `node_modules` installation
98+
Object.entries(require('ts-jest/presets').jsWithTs.transform).map(
99+
([glob, [transformer, options]]) => [
100+
glob,
101+
[
102+
require.resolve(transformer),
103+
{
104+
...options,
105+
diagnostics: false,
106+
},
107+
],
108+
],
109+
),
110+
)
111+
112+
log(transform)
113+
114+
return transform
115+
}
116+
117+
log('No `ts-jest` package detected, using default SWC transform')
118+
119+
const transform = {
120+
'^.+\\.(t|j|mj)sx?$': [
121+
require.resolve('@swc-node/jest', getResolvePaths()),
122+
swcConfig,
123+
],
124+
}
125+
126+
log(transform)
127+
128+
return transform
129+
}
130+
44131
/** @type JestConfig */
45132
const jestConfig = {
46133
roots: [fromRoot('.')],
@@ -55,52 +142,7 @@ const jestConfig = {
55142
testMatch,
56143
testPathIgnorePatterns: [...ignores, '<rootDir>/dist'],
57144
testLocationInResults: true,
58-
// The default transform is now SWC, however, `ts-jest` will
59-
// still be used if it is installed in the host project
60-
transform: hasDevDep('ts-jest')
61-
? Object.fromEntries(
62-
// Ensure we can resolve the preset even when
63-
// it's in a nested `node_modules` installation
64-
Object.entries(require('ts-jest/presets').jsWithTs.transform).map(
65-
([glob, [transformer, options]]) => [
66-
glob,
67-
[
68-
require.resolve(transformer),
69-
{
70-
...options,
71-
diagnostics: false,
72-
},
73-
],
74-
],
75-
),
76-
)
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-
},
145+
transform: getTransform(),
104146
coveragePathIgnorePatterns: [
105147
...ignores,
106148
'src/(umd|cjs|esm)-entry.js$',

src/utils.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const debug = require('debug')
12
const fs = require('fs')
23
const path = require('path')
34
const rimraf = require('rimraf')
@@ -266,10 +267,49 @@ const relative =
266267
p =>
267268
path.join(dirname, p).replace(process.cwd(), '.')
268269

270+
const DEBUG_NAMESPACE = 'hover'
271+
272+
/**
273+
* Get debug output functions prefixed for the provided namespace.
274+
*
275+
* @param {string} namespace
276+
*/
277+
const getDebug = namespace => {
278+
/**
279+
* @param {string} [prefix]
280+
*/
281+
const debugForNamespace = prefix =>
282+
debug([DEBUG_NAMESPACE, namespace, prefix].filter(Boolean).join(':'))
283+
284+
return Object.assign(debugForNamespace(), {
285+
/**
286+
* Output and return an an expression for debugging.
287+
*
288+
* @template T
289+
*
290+
* @param {T} output
291+
* @param {string} [prefix] Optional prefix to append to debug namespace
292+
*
293+
* @returns {T}
294+
*/
295+
trace: (output, prefix) => {
296+
debugForNamespace(prefix)(output)
297+
return output
298+
},
299+
/**
300+
* Get a debug function with an additional prefix appended to namespace.
301+
*
302+
* @param {string} prefix
303+
*/
304+
prefix: prefix => debugForNamespace(prefix),
305+
})
306+
}
307+
269308
module.exports = {
270309
appDirectory,
271310
fromRoot,
272311
getConcurrentlyArgs,
312+
getDebug,
273313
getPkgName,
274314
hasAnyDep,
275315
hasDevDep,

yarn.lock

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,7 @@ __metadata:
22792279
"@swc/core": ^1.3.102
22802280
"@swc/helpers": ^0.5.3
22812281
"@types/cross-spawn": ^6.0.6
2282+
"@types/debug": ^4
22822283
"@types/jest": ^29.5.4
22832284
"@types/lodash.has": ^4.5.9
22842285
"@types/mkdirp": ^1.0.2
@@ -2292,6 +2293,7 @@ __metadata:
22922293
cosmiconfig: ^8.3.6
22932294
cross-env: ^7.0.3
22942295
cross-spawn: ^7.0.3
2296+
debug: ^4.4.0
22952297
depcheck: ^1.4.7
22962298
doctoc: ^2.2.1
22972299
eslint: ^8.57.1
@@ -3311,6 +3313,15 @@ __metadata:
33113313
languageName: node
33123314
linkType: hard
33133315

3316+
"@types/debug@npm:^4":
3317+
version: 4.1.12
3318+
resolution: "@types/debug@npm:4.1.12"
3319+
dependencies:
3320+
"@types/ms": "*"
3321+
checksum: 47876a852de8240bfdaf7481357af2b88cb660d30c72e73789abf00c499d6bc7cd5e52f41c915d1b9cd8ec9fef5b05688d7b7aef17f7f272c2d04679508d1053
3322+
languageName: node
3323+
linkType: hard
3324+
33143325
"@types/glob@npm:*":
33153326
version: 7.2.0
33163327
resolution: "@types/glob@npm:7.2.0"
@@ -3438,6 +3449,13 @@ __metadata:
34383449
languageName: node
34393450
linkType: hard
34403451

3452+
"@types/ms@npm:*":
3453+
version: 2.1.0
3454+
resolution: "@types/ms@npm:2.1.0"
3455+
checksum: 532d2ebb91937ccc4a89389715e5b47d4c66e708d15942fe6cc25add6dc37b2be058230a327dd50f43f89b8b6d5d52b74685a9e8f70516edfc9bdd6be910eff4
3456+
languageName: node
3457+
linkType: hard
3458+
34413459
"@types/node@npm:*":
34423460
version: 18.11.18
34433461
resolution: "@types/node@npm:18.11.18"
@@ -5500,6 +5518,18 @@ __metadata:
55005518
languageName: node
55015519
linkType: hard
55025520

5521+
"debug@npm:^4.4.0":
5522+
version: 4.4.0
5523+
resolution: "debug@npm:4.4.0"
5524+
dependencies:
5525+
ms: ^2.1.3
5526+
peerDependenciesMeta:
5527+
supports-color:
5528+
optional: true
5529+
checksum: fb42df878dd0e22816fc56e1fdca9da73caa85212fbe40c868b1295a6878f9101ae684f4eeef516c13acfc700f5ea07f1136954f43d4cd2d477a811144136479
5530+
languageName: node
5531+
linkType: hard
5532+
55035533
"decamelize-keys@npm:^1.1.0":
55045534
version: 1.1.0
55055535
resolution: "decamelize-keys@npm:1.1.0"

0 commit comments

Comments
 (0)