forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest-adapter-init.js
More file actions
191 lines (171 loc) · 4.73 KB
/
Copy pathjest-adapter-init.js
File metadata and controls
191 lines (171 loc) · 4.73 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
import type {TestResult, Status} from 'types/TestResult';
import type {GlobalConfig, Path, ProjectConfig} from 'types/Config';
import type {Event, TestEntry} from '../../types';
import {
extractExpectedAssertionsErrors,
getState,
setState,
} from 'jest-matchers';
import {formatResultsErrors} from 'jest-message-util';
import {SnapshotState, addSerializer} from 'jest-snapshot';
import {addEventHandler, ROOT_DESCRIBE_BLOCK_NAME} from '../state';
import {getTestID} from '../utils';
import run from '../run';
import globals from '../index';
const initialize = ({
config,
globalConfig,
localRequire,
testPath,
}: {
config: ProjectConfig,
globalConfig: GlobalConfig,
localRequire: Path => any,
testPath: Path,
}) => {
Object.assign(global, globals);
global.xit = global.it.skip;
global.xtest = global.it.skip;
global.xdescribe = global.describe.skip;
global.fit = global.it.only;
global.fdescribe = global.describe.only;
addEventHandler(eventHandler);
// Jest tests snapshotSerializers in order preceding built-in serializers.
// Therefore, add in reverse because the last added is the first tested.
config.snapshotSerializers.concat().reverse().forEach(path => {
addSerializer(localRequire(path));
});
const {expand, updateSnapshot} = globalConfig;
const snapshotState = new SnapshotState(testPath, {expand, updateSnapshot});
setState({snapshotState, testPath});
// Return it back to the outer scope (test runner outside the VM).
return {globals, snapshotState};
};
const runAndTransformResultsToJestFormat = async ({
config,
globalConfig,
testPath,
}: {
config: ProjectConfig,
globalConfig: GlobalConfig,
testPath: string,
}): Promise<TestResult> => {
const result = await run();
let numFailingTests = 0;
let numPassingTests = 0;
let numPendingTests = 0;
for (const testResult of result) {
switch (testResult.status) {
case 'fail':
numFailingTests += 1;
break;
case 'pass':
numPassingTests += 1;
break;
case 'skip':
numPendingTests += 1;
break;
}
}
const assertionResults = result.map(testResult => {
let status: Status;
switch (testResult.status) {
case 'fail':
status = 'failed';
break;
case 'pass':
status = 'passed';
break;
case 'skip':
status = 'pending';
break;
}
const ancestorTitles = testResult.testPath.filter(
name => name !== ROOT_DESCRIBE_BLOCK_NAME,
);
const title = ancestorTitles.pop();
// $FlowFixMe Types are slightly incompatible and need to be refactored
return {
ancestorTitles,
duration: testResult.duration,
failureMessages: testResult.errors,
fullName: ancestorTitles.concat(title).join(' '),
numPassingAsserts: 0,
status,
title: testResult.testPath[testResult.testPath.length - 1],
};
});
const failureMessage = formatResultsErrors(
assertionResults,
config,
globalConfig,
testPath,
);
return {
console: null,
failureMessage,
numFailingTests,
numPassingTests,
numPendingTests,
perfStats: {
// populated outside
end: 0,
start: 0,
},
skipped: false,
snapshot: {
added: 0,
fileDeleted: false,
matched: 0,
unchecked: 0,
unmatched: 0,
updated: 0,
},
sourceMaps: {},
testFilePath: testPath,
testResults: assertionResults,
};
};
const eventHandler = (event: Event) => {
switch (event.name) {
case 'test_start': {
setState({currentTestName: getTestID(event.test)});
break;
}
case 'test_success':
case 'test_failure': {
_addSuppressedErrors(event.test);
_addExpectedAssertionErrors(event.test);
break;
}
}
};
const _addExpectedAssertionErrors = (test: TestEntry) => {
const errors = extractExpectedAssertionsErrors();
errors.length && (test.status = 'fail');
test.errors = test.errors.concat(errors);
};
// Get suppressed errors from ``jest-matchers`` that weren't throw during
// test execution and add them to the test result, potentially failing
// a passing test.
const _addSuppressedErrors = (test: TestEntry) => {
const {suppressedErrors} = getState();
setState({suppressedErrors: []});
if (suppressedErrors.length) {
test.status = 'fail';
test.errors = test.errors.concat(suppressedErrors);
}
};
module.exports = {
initialize,
runAndTransformResultsToJestFormat,
};