-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
report.js
153 lines (137 loc) 路 4.57 KB
/
report.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
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
const childProcess = require('child_process');
const fs = require('fs');
const path = require('path');
const globby = require('globby');
const proxyquire = require('proxyquire');
const replaceString = require('replace-string');
const pkg = require('../../package.json');
const {normalizeGlobs} = require('../../lib/globs');
const providerManager = require('../../lib/provider-manager');
let _Api = null;
const createApi = options => {
if (!_Api) {
_Api = proxyquire('../../lib/api', {
'./fork': proxyquire('../../lib/fork', {
child_process: { // eslint-disable-line camelcase
...childProcess,
fork(filename, argv, options) {
return childProcess.fork(path.join(__dirname, 'report-worker.js'), argv, {
...options,
env: {
...options.env,
NODE_NO_WARNINGS: '1'
}
});
}
}
})
});
}
return new _Api(options);
};
exports.assert = (t, logFile, buffer) => {
let existing = null;
try {
existing = fs.readFileSync(logFile);
} catch (_) {}
if (existing === null || process.env.UPDATE_REPORTER_LOG) {
fs.writeFileSync(logFile, buffer);
existing = buffer;
}
const expected = existing.toString('utf8');
const actual = buffer.toString('utf8');
if (actual === expected) {
t.pass();
} else {
// Log the entire actual and expected values, so they can be diffed
// manually. TAP's diff output is really confusing in this situation.
console.dir({actual, expected});
t.fail('Output did not match expectation');
}
};
exports.sanitizers = {
cwd: str => replaceString(str, process.cwd(), '~'),
experimentalWarning: str => str.replace(/^\(node:\d+\) ExperimentalWarning.+\n/g, ''),
lineEndings: str => replaceString(str, '\r\n', '\n'),
posix: str => replaceString(str, '\\', '/'),
slow: str => str.replace(/(?<slow>slow.+?)\(\d+m?s\)/g, '$<slow> (000ms)'),
timeout: str => replaceString(str, 'Timeout._onTimeout', 'Timeout.setTimeout'),
traces: str => str.replace(/(\[...)?[^\s'[]+\s\((.+\.js:\d+:\d+)\)/g, '$1$2'),
version: str => replaceString(str, `v${pkg.version}`, 'v1.0.0-beta.5.1')
};
exports.projectDir = type => path.join(__dirname, '../fixture/report', type.toLowerCase());
const run = (type, reporter, match = []) => {
const projectDir = exports.projectDir(type);
const providers = [{
type: 'babel',
main: providerManager.babel(projectDir).main({config: true})
}];
const options = {
extensions: ['js'],
failFast: type === 'failFast' || type === 'failFast2',
failWithoutAssertions: false,
serial: type === 'failFast' || type === 'failFast2',
require: [],
cacheEnabled: true,
experiments: {},
match,
providers,
projectDir,
timeout: type.startsWith('timeout') ? '10s' : undefined,
concurrency: 1,
updateSnapshots: false,
snapshotDir: false,
chalkOptions: {level: 1}
};
let pattern = '*.js';
if (type === 'typescript') {
options.extensions.push('ts');
options.require = ['ts-node/register'];
pattern = '*.ts';
}
options.globs = normalizeGlobs({extensions: options.extensions, providers: []});
const api = createApi(options);
api.on('run', plan => reporter.startRun(plan));
const files = globby.sync(pattern, {
absolute: true,
brace: true,
case: false,
cwd: projectDir,
dot: false,
expandDirectories: false,
extglob: true,
followSymlinkedDirectories: true,
gitignore: false,
globstar: true,
matchBase: false,
onlyFiles: true,
stats: false,
unique: true
}).sort();
if (type !== 'watch') {
return api.run({files}).then(() => {
reporter.endRun();
});
}
// Mimick watch mode
return api.run({files, runtimeOptions: {clearLogOnNextRun: false, previousFailures: 0, runVector: 1}}).then(() => {
reporter.endRun();
return api.run({files, runtimeOptions: {clearLogOnNextRun: true, previousFailures: 2, runVector: 2}});
}).then(() => {
reporter.endRun();
return api.run({files, runtimeOptions: {clearLogOnNextRun: false, previousFailures: 0, runVector: 3}});
}).then(() => {
reporter.endRun();
});
};
exports.regular = reporter => run('regular', reporter);
exports.failFast = reporter => run('failFast', reporter);
exports.failFast2 = reporter => run('failFast2', reporter);
exports.only = reporter => run('only', reporter);
exports.timeoutInSingleFile = reporter => run('timeoutInSingleFile', reporter);
exports.timeoutInMultipleFiles = reporter => run('timeoutInMultipleFiles', reporter);
exports.timeoutWithMatch = reporter => run('timeoutWithMatch', reporter, ['*needle*']);
exports.watch = reporter => run('watch', reporter);
exports.typescript = reporter => run('typescript', reporter);
exports.edgeCases = reporter => run('edgeCases', reporter);