|
1 | | -import { execSync } from 'child_process'; |
| 1 | +import { program } from '../src/index'; |
| 2 | +import * as utilsModule from '../src/utils'; |
2 | 3 | import { description, version } from '../package.json'; |
3 | 4 |
|
4 | | -const ulysse = 'node -r @babel/register src/index.js'; |
| 5 | +const runCommand = (command) => { |
| 6 | + const args = command.split(' '); |
| 7 | + return program.parseAsync(['node', 'src/index.js', ...args]); |
| 8 | +}; |
| 9 | + |
| 10 | +beforeEach(async () => { |
| 11 | + jest.spyOn(console, 'log').mockImplementation(() => {}); |
| 12 | + jest.spyOn(utilsModule, 'isDaemonRunning').mockReturnValue(true); |
| 13 | +}); |
5 | 14 |
|
6 | 15 | test('Should display the version', async () => { |
7 | | - const output = execSync(`${ulysse} -v`); |
| 16 | + const mockWrite = jest.fn(); |
| 17 | + program.exitOverride().configureOutput({ writeOut: mockWrite }); |
| 18 | + |
| 19 | + await expect(runCommand('--version')).rejects.toThrow(); |
| 20 | + |
| 21 | + expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining(version)); |
| 22 | +}); |
| 23 | + |
| 24 | +test('Should display the help message', async () => { |
| 25 | + const mockWrite = jest.fn(); |
| 26 | + program.exitOverride().configureOutput({ writeOut: mockWrite }); |
| 27 | + |
| 28 | + await expect(runCommand('--help')).rejects.toThrow(); |
8 | 29 |
|
9 | | - expect(output.toString().trim()).toBe(version); |
| 30 | + expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining(description)); |
10 | 31 | }); |
11 | 32 |
|
12 | | -test('Should display the help', async () => { |
13 | | - const output = execSync(`${ulysse} -h`); |
| 33 | +test('Should block a domain', async () => { |
| 34 | + await runCommand('blocklist add --website example.com'); |
14 | 35 |
|
15 | | - expect(output.toString().trim()).toContain(description); |
| 36 | + expect(console.log).toHaveBeenCalledWith('Blocking website example.com'); |
16 | 37 | }); |
0 commit comments