UNPKG

@whook/whook

Version:

Build strong and efficient REST web services.

93 lines (92 loc) 2.82 kB
import { describe, test, beforeEach, jest, expect } from '@jest/globals'; import initREPL from './repl.js'; import { PassThrough } from 'node:stream'; import streamtest from 'streamtest'; describe('initREPL', () => { const $ready = Promise.resolve(undefined); const $instance = { registered: jest.fn(), getRegisteredInitializer: jest.fn(), }; const $injector = jest.fn(); const $dispose = jest.fn(); const log = jest.fn(); beforeEach(() => { $injector.mockReset(); $dispose.mockReset(); log.mockReset(); }); test('should work as expected', async () => { const stdin = new PassThrough(); const [stdout, textPromise] = streamtest.toText(); const injectorPromise = new Promise((resolve) => { $injector.mockImplementation(() => { resolve(); return Promise.resolve({ time: () => Date.parse('2020-01-01T00:00:00Z'), }); }); }); const { dispose } = await initREPL({ $ready, $instance: $instance, $injector, $dispose, log, stdin: stdin, stdout: stdout, }); // Need to wait a bit await Promise.resolve(); await Promise.resolve(); stdin.write('.inject time;\n\n'); await injectorPromise; stdin.write('time();\n\n'); $instance.registered.mockReturnValueOnce(['time', 'log']); stdin.write('.registered\n\n'); if (dispose) { await dispose(); } stdout.end(); expect({ text: await textPromise, disposeCalls: $dispose.mock.calls, injectorCalls: $injector.mock.calls, logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')), }).toMatchInlineSnapshot(` { "disposeCalls": [ [], ], "injectorCalls": [ [ [ "time;", ], ], ], "logCalls": [ [ "debug", "🖵 - Initializing the REPL service!", ], ], "text": " _ ____ __ ___ _______ __ | | /| / / / ___ ___ / /__ / _ \\/ __/ _ \\/ / | |/ |/ / _ \\/ _ \\/ _ \\/ '_/ / , _/ _// ___/ /__ |__/|__/_//_/\\___/\\___/_/\\_\\ /_/|_/___/_/ /____/ Inject services with \`.inject\`. > .inject log > log('info', '👋 - Hello REPL!'); whook> whook> whook> 1577836800000 whook> whook> # Registered Services: - time (I), - log. (I: instantiated) whook> whook> ", } `); }); }); //# sourceMappingURL=repl.test.js.map