UNPKG

@bemedev/cli-test

Version:

A library for testing CLI libraries (INCEPTION !!)

87 lines (84 loc) 2.5 kB
import { prepareEnvironment } from '@gmrchk/cli-testing-library'; import { vi, beforeAll, afterAll } from 'vitest'; import 'node:path'; import { undefiny } from './helpers/utils.js'; const envFn = () => { return vi.fn(); }; const useBefore = () => { const readFile = envFn(); const writeFile = envFn(); const cleanup = envFn(); const ls = envFn(); const removeDir = envFn(); const execute = envFn(); const spawn = envFn(); const removeFile = envFn(); const exists = envFn(); const makeDir = envFn(); const path = vi.fn(); beforeAll(async () => { const cli = await prepareEnvironment(); // #region Implements functions readFile.mockImplementation(cli.readFile); writeFile.mockImplementation(cli.writeFile); cleanup.mockImplementation(cli.cleanup); ls.mockImplementation(cli.ls); removeDir.mockImplementation(cli.removeDir); execute.mockImplementation(cli.execute); spawn.mockImplementation(cli.spawn); makeDir.mockImplementation(cli.makeDir); path.mockReturnValue(cli.path); removeFile.mockImplementation(cli.removeFile); exists.mockImplementation(cli.exists); // #endregion }); return { readFile, ls, removeDir, execute, writeFile, spawn, cleanup, makeDir, path, removeFile, exists, read: undefiny(), }; }; const buildFn = (_fn) => { const fn = () => { const { readFile, ls, removeDir, execute, writeFile, spawn, cleanup, makeDir, path, removeFile, exists, } = useBefore(); const mockFs = (...envs) => { const __fn = globalThis?.buildMockFs?.getMockImplementation(); /* v8 ignore next 1 */ if (!__fn) return false; globalThis.buildMockFs({ exists, makeDir, removeDir, ls, readFile, writeFile }, ...envs); return true; }; afterAll(async () => { await cleanup(); // vi.clearAllMocks(); }); return _fn?.({ readFile, ls, removeDir, execute, writeFile, spawn, cleanup, makeDir, path: path(), removeFile, mockFs, exists, }); }; return fn; }; export { buildFn, envFn, useBefore }; //# sourceMappingURL=describe.utils.js.map