Repository navigation
Expand file tree
/
Copy pathjest.config.base.cjs
More file actions
121 lines (110 loc) · 3.14 KB
/
Copy pathjest.config.base.cjs
File metadata and controls
121 lines (110 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const fs = require('node:fs');
const path = require('node:path');
const workspaceRoot = __dirname;
/**
* Map `@imtbl/<name>` → `<packageRoot>/src` from workspace package.json files.
* Avoids relying on `publicHoistPattern: ['@imtbl/*']`.
*/
function buildImtblSrcMapper() {
/** @type {Record<string, string>} */
const mapper = {};
const candidates = [
'sdk',
'packages',
];
/** @param {string} dir */
function walk(dir) {
if (!fs.existsSync(dir)) return;
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
if (
ent.name === 'node_modules' ||
ent.name === 'dist' ||
ent.name === 'sample-app'
) {
continue;
}
const full = path.join(dir, ent.name);
if (ent.isDirectory()) {
walk(full);
continue;
}
if (ent.name !== 'package.json') continue;
const pkg = JSON.parse(fs.readFileSync(full, 'utf8'));
if (!pkg.name?.startsWith('@imtbl/')) continue;
const pkgRoot = path.dirname(full);
const srcDir = path.join(pkgRoot, 'src');
if (!fs.existsSync(srcDir)) continue;
// Exact package name → src root; subpaths → src/<subpath>
mapper[`^${pkg.name}$`] = srcDir;
mapper[`^${pkg.name}/(.*)$`] = path.join(srcDir, '$1');
}
}
for (const rel of candidates) {
walk(path.join(workspaceRoot, rel));
}
return mapper;
}
const imtblSrcMapper = buildImtblSrcMapper();
/**
* Shared Jest preset for SDK packages.
* Maps @imtbl/* to workspace package sources so tests don't need a prior full build.
*
* @param {import('jest').Config & {
* testEnvironment?: 'node' | 'jsdom',
* moduleNameMapper?: Record<string, string>,
* }} options
* @returns {import('jest').Config}
*/
function createPackageJestConfig(options = {}) {
const {
testEnvironment = 'node',
setupFiles,
setupFilesAfterEnv,
restoreMocks,
verbose,
transformIgnorePatterns = [],
coveragePathIgnorePatterns,
moduleNameMapper = {},
roots = ['<rootDir>/src'],
transform = {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
} = options;
return {
clearMocks: true,
coverageProvider: 'v8',
moduleDirectories: ['node_modules', 'src'],
roots,
testEnvironment,
transform,
transformIgnorePatterns,
moduleNameMapper: {
// Package-specific mappers first (Jest uses first match).
...moduleNameMapper,
...imtblSrcMapper,
},
...(setupFiles ? { setupFiles } : {}),
...(setupFilesAfterEnv ? { setupFilesAfterEnv } : {}),
...(restoreMocks ? { restoreMocks } : {}),
...(verbose ? { verbose } : {}),
...(coveragePathIgnorePatterns
? { coveragePathIgnorePatterns }
: {}),
};
}
function createNodeJestConfig(options = {}) {
return createPackageJestConfig({ ...options, testEnvironment: 'node' });
}
function createJsdomJestConfig(options = {}) {
return createPackageJestConfig({ ...options, testEnvironment: 'jsdom' });
}
function workspacePath(...segments) {
return path.join(workspaceRoot, ...segments);
}
module.exports = {
createPackageJestConfig,
createNodeJestConfig,
createJsdomJestConfig,
workspacePath,
workspaceRoot,
};