-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (110 loc) · 3.52 KB
/
index.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
'use strict';
const { EventEmitter } = require('events');
const path = require('path');
const fs = require('fs');
const tempy = require('tempy');
const { expandGlobs, getCommonParentDirectories } = require('../utils');
const OraBundleProgress = require('../bundle-progress/ora');
const createTestReporter = (testRunner) => {
const METRO_EVENTS = [
'bundle_build_started',
'bundle_transform_progressed',
'bundle_build_done',
'bundling_error',
'client_log',
];
const progress = new OraBundleProgress(testRunner);
const eventEmitter = new EventEmitter();
let running = false;
let pass = null;
// Metro's log reporter interface
const update = (event) => {
if (!METRO_EVENTS.includes(event.type)) {
return;
}
switch (event.type) {
case 'bundle_build_started':
pass = null;
progress.onStart(event);
break;
case 'bundle_build_done':
running = true;
progress.onSucceed(event);
break;
case 'bundling_error':
progress.onError(event);
break;
case 'bundle_build_failed': {
progress.onFail(event);
break;
}
case 'bundle_transform_progressed': {
progress.onProgress(event);
break;
}
case 'client_log':
handleClientLogEvent(event);
break;
default:
break;
}
};
const waitForTests = async () => {
if (pass !== null) {
return Promise.resolve();
}
await new Promise((resolve) => {
eventEmitter.addListener('done', resolve);
});
eventEmitter.removeAllListeners();
};
const handleClientLogEvent = ({ data }) => {
let testOutput = data[0];
if (testOutput === 'pass' || testOutput === 'fail') {
running = false;
pass = testOutput === 'pass';
if (!pass && data[1]) {
console.error(data[1]);
}
eventEmitter.emit('done');
return;
}
testOutput = testRunner.transformTestOutput(data);
console.log(testOutput);
};
return ({
update,
didPass: () => pass,
isRunning: () => running,
waitForTests,
});
};
const writeTestSuiteEntryModule = (cwd, testFilePaths, { preloadModulePath } = {}) => {
const tempFile = tempy.file({
name: 'test-suite.js',
});
const testSuite = [preloadModulePath, ...testFilePaths]
.filter(Boolean)
.map((file) => `require('${path.resolve(cwd, file)}');`)
.join('\n');
fs.writeFileSync(tempFile, testSuite);
return tempFile;
};
module.exports = ({
cwd,
runner,
require: preloadModulePath,
}, testFileGlobs = []) => {
const DEFAULT_TEST_FILE_GLOB = '**/test?(s)/**/?(*.)+(spec|test).js';
const testFilePaths = expandGlobs(cwd, testFileGlobs.length === 0 ? DEFAULT_TEST_FILE_GLOB : testFileGlobs);
const testSuiteEntryModulePath = writeTestSuiteEntryModule(cwd, testFilePaths, { preloadModulePath });
const testDirectoryPaths = getCommonParentDirectories(cwd, testFilePaths);
const createTestRunner = require(`./${runner}`);
const testRunner = createTestRunner();
const testReporter = createTestReporter(testRunner);
return {
reporter: testReporter,
resolveTestSuite: () => ({ testSuiteEntryModulePath, testDirectoryPaths }),
...testRunner,
};
};