Skip to content

Commit d632d22

Browse files
committed
feat: update
1 parent c88a7df commit d632d22

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

__tests__/index.spec.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
import { execSync } from 'child_process';
1+
import { program } from '../src/index';
2+
import * as utilsModule from '../src/utils';
23
import { description, version } from '../package.json';
34

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+
});
514

615
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();
829

9-
expect(output.toString().trim()).toBe(version);
30+
expect(mockWrite).toHaveBeenCalledWith(expect.stringContaining(description));
1031
});
1132

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');
1435

15-
expect(output.toString().trim()).toContain(description);
36+
expect(console.log).toHaveBeenCalledWith('Blocking website example.com');
1637
});

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,14 @@ program.commands.forEach((cmd) => {
310310
}
311311
});
312312

313-
program.parse(process.argv);
313+
const main = () => program.parse(process.argv);
314+
315+
if (process.env.NODE_ENV !== 'test') {
316+
main();
317+
}
314318

315319
if (!process.argv.slice(2).length) {
316320
program.outputHelp();
317321
}
322+
323+
export { program };

0 commit comments

Comments
 (0)