UNPKG

3.25 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8var _expect = require('expect');
9
10var _jestSnapshot = require('jest-snapshot');
11
12/**
13 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
14 *
15 * This source code is licensed under the MIT license found in the
16 * LICENSE file in the root directory of this source tree.
17 */
18// Get suppressed errors form jest-matchers that weren't throw during
19// test execution and add them to the test result, potentially failing
20// a passing test.
21const addSuppressedErrors = result => {
22 const {suppressedErrors} = (0, _expect.getState)();
23 (0, _expect.setState)({
24 suppressedErrors: []
25 });
26
27 if (suppressedErrors.length) {
28 result.status = 'failed';
29 result.failedExpectations = suppressedErrors.map(error => ({
30 actual: '',
31 // passing error for custom test reporters
32 error,
33 expected: '',
34 matcherName: '',
35 message: error.message,
36 passed: false,
37 stack: error.stack
38 }));
39 }
40};
41
42const addAssertionErrors = result => {
43 const assertionErrors = (0, _expect.extractExpectedAssertionsErrors)();
44
45 if (assertionErrors.length) {
46 const jasmineErrors = assertionErrors.map(({actual, error, expected}) => ({
47 actual,
48 expected,
49 message: error.stack,
50 passed: false
51 }));
52 result.status = 'failed';
53 result.failedExpectations = result.failedExpectations.concat(jasmineErrors);
54 }
55};
56
57const patchJasmine = () => {
58 global.jasmine.Spec = (realSpec => {
59 class Spec extends realSpec {
60 constructor(attr) {
61 const resultCallback = attr.resultCallback;
62
63 attr.resultCallback = function (result) {
64 addSuppressedErrors(result);
65 addAssertionErrors(result);
66 resultCallback.call(attr, result);
67 };
68
69 const onStart = attr.onStart;
70
71 attr.onStart = context => {
72 (0, _expect.setState)({
73 currentTestName: context.getFullName()
74 });
75 onStart && onStart.call(attr, context);
76 };
77
78 super(attr);
79 }
80 }
81
82 return Spec;
83 })(global.jasmine.Spec);
84};
85
86var _default = async ({config, globalConfig, localRequire, testPath}) => {
87 // Jest tests snapshotSerializers in order preceding built-in serializers.
88 // Therefore, add in reverse because the last added is the first tested.
89 config.snapshotSerializers
90 .concat()
91 .reverse()
92 .forEach(path => {
93 (0, _jestSnapshot.addSerializer)(localRequire(path));
94 });
95 patchJasmine();
96 const {expand, updateSnapshot} = globalConfig;
97 const {prettierPath, snapshotFormat} = config;
98 const snapshotResolver = await (0, _jestSnapshot.buildSnapshotResolver)(
99 config,
100 localRequire
101 );
102 const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
103 const snapshotState = new _jestSnapshot.SnapshotState(snapshotPath, {
104 expand,
105 prettierPath,
106 snapshotFormat,
107 updateSnapshot
108 }); // @ts-expect-error: snapshotState is a jest extension of `expect`
109
110 (0, _expect.setState)({
111 snapshotState,
112 testPath
113 }); // Return it back to the outer scope (test runner outside the VM).
114
115 return snapshotState;
116};
117
118exports.default = _default;