UNPKG

mocha-chai-jest-snapshot

Version:
76 lines 4.27 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSerializers = exports.addSerializer = exports.jestSnapshotPlugin = exports.initSnapshotManager = exports._getSnapshotManager = exports._setReporterAttached = void 0; const node_path_1 = __importDefault(require("node:path")); const node_util_1 = __importDefault(require("node:util")); const jest_snapshot_1 = require("jest-snapshot"); Object.defineProperty(exports, "addSerializer", { enumerable: true, get: function () { return jest_snapshot_1.addSerializer; } }); Object.defineProperty(exports, "getSerializers", { enumerable: true, get: function () { return jest_snapshot_1.getSerializers; } }); const find_package_json_1 = __importDefault(require("find-package-json")); const constants_1 = require("./constants"); const jest_config_helper_1 = require("./utils/jest-config-helper"); const helper_1 = require("./helper"); const manager_1 = __importDefault(require("./manager")); let _manager; let _reporterAttached = false; function _setReporterAttached() { _reporterAttached = true; } exports._setReporterAttached = _setReporterAttached; function _getSnapshotManager() { return _manager; } exports._getSnapshotManager = _getSnapshotManager; const jestSnapshotPlugin = (optionalConfig) => { var _a, _b; const packageJson = (0, find_package_json_1.default)(process.cwd()).next(); const rootDir = packageJson.done ? process.cwd() : node_path_1.default.dirname(packageJson.filename); const config = (0, jest_config_helper_1.replaceRootDirInObject)(rootDir, Object.assign(Object.assign(Object.assign({ rootDir }, (_a = packageJson.value) === null || _a === void 0 ? void 0 : _a.jest), (0, helper_1.readJestConfig)(rootDir)), optionalConfig)); // Jest tests snapshotSerializers in order preceding built-in serializers. // Therefore, add in reverse because the last added is the first tested. (_b = config.snapshotSerializers) === null || _b === void 0 ? void 0 : _b.concat().reverse().forEach((path) => { (0, jest_snapshot_1.addSerializer)(require.main.require(path)); }); _manager = new manager_1.default({ rootDir, }); return function (chai, utils) { before(() => __awaiter(this, void 0, void 0, function* () { _manager.snapshotResolver = yield (0, jest_snapshot_1.buildSnapshotResolver)(Object.assign({ transform: [] }, config)); })); beforeEach(function () { if (this.currentTest) _manager.setContext(this.currentTest, chai); }); after(function () { _manager.saveSnap(); if (!_reporterAttached) _manager.report(); }); for (const key of ["matchSnapshot", "toMatchSnapshot"]) { utils.addMethod(chai.Assertion.prototype, key, function (message) { const expected = utils.flag(this, "object"); _manager.assert(expected, message); }); } chai.expect.addSnapshotSerializer = jest_snapshot_1.addSerializer; }; }; exports.jestSnapshotPlugin = jestSnapshotPlugin; const initSnapshotManager = node_util_1.default.deprecate(jestSnapshotPlugin(), "chai.use(initSnapshotManager) was deprecated. use chai.use(jestSnapshotPlugin()) instead.", constants_1.DEPRECATED_CODE_INIT_SNAPSHOT_MANAGER); exports.initSnapshotManager = initSnapshotManager; //# sourceMappingURL=index.js.map