1 | jest.mock('testConfig.js', () => ({}), { virtual: true });
|
2 | jest.useFakeTimers();
|
3 |
|
4 | import { run } from '../index';
|
5 | import workerFarm from 'worker-farm';
|
6 | import Bluebird, {
|
7 | promisify,
|
8 | error,
|
9 | then,
|
10 | asCallback,
|
11 | } from 'bluebird';
|
12 |
|
13 | describe('index.js', () => {
|
14 | describe('run', () => {
|
15 | beforeEach(() => {
|
16 | jest.clearAllMocks();
|
17 | });
|
18 | afterEach(() => {
|
19 | process.removeAllListeners('SIGINT');
|
20 | })
|
21 |
|
22 | it('should reject promise config cannot get loaded', () => {
|
23 | const returnPromise = run('./does/not/exist.js', { colors: false }, jest.fn());
|
24 | expect(returnPromise.reject.mock.calls[0][0].toString()).toMatchSnapshot();
|
25 | });
|
26 |
|
27 | it('should reject promise if options validation fails', () => {
|
28 | const returnPromise = run('testConfig.js', { maxConcurrentWorkers: 'fail' }, jest.fn());
|
29 | expect(returnPromise.reject.mock.calls[0]).toMatchSnapshot();
|
30 | });
|
31 |
|
32 | it('should call generate workers and return farm promise', () => {
|
33 | jest.spyOn(console, 'log').mockImplementation(() => {});
|
34 | promisify.mockReturnValueOnce(jest.fn());
|
35 |
|
36 | const returnPromise = run('testConfig.js', { colors: false }, jest.fn());
|
37 | expect(workerFarm.mock.calls[0][0]).toEqual({ maxRetries: 0 });
|
38 |
|
39 | expect(returnPromise).toBe(Bluebird);
|
40 | expect(promisify).toHaveBeenCalledTimes(1);
|
41 | expect(error).toHaveBeenCalledTimes(1);
|
42 | expect(then).toHaveBeenCalledTimes(2);
|
43 | expect(Bluebird.finally).toHaveBeenCalledTimes(1);
|
44 | expect(asCallback).toHaveBeenCalledTimes(1);
|
45 | });
|
46 |
|
47 | describe('error callback', () => {
|
48 | beforeEach(() => {
|
49 | jest.spyOn(console, 'log').mockImplementation(() => {});
|
50 | jest.spyOn(Date, 'now')
|
51 | .mockImplementationOnce(() => 0)
|
52 | .mockImplementationOnce(() => 300);
|
53 | });
|
54 |
|
55 | const errorCbTest = options => {
|
56 | promisify.mockReturnValueOnce(jest.fn());
|
57 |
|
58 | const returnPromise = run('testConfig.js', {
|
59 | json: options.silent,
|
60 | colors: false
|
61 | }, jest.fn());
|
62 | const cb = returnPromise.error.mock.calls[0][0];
|
63 | const response = cb('Exception on worker farm');
|
64 |
|
65 | expect(response).toBe(Bluebird);
|
66 | expect(Bluebird.reject).toHaveBeenCalledWith('Exception on worker farm');
|
67 | if (options.silent) {
|
68 | expect(console.log).not.toHaveBeenCalled();
|
69 | } else {
|
70 | expect(console.log.mock.calls).toMatchSnapshot();
|
71 | }
|
72 | };
|
73 |
|
74 | it('should log and reject with error', () => {
|
75 | errorCbTest({ silent: false });
|
76 | });
|
77 | it('should only reject with error when silent', () => {
|
78 | errorCbTest({ silent: true });
|
79 | });
|
80 | });
|
81 |
|
82 | describe('then callback', () => {
|
83 | beforeEach(() => {
|
84 | jest.spyOn(console, 'log').mockImplementation(() => {});
|
85 | jest.spyOn(Date, 'now')
|
86 | .mockImplementationOnce(() => 30000)
|
87 | .mockImplementationOnce(() => 0);
|
88 | });
|
89 |
|
90 | const thenCbTest = options => {
|
91 | promisify.mockReturnValueOnce(jest.fn());
|
92 |
|
93 | const returnPromise = run('testConfig.js', {
|
94 | json: options.silent,
|
95 | colors: false
|
96 | }, jest.fn());
|
97 | const cb = returnPromise.then.mock.calls[1][0];
|
98 | const response = cb([true, true, false, undefined, '', 0]);
|
99 |
|
100 | expect(response).toEqual([ true, true ]);
|
101 | if(options.silent) {
|
102 | expect(console.log).not.toHaveBeenCalled();
|
103 | } else {
|
104 | expect(console.log.mock.calls).toMatchSnapshot();
|
105 | }
|
106 | };
|
107 |
|
108 | it('should filter non-truthy and return results', () => {
|
109 | thenCbTest({ silent: false });
|
110 | });
|
111 | it('should not log when silent', () => {
|
112 | thenCbTest({ silent: true });
|
113 | });
|
114 | });
|
115 |
|
116 | describe('finally callback', () => {
|
117 | it('should call end workerFarm and remove SIGINT listener', () => {
|
118 | promisify.mockReturnValueOnce(jest.fn());
|
119 |
|
120 | const returnPromise = run('testConfig.js', { colors: false }, jest.fn());
|
121 | const cb = returnPromise.finally.mock.calls[0][0];
|
122 |
|
123 | expect(process.listenerCount('SIGINT')).toBe(1);
|
124 | cb();
|
125 | jest.runOnlyPendingTimers();
|
126 | expect(process.listenerCount('SIGINT')).toBe(0);
|
127 |
|
128 |
|
129 | expect(workerFarm.end.mock.calls[0][0]).toBe(workerFarm.end);
|
130 | });
|
131 | it('should call keepAliveAfterFinishCallback if flag is set', () => {
|
132 | const options = {};
|
133 | Object.defineProperty(options, 'keepAliveAfterFinish', {
|
134 | value: 500
|
135 | });
|
136 | const keepAliveAfterFinishCallback = jest.fn(() => {
|
137 | setTimeout(expect.any(Function), options.keepAliveAfterFinish);
|
138 | });
|
139 | keepAliveAfterFinishCallback();
|
140 | expect(setTimeout).toHaveBeenCalledTimes(1);
|
141 | expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 500);
|
142 | });
|
143 | });
|
144 |
|
145 | describe('shutdownCallback', () => {
|
146 | const shutdownTest = options => {
|
147 | promisify.mockReturnValueOnce(jest.fn());
|
148 |
|
149 | const returnPromise = run('testConfig.js', {
|
150 | json: options.silent,
|
151 | colors: false
|
152 | }, jest.fn());
|
153 |
|
154 | expect(process.listenerCount('SIGINT')).toBe(1);
|
155 |
|
156 | process.emit('SIGINT');
|
157 |
|
158 | expect(workerFarm.end.mock.calls[0][0]).toBe(workerFarm.end);
|
159 | if(options.silent) {
|
160 | expect(console.log).not.toHaveBeenCalled();
|
161 | } else {
|
162 | expect(console.log.mock.calls).toMatchSnapshot();
|
163 | }
|
164 | };
|
165 |
|
166 | it('should call end and remove callback', () => {
|
167 | shutdownTest({ silent: false });
|
168 | });
|
169 |
|
170 | it('should call end and remove callback silently', () => {
|
171 | shutdownTest({ silent: true });
|
172 |
|
173 | });
|
174 | });
|
175 | });
|
176 |
|
177 | });
|