UNPKG

@fgv/ts-utils-jest

Version:

Custom matchers for ts-utils result class

180 lines 7.2 kB
"use strict"; /* * Copyright (c) 2020 Erik Fortune * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockFileSystem = exports.ReadWriteSpies = void 0; const fs_1 = __importDefault(require("fs")); const path = __importStar(require("path")); /** * @public */ class ReadWriteSpies { constructor(read, write) { this.read = read; this.write = write; } clear() { this.read.mockClear(); this.write.mockClear(); } restore() { this.read.mockRestore(); this.write.mockRestore(); } } exports.ReadWriteSpies = ReadWriteSpies; /** * @public */ class MockFileSystem { constructor(configs, options) { this._extraWrites = []; this._config = new Map(); this._data = new Map(); this._options = options; this._realReadFileSync = fs_1.default.readFileSync; for (const config of configs) { const fullPath = path.resolve(config.path); this._config.set(fullPath, config); if (config.backingFile) { this.readMockFileSync(fullPath); } else if (config.payload) { this._data.set(fullPath, config.payload); } } } readMockFileSync(wanted) { var _a; const fullPathWanted = path.resolve(wanted); if (!this._data.has(fullPathWanted)) { const config = this._config.get(fullPathWanted); if ((config === null || config === void 0 ? void 0 : config.backingFile) === undefined) { if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.mockWriteOnly) !== true) { throw new Error(`Mock file not found: ${wanted}`); } const body = this._realReadFileSync(fullPathWanted).toString(); this._data.set(fullPathWanted, body); } else { const fullBackingPath = path.resolve(config.backingFile); const body = this._realReadFileSync(fullBackingPath).toString(); this._data.set(fullPathWanted, body); } } const payload = this._data.get(fullPathWanted); // not actually reproducible right now /* c8 ignore next 3 */ if (payload === undefined) { throw new Error(`Mock file ${wanted} payload is undefined.`); } return payload; } writeMockFileSync(wanted, body) { var _a; const fullPathWanted = path.resolve(wanted); const config = this._config.get(fullPathWanted); if (config === undefined) { if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.allowUnknownMockWrite) !== true) { throw new Error(`Mock path not found: ${wanted}`); } this._extraWrites.push(fullPathWanted); } else if (config.writable !== true) { throw new Error(`Mock permission denied: ${wanted}`); } this._data.set(fullPathWanted, body); } getExtraFilesWritten() { return Array.from(this._extraWrites); } tryGetPayload(want) { return this._data.get(path.resolve(want)); } reset() { const writable = Array.from(this._config.values()).filter((c) => c.writable === true); for (const config of writable) { this._data.delete(path.resolve(config.path)); if (config.backingFile) { this.readMockFileSync(path.resolve(config.path)); } else if (config.payload) { this._data.set(path.resolve(config.path), config.payload); } } for (const path of this._extraWrites) { this._data.delete(path); } this._extraWrites.splice(0, this._extraWrites.length); } startSpies() { return new ReadWriteSpies(jest.spyOn(fs_1.default, 'readFileSync').mockImplementation((wanted) => { if (typeof wanted !== 'string') { throw new Error('readFileSync mock supports only string parameters'); } return this.readMockFileSync(wanted); }), jest.spyOn(fs_1.default, 'writeFileSync').mockImplementation((wanted, payload) => { if (typeof wanted !== 'string' || typeof payload !== 'string') { throw new Error('writeFileSync mock supports only string parameters'); } return this.writeMockFileSync(wanted, payload); })); } } exports.MockFileSystem = MockFileSystem; //# sourceMappingURL=fsHelpers.js.map