-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
setup_jest_globals.ts
116 lines (106 loc) · 3.43 KB
/
setup_jest_globals.ts
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {Config, Global} from '@jest/types';
import {Plugin} from 'pretty-format';
import {extractExpectedAssertionsErrors, getState, setState} from 'expect';
import {
buildSnapshotResolver,
SnapshotState,
addSerializer,
} from 'jest-snapshot';
import JasmineSpec, {Attributes, SpecResult} from './jasmine/Spec';
import {Jasmine} from './types';
declare const global: Global.Global;
export type SetupOptions = {
config: Config.ProjectConfig;
globalConfig: Config.GlobalConfig;
localRequire: (moduleName: string) => Plugin;
testPath: Config.Path;
};
// Get suppressed errors form jest-matchers that weren't throw during
// test execution and add them to the test result, potentially failing
// a passing test.
const addSuppressedErrors = (result: SpecResult) => {
const {suppressedErrors} = getState();
setState({suppressedErrors: []});
if (suppressedErrors.length) {
result.status = 'failed';
result.failedExpectations = suppressedErrors.map(error => ({
actual: '',
// passing error for custom test reporters
error,
expected: '',
matcherName: '',
message: error.message,
passed: false,
stack: error.stack,
}));
}
};
const addAssertionErrors = (result: SpecResult) => {
const assertionErrors = extractExpectedAssertionsErrors();
if (assertionErrors.length) {
const jasmineErrors = assertionErrors.map(({actual, error, expected}) => ({
actual,
expected,
message: error.stack,
passed: false,
}));
result.status = 'failed';
result.failedExpectations = result.failedExpectations.concat(jasmineErrors);
}
};
const patchJasmine = () => {
(global.jasmine as Jasmine).Spec = (realSpec => {
class Spec extends realSpec {
constructor(attr: Attributes) {
const resultCallback = attr.resultCallback;
attr.resultCallback = function(result: SpecResult) {
addSuppressedErrors(result);
addAssertionErrors(result);
resultCallback.call(attr, result);
};
const onStart = attr.onStart;
attr.onStart = (context: JasmineSpec) => {
setState({currentTestName: context.getFullName()});
onStart && onStart.call(attr, context);
};
super(attr);
}
}
return Spec;
})((global.jasmine as Jasmine).Spec);
};
export default ({
config,
globalConfig,
localRequire,
testPath,
}: SetupOptions) => {
// 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));
});
patchJasmine();
const {expand, updateSnapshot} = globalConfig;
const snapshotResolver = buildSnapshotResolver(config);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
getBabelTraverse: () => require('@babel/traverse').default,
getPrettier: () =>
config.prettierPath ? require(config.prettierPath) : null,
updateSnapshot,
});
setState({snapshotState, testPath});
// Return it back to the outer scope (test runner outside the VM).
return snapshotState;
};