/
index.js
344 lines (269 loc) · 12.3 KB
/
index.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'use strict';
const TestReader = require('lib/test-reader');
const TestParser = require('lib/test-reader/mocha-test-parser');
const TestSkipper = require('lib/test-reader/test-skipper');
const Events = require('lib/constants/runner-events');
const SetsBuilder = require('gemini-core').SetsBuilder;
const {makeConfigStub} = require('../../utils');
const _ = require('lodash');
describe('test-reader', () => {
const sandbox = sinon.sandbox.create();
const readTests_ = (opts = {}) => {
opts = _.defaults(opts, {
paths: [],
config: makeConfigStub(),
sets: [],
ignore: [],
grep: 'default-grep'
});
const reader = opts.reader || TestReader.create(opts.config);
return reader.read(opts);
};
beforeEach(() => {
sandbox.spy(SetsBuilder, 'create');
sandbox.stub(SetsBuilder.prototype, 'useFiles').returnsThis();
sandbox.stub(SetsBuilder.prototype, 'useSets').returnsThis();
sandbox.stub(SetsBuilder.prototype, 'useBrowsers').returnsThis();
sandbox.stub(SetsBuilder.prototype, 'build').resolves({groupByBrowser: () => ({})});
sandbox.stub(TestParser, 'prepare');
sandbox.spy(TestParser, 'create');
sandbox.stub(TestParser.prototype, 'applySkip').returnsThis();
sandbox.stub(TestParser.prototype, 'applyConfigController').returnsThis();
sandbox.stub(TestParser.prototype, 'applyGrep').returnsThis();
sandbox.stub(TestParser.prototype, 'loadFiles').returnsThis();
sandbox.stub(TestParser.prototype, 'parse');
sandbox.spy(TestSkipper, 'create');
});
afterEach(() => sandbox.restore());
describe('read', async () => {
it('should create set-builder with sets from config and default directory', async () => {
const defaultDir = require('../../../package').name;
await readTests_({
config: makeConfigStub({
sets: {
all: {}
}
})
});
assert.calledOnce(SetsBuilder.create);
assert.calledWithMatch(SetsBuilder.create, {all: {}}, {defaultDir});
});
it('should use passed paths', async () => {
await readTests_({paths: ['some/path']});
assert.calledOnceWith(SetsBuilder.prototype.useFiles, ['some/path']);
});
it('should use pased sets', async () => {
await readTests_({sets: ['set1']});
assert.calledOnceWith(SetsBuilder.prototype.useSets, ['set1']);
});
it('should use pased browsers', async () => {
await readTests_({browsers: ['bro1']});
assert.calledOnceWith(SetsBuilder.prototype.useBrowsers, ['bro1']);
});
it('should build set-collection using working directory', async () => {
await readTests_();
assert.calledOnceWith(SetsBuilder.prototype.build, process.cwd());
});
it('should pass ignore files to build', async () => {
await readTests_({ignore: 'foo/bar'});
assert.calledOnceWith(SetsBuilder.prototype.build, sinon.match.any, {ignore: 'foo/bar'});
});
it('should call set-builder methods in rigth order', async () => {
await readTests_();
assert.callOrder(
SetsBuilder.create,
SetsBuilder.prototype.useFiles,
SetsBuilder.prototype.useSets,
SetsBuilder.prototype.useBrowsers,
SetsBuilder.prototype.build
);
});
it('should group files by browser', async () => {
const groupByBrowser = sinon.stub().returns({});
SetsBuilder.prototype.build.resolves({groupByBrowser});
await readTests_();
assert.calledOnce(groupByBrowser);
});
it('should prepare parser once before parsing', async () => {
const groupByBrowser = sinon.stub().returns({
bro1: [],
bro2: []
});
SetsBuilder.prototype.build.resolves({groupByBrowser});
const config = makeConfigStub({browsers: ['bro1', 'bro2']});
await readTests_({config});
assert.calledOnce(TestParser.prepare);
assert.callOrder(TestParser.prepare, TestParser.create);
});
it('should create parser for each browser with the same config object', async () => {
const groupByBrowser = sinon.stub().returns({
bro1: [],
bro2: []
});
SetsBuilder.prototype.build.resolves({groupByBrowser});
const config = makeConfigStub({browsers: ['bro1', 'bro2']});
await readTests_({config});
assert.calledTwice(TestParser.create);
assert.calledWith(TestParser.create, 'bro1', config);
assert.calledWith(TestParser.create, 'bro2', config);
});
it('should load files for each browser', async () => {
const groupByBrowser = sinon.stub().returns({
bro1: ['common/file', 'file1'],
bro2: ['common/file', 'file2']
});
SetsBuilder.prototype.build.resolves({groupByBrowser});
const config = makeConfigStub({browsers: ['bro1', 'bro2']});
await readTests_({config});
assert.calledTwice(TestParser.prototype.loadFiles);
assert.calledWith(TestParser.prototype.loadFiles, ['common/file', 'file1']);
assert.calledWith(TestParser.prototype.loadFiles, ['common/file', 'file2']);
});
it('should return parsed tests grouped by browser', async () => {
const groupByBrowser = sinon.stub().returns({
bro1: ['file1'],
bro2: ['file2']
});
SetsBuilder.prototype.build.resolves({groupByBrowser});
const test1 = {title: 'test1'};
const test2 = {title: 'test2'};
const test3 = {title: 'test3'};
const test4 = {title: 'test4'};
TestParser.prototype.loadFiles
.withArgs(['file1']).callsFake(function() {
this._tests = [test1, test2];
return this;
})
.withArgs(['file2']).callsFake(function() {
this._tests = [test3, test4];
return this;
});
TestParser.prototype.parse.callsFake(function() {
return this._tests;
});
const config = makeConfigStub({browsers: ['bro1', 'bro2']});
const specs = await readTests_({config});
assert.deepEqual(specs, {
bro1: [test1, test2],
bro2: [test3, test4]
});
});
it('should apply config controller for all browsers before loading any file', async () => {
const groupByBrowser = sinon.stub().returns({
bro1: [],
bro2: []
});
SetsBuilder.prototype.build.resolves({groupByBrowser});
const calls = [];
TestParser.prototype.applyConfigController.reset();
TestParser.prototype.applyConfigController.callsFake(function() {
calls.push('applyConfigController');
return this;
});
TestParser.prototype.loadFiles.reset();
TestParser.prototype.loadFiles.callsFake(function() {
calls.push('loadFiles');
return this;
});
const config = makeConfigStub({browsers: ['bro1', 'bro2']});
await readTests_({config});
assert.deepEqual(calls, ['applyConfigController', 'applyConfigController', 'loadFiles', 'loadFiles']);
});
it('should apply grep for all browsers before loading any file', async () => {
const groupByBrowser = sinon.stub().returns({
bro1: [],
bro2: []
});
SetsBuilder.prototype.build.resolves({groupByBrowser});
const calls = [];
TestParser.prototype.applyGrep.reset();
TestParser.prototype.applyGrep.callsFake(function() {
calls.push('applyGrep');
return this;
});
TestParser.prototype.loadFiles.reset();
TestParser.prototype.loadFiles.callsFake(function() {
calls.push('loadFiles');
return this;
});
const config = makeConfigStub({browsers: ['bro1', 'bro2']});
await readTests_({config});
assert.deepEqual(calls, ['applyGrep', 'applyGrep', 'loadFiles', 'loadFiles']);
});
it('should load files for all browsers before parsing any', async () => {
const groupByBrowser = sinon.stub().returns({
bro1: [],
bro2: []
});
SetsBuilder.prototype.build.resolves({groupByBrowser});
const calls = [];
TestParser.prototype.loadFiles.reset();
TestParser.prototype.loadFiles.callsFake(function() {
calls.push('loadFiles');
return this;
});
TestParser.prototype.parse.callsFake(() => calls.push('parse'));
const config = makeConfigStub({browsers: ['bro1', 'bro2']});
await readTests_({config});
assert.deepEqual(calls, ['loadFiles', 'loadFiles', 'parse', 'parse']);
});
describe('for each browser', () => {
beforeEach(() => {
SetsBuilder.prototype.build.resolves({
groupByBrowser: () => ({bro: []})
});
});
[
'BEFORE_FILE_READ',
'AFTER_FILE_READ'
].forEach((event) => {
it(`should passthrough ${event} event from test reader`, async () => {
const onEvent = sinon.spy().named(`on${event}`);
const reader = TestReader.create(makeConfigStub({browsers: ['bro']}))
.on(Events[event], onEvent);
TestParser.prototype.parse.callsFake(function() {
this.emit(Events[event], {foo: 'bar'});
});
await readTests_({reader});
assert.calledOnceWith(onEvent, {foo: 'bar'});
});
});
it('should create test skipper', async () => {
const config = makeConfigStub({browsers: ['bro']});
await readTests_({config});
assert.calledOnceWith(TestSkipper.create, config);
});
it('should share test skipper between all parsers', async () => {
SetsBuilder.prototype.build.resolves({
groupByBrowser: () => ({
bro1: [],
bro2: []
})
});
const config = makeConfigStub({browsers: ['bro1', 'bro2']});
await readTests_({config});
assert.calledTwice(TestParser.prototype.applySkip);
assert.equal(
TestParser.prototype.applySkip.firstCall.args[0],
TestParser.prototype.applySkip.secondCall.args[0]
);
});
it('should apply grep to test parser', async () => {
const config = makeConfigStub({browsers: ['bro']});
await readTests_({config, grep: 'foo bar'});
assert.calledOnceWith(TestParser.prototype.applyGrep, 'foo bar');
});
it('should apply all props before loading files', async () => {
const config = makeConfigStub({browsers: ['bro']});
await readTests_({config});
assert.callOrder(TestParser.prototype.applySkip, TestParser.prototype.loadFiles);
assert.callOrder(TestParser.prototype.applyGrep, TestParser.prototype.loadFiles);
});
it('should load files before parsing', async () => {
const config = makeConfigStub({browsers: ['bro']});
await readTests_({config});
assert.callOrder(TestParser.prototype.loadFiles, TestParser.prototype.parse);
});
});
});
});