forked from belgattitude/nextjs-monorepo-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.config.js
57 lines (53 loc) · 1.69 KB
/
jest.config.js
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
// @ts-check
import { getTsconfig } from 'get-tsconfig';
import { pathsToModuleNameMapper } from 'ts-jest';
const tsConfigFile = new URL('./tsconfig.jest.json', import.meta.url).pathname;
const tsConfigPathsFile = new URL('./tsconfig.json', import.meta.url).pathname;
/**
* Transform the tsconfig paths into jest compatible one (support extends)
* @param {string} tsConfigFile
*/
const getTsConfigBasePaths = (tsConfigFile) => {
const parsedTsConfig = getTsconfig(tsConfigFile);
if (parsedTsConfig === null) {
throw new Error(`Cannot find tsconfig file: ${tsConfigFile}`);
}
const tsPaths = parsedTsConfig.config.compilerOptions?.paths;
return tsPaths
? pathsToModuleNameMapper(tsPaths, {
prefix: '<rootDir>/',
})
: {};
};
/** @type {import('ts-jest').JestConfigWithTsJest} */
const config = {
displayName: `nextjs-app:unit`,
testEnvironment: 'jsdom',
extensionsToTreatAsEsm: ['.ts', '.tsx'],
verbose: true,
rootDir: './src',
setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],
testMatch: ['<rootDir>/**/*.{spec,test}.{js,jsx,ts,tsx}'],
transform: {
'^.+\\.m?[tj]sx?$': [
'ts-jest',
{
tsconfig: tsConfigFile,
},
],
},
moduleNameMapper: {
'^.+\\.(svg)$': '<rootDir>/../config/tests/ReactSvgrMock.tsx',
'.+\\.(css|styl|less|sass|scss)$': 'jest-css-modules-transform',
...getTsConfigBasePaths(tsConfigPathsFile),
},
// false by default, overrides in cli, ie: yarn test:unit --collect-coverage=true
collectCoverage: false,
coverageDirectory: '<rootDir>/../coverage',
collectCoverageFrom: [
'<rootDir>/**/*.{ts,tsx,js,jsx}',
'!**/*.test.{js,ts}',
'!**/__mock__/*',
],
};
export default config;