-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.test.js
98 lines (92 loc) · 2.82 KB
/
main.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { describe, jest, test, beforeEach } from '@jest/globals';
import { mockConfig, easyMock, easyResolve } from './mocks.js';
// setup mocks
const easyMocks = [
['src/commands/auth.js', ['authAction']],
['src/commands/init.js', ['initAction']],
['src/commands/solve.js', ['solveAction']],
['src/commands/stats.js', ['statsAction']],
['src/commands/submit.js', ['submitAction']],
['src/commands/import.js', ['importAction']],
['src/errorHandler.js', ['handleError']],
['src/festive.js', ['printFestiveTitle']],
['src/arguments.js', ['getDayArg', 'getLevelArg']],
];
easyMock(easyMocks);
mockConfig();
const mockCommander = (() => {
const toReturn = {
name: jest.fn().mockReturnThis(),
description: jest.fn().mockReturnThis(),
version: jest.fn().mockReturnThis(),
addHelpText: jest.fn().mockReturnThis(),
command: jest.fn().mockReturnThis(),
hook: jest.fn().mockReturnThis(),
action: jest.fn().mockReturnThis(),
argument: jest.fn().mockReturnThis(),
addArgument: jest.fn().mockReturnThis(),
option: jest.fn().mockReturnThis(),
parseAsync: jest.fn(),
};
jest.unstable_mockModule('commander', () => ({
// eslint-disable-next-line func-names, object-shorthand
Command: function () {
return toReturn;
},
// eslint-disable-next-line func-names, object-shorthand
Argument: function () {
return {
argParser: jest.fn().mockReturnThis(),
};
},
}));
return toReturn;
})();
// import after mocks set up
const {
authAction,
initAction,
solveAction,
statsAction,
submitAction,
importAction,
handleError,
} = await easyResolve(easyMocks);
describe('main', () => {
beforeEach(() => {
jest.clearAllMocks();
});
test.each([
['auth', authAction],
['init', initAction],
['solve', solveAction],
['submit', submitAction],
['stats', statsAction],
['import', importAction],
])('adds command %s', async (name, action) =>
jest.isolateModulesAsync(async () => {
await import('../src/main.js');
const commandIndex = mockCommander.command.mock.calls
.flat()
.indexOf(name);
const actionIndex = mockCommander.action.mock.calls
.flat()
.indexOf(action);
expect(commandIndex).not.toBe(-1);
expect(actionIndex).not.toBe(-1);
expect(commandIndex).toBe(actionIndex);
})
);
test('invokes the CLI', async () =>
jest.isolateModulesAsync(async () => {
await import('../src/main.js');
expect(mockCommander.parseAsync).toHaveBeenCalledTimes(1);
}));
test('invokes errorHandler on exception', async () =>
jest.isolateModulesAsync(async () => {
const error = new Error('BAD');
mockCommander.parseAsync.mockRejectedValue(error);
await import('../src/main.js');
expect(handleError).toHaveBeenCalledWith(error);
}));
});