-
Notifications
You must be signed in to change notification settings - Fork 1
/
jest.config.ts
44 lines (40 loc) · 1.58 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
import type { Config } from '@jest/types';
import { pathsToModuleNameMapper } from 'ts-jest';
import tsconfigJson from './tsconfig.json';
function manageKey(key: string): string {
return key.includes('(.*)') ? key.slice(0, -1) + '\\.js$' : key;
}
function manageMapper(mapper: Record<string, string>): Record<string, string> {
const newMapper: Record<string, string> = {};
for (const key in mapper) {
newMapper[manageKey(key)] = mapper[key];
}
newMapper['^(.*)\\.js$'] = '$1';
return newMapper;
}
function getNodeModulesWithESMPatternsToTransform(modules: string[]): Record<string, string> {
return modules.reduce((acc, module) => ({
...acc,
[`node_modules/${module}/.+\\.(j|t)sx?$`]: 'babel-jest'
}), {});
};
function getNodeModulesWithESMTransformIgnorePattern(modules: string[]): string {
return `<rootDir>/node_modules/${modules.map(module => `(?!${module}/.*)`).join('')}`;
}
const nodeModulesWithESM = ['chalk'];
const config: Config.InitialOptions = {
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
verbose: true,
transform: {
'^.+\\.tsx?$': ['ts-jest', {
tsconfig: './tsconfig.json',
useEsm: true
}],
...getNodeModulesWithESMPatternsToTransform(nodeModulesWithESM)
},
coverageProvider: 'v8',
moduleNameMapper: manageMapper(pathsToModuleNameMapper(tsconfigJson.compilerOptions.paths, { prefix: '<rootDir>/' }) as Record<string, string>),
transformIgnorePatterns: [ getNodeModulesWithESMTransformIgnorePattern(nodeModulesWithESM)]
};
export default config;