UNPKG

mockzilla

Version:

A mocking toolkit leveraging the power of TypeScript to enhance your jest experience.

110 lines (109 loc) 3.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deepMock = void 0; const node_1 = require("./node"); const error_1 = require("./error"); const createTimes = (target, expectation) => (callCount) => { while (--callCount > 0) target.rootNode.addExpectation(target.path, expectation); }; const notAllowed = (name) => () => { throw new error_1.MockzillaError(`Error: It's not allowed to use ${name} on a mock builder.`); }; const deepMockHandler = { get(target, prop) { if (prop === "expect") { const expectation = { stack: (0, error_1.getCleanStack)(), }; target.rootNode.addExpectation(target.path, expectation); const expect = Object.assign((...args) => { expectation.args = args; return expect; }, { andResolve: (result) => { expectation.returns = Promise.resolve(result); return expect; }, andReject: (error) => { expectation.returns = Promise.reject(error); return expect; }, andReturn: (result) => { expectation.returns = result; return expect; }, andThrow: (error) => { expectation.throws = error; return expect; }, times: createTimes(target, expectation), }); return expect; } if (prop === "spy") { return (spy) => { const expectation = { spy, stack: (0, error_1.getCleanStack)() }; target.rootNode.addExpectation(target.path, expectation); return { times: createTimes(target, expectation) }; }; } if (prop === "getMockCalls") { return () => target.rootNode.getCalls(target.path); } if (prop === "mock") { return (value) => target.rootNode.setValue(target.path, value); } if (prop === "mockAllow") { return () => target.rootNode.allow(target.path); } if (prop === "mockAllowMethod") { return () => target.rootNode.addExpectation(target.path, null); } if (prop === "mockPath") { return target.path; } const key = prop.toString(); let child = target.children[key]; if (!child) { const path = target.path ? `${target.path}.${key}` : key; child = new Proxy({ path, children: {}, rootNode: target.rootNode }, deepMockHandler); target.children[key] = child; } return child; }, // not to be called apply: notAllowed("apply"), ownKeys: notAllowed("ownKeys"), has: notAllowed("has"), getPrototypeOf: notAllowed("getPrototypeOf"), setPrototypeOf: notAllowed("setPrototypeOf"), isExtensible: notAllowed("isExtensible"), preventExtensions: notAllowed("preventExtensions"), set: notAllowed("set"), deleteProperty: notAllowed("deleteProperty"), construct: notAllowed("construct"), getOwnPropertyDescriptor: notAllowed("getOwnPropertyDescriptor"), defineProperty: notAllowed("defineProperty"), }; const autoCleanupNodes = []; function deepMock(name, autoCleanup = true) { const rootNode = new node_1.MockzillaNode(name); const proxy = rootNode.getProxy(); const mock = new Proxy({ path: "", children: {}, rootNode }, deepMockHandler); if (autoCleanup) autoCleanupNodes.push(rootNode); return [proxy, mock, rootNode]; } exports.deepMock = deepMock; afterEach(() => { try { for (const node of autoCleanupNodes) node.verify(); } finally { for (const node of autoCleanupNodes) node.disable(); autoCleanupNodes.length = 0; } });