jest-jasmine2
Version:
137 lines (133 loc) • 3.9 kB
JavaScript
/*!
* /**
* * Copyright (c) Meta Platforms, Inc. and affiliates.
* *
* * This source code is licensed under the MIT license found in the
* * LICENSE file in the root directory of this source tree.
* * /
*/
/******/ (() => { // webpackBootstrap
/******/ "use strict";
var __webpack_exports__ = {};
// This entry needs to be wrapped in an IIFE because it uses a non-standard name for the exports (exports).
(() => {
var exports = __webpack_exports__;
Object.defineProperty(exports, "__esModule", ({
value: true
}));
exports["default"] = setupJestGlobals;
var _expect = require("@jest/expect");
var _jestSnapshot = require("jest-snapshot");
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// 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 => {
const {
suppressedErrors
} = _expect.jestExpect.getState();
_expect.jestExpect.setState({
suppressedErrors: []
});
if (suppressedErrors.length > 0) {
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 => {
const assertionErrors = _expect.jestExpect.extractExpectedAssertionsErrors();
if (assertionErrors.length > 0) {
const jasmineErrors = assertionErrors.map(({
actual,
error,
expected
}) => ({
actual,
expected,
message: error.stack,
passed: false
}));
result.status = 'failed';
result.failedExpectations = [...result.failedExpectations, ...jasmineErrors];
}
};
const patchJasmine = () => {
// @ts-expect-error: jasmine doesn't exist on globalThis
globalThis.jasmine.Spec = (realSpec => {
class Spec extends realSpec {
constructor(attr) {
const resultCallback = attr.resultCallback;
attr.resultCallback = function (result) {
addSuppressedErrors(result);
addAssertionErrors(result);
resultCallback.call(attr, result);
};
const onStart = attr.onStart;
attr.onStart = context => {
_expect.jestExpect.setState({
currentTestName: context.getFullName()
});
onStart?.call(attr, context);
};
super(attr);
}
}
return Spec;
// @ts-expect-error: jasmine doesn't exist on globalThis
})(globalThis.jasmine.Spec);
};
async function setupJestGlobals({
config,
globalConfig,
localRequire,
testPath
}) {
// Jest tests snapshotSerializers in order preceding built-in serializers.
// Therefore, add in reverse because the last added is the first tested.
for (let i = config.snapshotSerializers.length - 1; i >= 0; i--) {
(0, _jestSnapshot.addSerializer)(localRequire(config.snapshotSerializers[i]));
}
patchJasmine();
const {
expand,
updateSnapshot
} = globalConfig;
const {
prettierPath,
rootDir,
snapshotFormat
} = config;
const snapshotResolver = await (0, _jestSnapshot.buildSnapshotResolver)(config, localRequire);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new _jestSnapshot.SnapshotState(snapshotPath, {
expand,
prettierPath,
rootDir,
snapshotFormat,
updateSnapshot
});
_expect.jestExpect.setState({
snapshotState,
testPath
});
// Return it back to the outer scope (test runner outside the VM).
return snapshotState;
}
})();
module.exports = __webpack_exports__;
/******/ })()
;