-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest.config.ts
80 lines (72 loc) · 2.22 KB
/
jest.config.ts
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
// @ts-check
/* eslint-env node */
import { resolve } from 'path';
import { JestConfigWithTsJest, pathsToModuleNameMapper } from 'ts-jest';
import findUp from 'find-up';
import { readFileSync } from 'fs';
import { parse } from 'json5';
import { dirname, join } from 'path';
export const createJestConfig = (config?: {
rootDir?: string;
tsconfig?: string;
}): JestConfigWithTsJest => {
const rootDir = config?.rootDir ?? __dirname;
const tsconfigPath =
config?.tsconfig ??
findUp.sync(['tsconfig.spec.json', 'tsconfig.json'], {
cwd: rootDir,
})!;
const tsconfig = parse(readFileSync(tsconfigPath).toString());
const pathsConfig = getPathAliases(rootDir, tsconfig);
return {
// Automatically clear mock calls and instances between every test
clearMocks: true,
verbose: true,
preset: 'ts-jest',
transform: {
'^.+.tsx?$': [
'ts-jest',
{
tsconfig: 'tsconfig.spec.json',
},
],
},
// rootDir: module.parent.path,
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/.*/dist/'],
moduleNameMapper: pathsToModuleNameMapper(pathsConfig),
resolver: 'ts-jest-resolver',
coverageDirectory: join(__dirname, 'dist', 'coverage'),
collectCoverage: false, // disabled, as it slows down running test with vscode-jest extension
} satisfies JestConfigWithTsJest;
};
/**
* An object with Jest options.
* @type {import('@jest/types').Config.InitialOptions}
*/
export default createJestConfig();
function getPathAliases(rootDir: string, tsconfig: any) {
const { compilerOptions } = tsconfig;
const parentAliases: any = tsconfig.extends
? getPathAliases(
dirname(tsconfig.extends),
parse(readFileSync(tsconfig.extends).toString()),
)
: {};
const aliases = Object.entries(compilerOptions?.paths ?? {})
.map(([alias, paths]) => {
if (alias === '*') {
alias = '<rootDir>';
}
return [alias, paths] as [string, string[]];
})
.reduce((pathsConfig: any, [alias, paths]: [any, any]) => {
pathsConfig[alias] = ([] as string[])
.concat(paths)
.map((p) => resolve(rootDir, p));
return pathsConfig;
}, {});
return {
...parentAliases,
...aliases,
};
}