|
1 |
| -/* eslint-disable import/order */ |
2 |
| -import * as mockery from 'mockery'; |
| 1 | +import * as child_process from 'child_process'; |
| 2 | +import { mocked } from 'jest-mock'; |
3 | 3 | import { CommandHandler } from '../lib/command-api';
|
4 | 4 | import { realHandler } from '../lib/commands/docs';
|
5 |
| - |
6 | 5 | const argv = {
|
7 | 6 | browser: 'echo %u',
|
8 | 7 | commandHandler: undefined as (CommandHandler | undefined),
|
9 | 8 | };
|
10 | 9 |
|
11 |
| -describe('`cdk docs`', () => { |
12 |
| - beforeEach(done => { |
13 |
| - mockery.registerMock('../../lib/logging', { |
14 |
| - debug() { return; }, |
15 |
| - error() { return; }, |
16 |
| - print() { return; }, |
17 |
| - warning() { return; }, |
18 |
| - }); |
19 |
| - mockery.enable({ useCleanCache: true, warnOnReplace: true, warnOnUnregistered: false }); |
20 |
| - done(); |
21 |
| - }); |
| 10 | +// eslint-disable-next-line no-console |
| 11 | +console.log = jest.fn(); |
| 12 | +jest.mock('child_process'); |
22 | 13 |
|
23 |
| - afterAll(done => { |
24 |
| - mockery.disable(); |
25 |
| - mockery.deregisterAll(); |
26 |
| - done(); |
27 |
| - }); |
| 14 | +describe('`cdk docs`', () => { |
28 | 15 |
|
29 | 16 | test('exits with 0 when everything is OK', async () => {
|
| 17 | + const mockChildProcessExec: any = (_: string, cb: (err?: Error, stdout?: string, stderr?: string) => void) => cb(); |
| 18 | + mocked(child_process.exec).mockImplementation(mockChildProcessExec); |
| 19 | + |
30 | 20 | const result = await realHandler({ args: argv } as any);
|
31 | 21 | expect(result).toBe(0);
|
32 | 22 | });
|
33 | 23 |
|
34 | 24 | test('exits with 0 when opening the browser fails', async () => {
|
35 |
| - mockery.registerMock('child_process', { |
36 |
| - exec(_: string, cb: (err: Error, stdout?: string, stderr?: string) => void) { |
37 |
| - cb(new Error('TEST')); |
38 |
| - }, |
39 |
| - }); |
| 25 | + const mockChildProcessExec: any = (_: string, cb: (err: Error, stdout?: string, stderr?: string) => void) => cb(new Error('TEST')); |
| 26 | + mocked(child_process.exec).mockImplementation(mockChildProcessExec); |
| 27 | + |
40 | 28 | const result = await realHandler({ args: argv } as any);
|
41 | 29 | expect(result).toBe(0);
|
42 | 30 | });
|
|
0 commit comments