Repository navigation
Expand file tree
/
Copy pathrunTestSuite.js
More file actions
132 lines (111 loc) · 3.39 KB
/
Copy pathrunTestSuite.js
File metadata and controls
132 lines (111 loc) · 3.39 KB
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
/* @flow */
import colors from 'colors/safe';
import {format} from 'util';
import {noErrors} from '../flowResult';
import {TestStep, TestStepFirstStage} from './TestStep';
import {newEnv} from './stepEnv';
import type Builder from './builder';
import type Suite from './Suite';
import type {StepResult} from './TestStep';
export type TestResult = {
name: ?string,
stepResults: Array<StepResult>,
};
export default async function(
bin: string,
builder: Builder,
suiteName: string,
testSuite: Suite,
reportStatus: (status: string, details: string) => void,
): Promise<Array<TestResult>> {
let results = [];
let testNum = 1;
const emptyTestStep = new TestStepFirstStage();
const tests = testSuite.tests(emptyTestStep);
let stepsPassed = 0;
let stepsFailed = 0;
let testsRun = 0;
const totalTests = tests.length;
function printStatus(status: 'RUN' | 'PASS' | 'FAIL'): void {
let statusText = colors.grey.bold("[ ] RUN")+": ";
let newline = "";
if (status === 'PASS') {
statusText = colors.green.bold("[✓] PASS")+":"
newline = "\n";
} else if (status === 'FAIL') {
statusText = colors.red.bold("[✗] FAIL")+":"
newline = "\n";
}
reportStatus(
statusText,
format(
colors.grey("(%d/%d tests. %d steps passed. %d steps failed)"),
testsRun,
totalTests,
stepsPassed,
stepsFailed,
),
);
}
for (const test of tests) {
// flowErrors contains the current flow errors. If a step doesn't care
// about flow errors, we won't check for the current flow errors and
// flowErrors will contain null
let flowErrors = noErrors;
let testBuilder = await builder.createFreshTest(
bin,
suiteName,
testNum,
test.flowConfigFilename,
);
let stepResults = [];
const steps = [].concat(testSuite.getBeforeEach(emptyTestStep), test.steps);
for (const step of steps) {
if (!(step instanceof TestStep)) {
throw new Error(format("Expected a TestStep, instead got", step));
}
printStatus('RUN');
if (step.needsFlowServer()) {
// No-op if one is already running
await testBuilder.startFlowServer();
}
// If one of the assertions will check flow errors, then we need to make
// sure we have the current errors BEFORE doing any actions
if (flowErrors == null && step.needsFlowCheck()) {
flowErrors = await testBuilder.getFlowErrors();
}
let { envRead, envWrite } = newEnv(flowErrors || noErrors);
await step.performActions(testBuilder, envWrite);
let oldErrors = flowErrors;
// If any of the actions affect flow and if the assertions check flow
// errors, then get the new flow errors
if (envRead.shouldRunFlow() && step.needsFlowCheck()) {
flowErrors = await testBuilder.getFlowErrors();
envWrite.setNewErrors(flowErrors);
} else {
flowErrors = null;
}
let result = step.checkAssertions(envRead);
if (result.passed) {
stepsPassed++;
} else {
stepsFailed++;
}
stepResults.push(result);
}
// No-op if nothing is running
await testBuilder.stopFlowServer();
testsRun++;
results.push({
name: test.name,
stepResults,
});
testNum++;
}
if (stepsFailed === 0) {
printStatus('PASS');
} else {
printStatus('FAIL');
}
return results;
}