-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjest.js
140 lines (125 loc) · 4.23 KB
/
jest.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright © 1998 - 2021 Tencent. All Rights Reserved.
* @author enoyao
*/
const vm = require("vm");
const path = require("path");
const fs = require("fs");
const testPath = process.argv.slice(2)[0];
const code = fs.readFileSync(path.join(process.cwd(), testPath)).toString();
const dispatch = event => {
const { fn, type, name, pass } = event;
switch (type) {
case "ADD_TEST":
const { testBlock } = global["STATE_SYMBOL"];
testBlock.push({ fn, name });
break;
case "BEFORE_EACH":
const { beforeEachBlock } = global["STATE_SYMBOL"];
beforeEachBlock.push(fn);
break;
case "BEFORE_ALL":
const { beforeAllBlock } = global["STATE_SYMBOL"];
beforeAllBlock.push(fn);
break;
case "AFTER_EACH":
const { afterEachBlock } = global["STATE_SYMBOL"];
afterEachBlock.push(fn);
break;
case "AFTER_ALL":
const { afterAllBlock } = global["STATE_SYMBOL"];
afterAllBlock.push(fn);
break;
case "COLLECT_REPORT":
const { reports } = global["STATE_SYMBOL"];
reports.push({ name, pass });
break;
}
};
const createState = () => {
global["STATE_SYMBOL"] = {
testBlock: [],
beforeEachBlock: [],
beforeAllBlock: [],
afterEachBlock: [],
afterAllBlock: [],
reports: []
};
};
createState();
const jest = {
fn(impl = () => { }) {
const mockFn = (...args) => {
mockFn.mock.calls.push(args);
return impl(...args);
};
mockFn.originImpl = impl;
mockFn.mock = { calls: [] };
return mockFn;
},
mock(mockPath, mockExports = {}) {
const path = require.resolve(mockPath, { paths: ["."] });
require.cache[path] = {
id: path,
filename: path,
loaded: true,
exports: mockExports,
};
}
};
const log = (color, text) => console.log(color, text);
const expect = (actual) => ({
toBe(expected) {
if (actual !== expected) {
throw new Error(`${actual} is not equal to ${expected}`);
}
},
toEqual(expected) {
try {
assert.deepStrictEqual(actual, expected);
} catch {
throw new Error(`${JSON.stringify(actual)} is not equal to ${JSON.stringify(expected)}`);
}
},
});
const context = {
console: console.Console({ stdout: process.stdout, stderr: process.stderr }),
jest,
expect,
require,
afterAll: (fn) => dispatch({ type: "AFTER_ALL", fn }),
afterEach: (fn) => dispatch({ type: "AFTER_EACH", fn }),
beforeAll: (fn) => dispatch({ type: "BEFORE_ALL", fn }),
beforeEach: (fn) => dispatch({ type: "BEFORE_EACH", fn }),
test: (name, fn) => dispatch({ type: "ADD_TEST", fn, name }),
};
(async () => {
const start = new Date();
vm.createContext(context);
vm.runInContext(code, context);
const { testBlock, beforeEachBlock, beforeAllBlock, afterEachBlock, afterAllBlock } = global["STATE_SYMBOL"];
await new Promise((done) => {
beforeAllBlock.forEach(async (beforeAll) => await beforeAll());
testBlock.forEach(async (item) => {
const { fn, name } = item;
try {
beforeEachBlock.forEach(async (beforeEach) => await beforeEach());
await fn.apply(this);
dispatch({ type: "COLLECT_REPORT", name, pass: 1 });
afterEachBlock.forEach(async (afterEach) => await afterEach());
log("\x1b[32m%s\x1b[0m", `√ ${name} passed`);
} catch (error) {
dispatch({ type: "COLLECT_REPORT", name, pass: 0 });
console.error(error);
log("\x1b[32m%s\x1b[0m", `× ${name} error`);
}
});
afterAllBlock.forEach(async (afterAll) => await afterAll());
done();
})
const end = new Date();
log("\x1b[32m%s\x1b[0m", `Time: ${end - start} ms`);
const { reports } = global["STATE_SYMBOL"];
const pass = reports.reduce((pre, next) => pre.pass + next.pass);
log("\x1b[32m%s\x1b[0m", `All Tests: ${pass}/${reports.length} passed`);
})();