/
exec.js
129 lines (108 loc) 路 3.07 KB
/
exec.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
const path = require('path');
const v8 = require('v8');
const test = require('@ava/test');
const execa = require('execa');
const defaultsDeep = require('lodash/defaultsDeep');
const cliPath = path.resolve(__dirname, '../../cli.js');
const ttySimulator = path.join(__dirname, './simulate-tty.js');
const serialization = process.versions.node >= '12.16.0' ? 'advanced' : 'json';
const normalizePath = (root, file) => path.posix.normalize(path.relative(root, file));
const compareStatObjects = (a, b) => {
if (a.file < b.file) {
return -1;
}
if (a.file > b.file) {
return 1;
}
if (a.title < b.title) {
return -1;
}
return 1;
};
exports.fixture = async (args, options = {}) => {
const cwd = path.join(path.dirname(test.meta.file), 'fixtures');
const running = execa.node(cliPath, args, defaultsDeep({
env: {
AVA_EMIT_RUN_STATUS_OVER_IPC: 'I\'ll find a payphone baby / Take some time to talk to you'
},
cwd,
serialization,
nodeOptions: ['--require', ttySimulator]
}, options));
// Besides buffering stderr, if this environment variable is set, also pipe
// to stderr. This can be useful when debugging the tests.
if (process.env.DEBUG_TEST_AVA) {
running.stderr.pipe(process.stderr);
}
const errors = new WeakMap();
const stats = {
failed: [],
failedHooks: [],
passed: [],
skipped: [],
uncaughtExceptions: [],
unsavedSnapshots: [],
getError(statObject) {
return errors.get(statObject);
}
};
running.on('message', statusEvent => {
if (serialization === 'json') {
statusEvent = v8.deserialize(Uint8Array.from(statusEvent));
}
switch (statusEvent.type) {
case 'hook-failed': {
const {title, testFile} = statusEvent;
const statObject = {title, file: normalizePath(cwd, testFile)};
errors.set(statObject, statusEvent.err);
stats.failedHooks.push(statObject);
break;
}
case 'selected-test': {
if (statusEvent.skip) {
const {title, testFile} = statusEvent;
stats.skipped.push({title, file: normalizePath(cwd, testFile)});
}
break;
}
case 'snapshot-error': {
const {testFile} = statusEvent;
stats.unsavedSnapshots.push({file: normalizePath(cwd, testFile)});
break;
}
case 'test-passed': {
const {title, testFile} = statusEvent;
stats.passed.push({title, file: normalizePath(cwd, testFile)});
break;
}
case 'test-failed': {
const {title, testFile} = statusEvent;
const statObject = {title, file: normalizePath(cwd, testFile)};
errors.set(statObject, statusEvent.err);
stats.failed.push(statObject);
break;
}
case 'uncaught-exception': {
const {message, name, stack} = statusEvent.err;
stats.uncaughtExceptions.push({message, name, stack});
break;
}
default:
break;
}
});
try {
return {
stats,
...await running
};
} catch (error) {
throw Object.assign(error, {stats});
} finally {
stats.failed.sort(compareStatObjects);
stats.failedHooks.sort(compareStatObjects);
stats.passed.sort(compareStatObjects);
stats.skipped.sort(compareStatObjects);
stats.unsavedSnapshots.sort(compareStatObjects);
}
};