-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.config.js
98 lines (87 loc) · 3.3 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
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
const { pathsToModuleNameMapper } = require('ts-jest');
// which contains the path mapping (ie the `compilerOptions.paths` option):
const { compilerOptions } = require('./tsconfig');
const fs = require('fs');
const path = require('path');
/**
* @type {import('@jest/types').Config.ProjectConfig}
* Config used between test projects.
*/
const baseConfig = {
// Automatically clear mock calls and instances between every test
clearMocks: true,
coveragePathIgnorePatterns: ['index.ts', '/node_modules/'],
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
testEnvironment: 'node',
rootDir: '.',
// A map from regular expressions to paths to transformers
transform: {
'\\.(ts)$': 'ts-jest'
},
transformIgnorePatterns: ['node_modules'],
moduleDirectories: ['node_modules', 'src'],
roots: ['<rootDir>'],
modulePaths: [compilerOptions.baseUrl], // <-- This will be set to 'baseUrl' value
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */),
modulePathIgnorePatterns: ['<rootDir>/dist/']
};
/**
* For easier debugging, create a test label for each test file.
* @param {string} baseDir
* @returns
*/
function createTestLabels(baseDir) {
// Read the contents of the directory synchronously
const filePaths = fs.readdirSync(baseDir).map((f) => path.join(baseDir, f));
const results = [];
while (filePaths.length > 0) {
const filePath = filePaths.pop();
const fileStat = fs.statSync(filePath); // Get the file's stats
if (fileStat.isDirectory()) {
const subFiles = fs.readdirSync(filePath);
filePaths.push(...subFiles.map((f) => path.join(filePath, f)));
continue;
}
// Check if the file is test file
let type;
if (filePath.endsWith('.spec.ts')) type = 'spec';
else if (filePath.endsWith('.test.ts')) type = 'test';
else continue;
const fileName = path.basename(filePath);
const label = fileName.slice(0, -8);
results.push({ label, type });
}
return results.map((r) => ({
...baseConfig,
displayName: r.label,
testMatch: [`**/${r.label}.${r.type}.ts`],
setupFilesAfterEnv: r.type === 'spec' ? ['<rootDir>/src/setupTest.ts'] : undefined
}));
}
const unitTestProject = {
...baseConfig,
displayName: 'unit',
testMatch: ['**/*.test.ts']
};
const integrationTestProject = {
...baseConfig,
displayName: 'integration',
testMatch: ['**/*.spec.ts'],
setupFilesAfterEnv: ['<rootDir>/src/setupTest.ts']
};
/** @type {import('jest').Config} */
module.exports = {
testTimeout: 120 * 1000,
passWithNoTests: true,
reporters: [['github-actions', { silent: false }], 'default'],
// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,
// The directory where Jest should output its coverage files
coverageDirectory: 'coverage',
detectOpenHandles: true,
// Wait at most 5 seconds util all promises are resolved before exiting the test
openHandlesTimeout: 5000,
// Exit the test suite immediately upon the first failing test
bail: true,
projects: [unitTestProject, integrationTestProject, ...createTestLabels(compilerOptions.sourceRoot)]
};