-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.staged.js
61 lines (56 loc) · 1.7 KB
/
jest.staged.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
const fs = require('fs').promises;
const path = require('path');
const { execSync } = require('child_process');
/** 处理jest只执行本次修改到的工具方法内的测试用例 */
async function start() {
/** 1. 获取git add 的文件的列表 */
const addFiles = execSync(`git diff --staged --diff-filter=ACMR --name-only`)
.toString()
.split('\n');
/** 2. 获取文件的绝对路径 */
const diffFileList = addFiles
.filter(Boolean)
.map((item) => path.join(__dirname, item));
/** 3. 获取src源码目录 */
const srcPath = path.join(__dirname, './src');
/** 4. 记录本次修改的函数方法 */
const diffFileMap = {};
diffFileList.forEach((filePath) => {
if (
filePath.includes(srcPath) &&
(filePath.endsWith('.ts') || filePath.endsWith('.tsx'))
) {
const relativePath = path.relative(srcPath, filePath);
const str = '\\';
if (relativePath.includes(str)) {
diffFileMap[relativePath.split(str)[0]] = true;
}
}
});
/** 5. 找到改动方法下面所有的单元测试文件 */
const list = (
await Promise.all(
Object.keys(diffFileMap).map(async (toolPath) => {
const testsDir = path.join(srcPath, toolPath, '__tests__');
try {
const files = await fs.readdir(testsDir);
return files.map((item) => path.join(testsDir, item));
} catch (error) {
return [];
}
}),
)
).flat();
/** 6. 执行单元测试脚本 */
if (list.length) {
try {
execSync(`npx jest --bail --findRelatedTests ${list.join(/ /)}`, {
cwd: __dirname,
stdio: 'inherit',
});
} catch (error) {
process.exit(1);
}
}
}
start();