/
worker.spec.js
223 lines (193 loc) · 6.6 KB
/
worker.spec.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'use strict';
const serializeJavascript = require('serialize-javascript');
const rewiremock = require('rewiremock/node');
const {SerializableWorkerResult} = require('../../lib/nodejs/serializer');
const {createSandbox} = require('sinon');
const WORKER_PATH = require.resolve('../../lib/nodejs/worker.js');
describe('worker', function() {
let worker;
let sandbox;
let stubs;
beforeEach(function() {
sandbox = createSandbox();
stubs = {
workerpool: {
isMainThread: false,
worker: sandbox.stub()
}
};
sandbox.spy(process, 'removeAllListeners');
});
describe('when run as main process', function() {
it('should throw', function() {
expect(() => {
rewiremock.proxy(WORKER_PATH, {
workerpool: {
isMainThread: true,
worker: stubs.workerpool.worker
}
});
}, 'to throw');
});
});
describe('when run as worker process', function() {
let mocha;
beforeEach(function() {
mocha = {
addFile: sandbox.stub().returnsThis(),
loadFilesAsync: sandbox.stub().resolves(),
run: sandbox.stub().callsArgAsync(0),
unloadFiles: sandbox.stub().returnsThis()
};
stubs.Mocha = Object.assign(sandbox.stub().returns(mocha), {
bdd: sandbox.stub(),
interfaces: {}
});
stubs.serializer = {
serialize: sandbox.stub()
};
stubs.runHelpers = {
handleRequires: sandbox.stub(),
validatePlugin: sandbox.stub(),
loadRootHooks: sandbox.stub().resolves()
};
worker = rewiremock.proxy(WORKER_PATH, {
workerpool: stubs.workerpool,
'../../lib/mocha': stubs.Mocha,
'../../lib/nodejs/serializer': stubs.serializer,
'../../lib/cli/run-helpers': stubs.runHelpers
});
});
it('should register itself with workerpool', function() {
expect(stubs.workerpool.worker, 'to have a call satisfying', [
{run: worker.run}
]);
});
describe('function', function() {
describe('run()', function() {
describe('when called without arguments', function() {
it('should reject', async function() {
return expect(worker.run, 'to be rejected with error satisfying', {
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
});
});
});
describe('when passed a non-string `options` value', function() {
it('should reject', async function() {
return expect(
() => worker.run('foo.js', 42),
'to be rejected with error satisfying',
{
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
}
);
});
});
describe('when passed an invalid string `options` value', function() {
it('should reject', async function() {
return expect(
() => worker.run('foo.js', 'tomfoolery'),
'to be rejected with error satisfying',
{
code: 'ERR_MOCHA_INVALID_ARG_VALUE'
}
);
});
});
describe('when called with empty "filepath" argument', function() {
it('should reject', async function() {
return expect(
() => worker.run(''),
'to be rejected with error satisfying',
{
code: 'ERR_MOCHA_INVALID_ARG_TYPE'
}
);
});
});
describe('when the file at "filepath" argument is unloadable', function() {
it('should reject', async function() {
mocha.loadFilesAsync.rejects();
return expect(
() => worker.run('some-non-existent-file.js'),
'to be rejected'
);
});
});
describe('when the file at "filepath" is loadable', function() {
let result;
beforeEach(function() {
result = SerializableWorkerResult.create();
mocha.loadFilesAsync.resolves();
mocha.run.yields(result);
});
it('should handle "--require"', async function() {
await worker.run(
'some-file.js',
serializeJavascript({require: 'foo'})
);
expect(
stubs.runHelpers.handleRequires,
'to have a call satisfying',
['foo']
).and('was called once');
});
it('should handle "--ui"', async function() {
const argv = {foo: 'bar'};
await worker.run('some-file.js', serializeJavascript(argv));
expect(
stubs.runHelpers.validatePlugin,
'to have a call satisfying',
[argv, 'ui', stubs.Mocha.interfaces]
).and('was called once');
});
it('should call Mocha#run', async function() {
await worker.run('some-file.js');
expect(mocha.run, 'was called once');
});
it('should remove all uncaughtException listeners', async function() {
await worker.run('some-file.js');
expect(process.removeAllListeners, 'to have a call satisfying', [
'uncaughtException'
]);
});
describe('when serialization succeeds', function() {
beforeEach(function() {
stubs.serializer.serialize.returnsArg(0);
});
it('should resolve with a SerializedWorkerResult', async function() {
return expect(
worker.run('some-file.js'),
'to be fulfilled with',
result
);
});
});
describe('when serialization fails', function() {
beforeEach(function() {
stubs.serializer.serialize.throws();
});
it('should reject', async function() {
return expect(worker.run('some-file.js'), 'to be rejected');
});
});
describe('when run twice', function() {
it('should initialize only once', async function() {
await worker.run('some-file.js');
await worker.run('some-other-file.js');
expect(stubs.runHelpers, 'to satisfy', {
handleRequires: expect.it('was called once'),
validatePlugin: expect.it('was called once')
});
});
});
});
});
});
});
afterEach(function() {
sandbox.restore();
// this is needed due to `require.cache` getting dumped in watch mode
process.removeAllListeners('beforeExit');
});
});