UNPKG

jest-jasmine2

Version:
77 lines (75 loc) 2.33 kB
import { jestExpect } from "@jest/expect"; import { SnapshotState, addSerializer, buildSnapshotResolver } from "jest-snapshot"; //#region src/setup_jest_globals.ts const addSuppressedErrors = (result) => { const { suppressedErrors } = jestExpect.getState(); jestExpect.setState({ suppressedErrors: [] }); if (suppressedErrors.length > 0) { result.status = "failed"; result.failedExpectations = suppressedErrors.map((error) => ({ actual: "", error, expected: "", matcherName: "", message: error.message, passed: false, stack: error.stack })); } }; const addAssertionErrors = (result) => { const assertionErrors = 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 = () => { 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) => { jestExpect.setState({ currentTestName: context.getFullName() }); onStart?.call(attr, context); }; super(attr); } } return Spec; })(globalThis.jasmine.Spec); }; async function setupJestGlobals({ config, globalConfig, localRequire, testPath }) { for (let i = config.snapshotSerializers.length - 1; i >= 0; i--) addSerializer(localRequire(config.snapshotSerializers[i])); patchJasmine(); const { expand, updateSnapshot } = globalConfig; const { prettierPath, rootDir, snapshotFormat } = config; const snapshotResolver = await buildSnapshotResolver(config, localRequire); const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath); const snapshotState = new SnapshotState(snapshotPath, { expand, prettierPath, rootDir, snapshotFormat, updateSnapshot }); jestExpect.setState({ snapshotState, testPath }); return snapshotState; } //#endregion export { setupJestGlobals as default };