UNPKG

@whook/whook

Version:

Build strong and efficient REST web services.

114 lines 3.43 kB
import { describe, test, beforeEach, jest, expect } from '@jest/globals'; import initCommand from './command.js'; import { definition as initEnvCommandDefinition } from '../commands/env.js'; import { YError } from 'yerror'; import { DEFAULT_COERCION_OPTIONS } from '../libs/coercion.js'; describe('command', () => { const API = {}; const log = jest.fn(); const schemaValidators = jest.fn(); const validator = jest.fn(); beforeEach(() => { log.mockReset(); }); test('should work with no error', async () => { const commandHandler = async () => log('info', 'commandHandler ran'); let resolveWaitProcess; const waitProcess = new Promise((resolve) => { resolveWaitProcess = resolve; }); const $ready = Promise.resolve(); const $instance = { destroy: resolveWaitProcess, }; const $fatalError = {}; schemaValidators.mockReturnValue(validator); await initCommand({ ENV: { NO_PROMPT: '1', }, API, COMMAND_DEFINITION: initEnvCommandDefinition, COERCION_OPTIONS: DEFAULT_COERCION_OPTIONS, commandHandler, schemaValidators, args: { command: 'whook', rest: ['env'], namedArguments: { name: 'APP_ENV', default: 'test', }, }, $instance, $ready, $fatalError, log, }); await waitProcess; expect({ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')), }).toMatchInlineSnapshot(` { "logCalls": [ [ "info", "commandHandler ran", ], ], } `); }); test('should fail with errors', async () => { const commandHandler = async () => { throw new YError('E_ERRORING'); }; let resolveWaitProcess; const waitProcess = new Promise((resolve) => { resolveWaitProcess = resolve; }); const $ready = Promise.resolve(); const $instance = {}; const $fatalError = { throwFatalError: jest.fn().mockImplementationOnce(resolveWaitProcess), }; schemaValidators.mockReturnValue(validator); await initCommand({ ENV: { NO_PROMPT: '1', }, API, COMMAND_DEFINITION: initEnvCommandDefinition, COERCION_OPTIONS: DEFAULT_COERCION_OPTIONS, commandHandler, schemaValidators, args: { command: 'whook', rest: ['env'], namedArguments: { name: 'APP_ENV', default: 'test', }, }, $instance, $ready, $fatalError: $fatalError, log, }); await waitProcess; expect({ logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')), fatalErrorCalls: $fatalError.throwFatalError.mock.calls, }).toMatchInlineSnapshot(` { "fatalErrorCalls": [ [ [YError: E_ERRORING (): E_ERRORING], ], ], "logCalls": [], } `); }); }); //# sourceMappingURL=command.test.js.map