-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.test.js
38 lines (30 loc) · 1.06 KB
/
function.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
const myFunc = require('./function').myFunc;
describe('jest-process-exit test suite', () => {
var testOutput = [];
const originalConsoleLog = console.log;
const testConsoleLog = (output) => { testOutput.push(output) };
beforeEach(() => {
console.log = testConsoleLog;
testOutput = [];
});
afterEach(() => {
console.log = originalConsoleLog;
});
it('tests myFunc with process.exit', async () => {
const mockExit = jest.spyOn(process, 'exit')
.mockImplementation((number) => { throw new Error('process.exit: ' + number); });
expect(() => {
myFunc(true);
}).toThrow();
expect(mockExit).toHaveBeenCalledWith(-1);
mockExit.mockRestore();
expect(testOutput.length).toBe(1);
expect(testOutput[0]).toBe('before');
});
it('tests myFunc without process.exit', async () => {
myFunc(false);
expect(testOutput.length).toBe(2);
expect(testOutput[0]).toBe('before');
expect(testOutput[1]).toBe('after');
});
});