This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
index.test.js
255 lines (204 loc) Β· 7.84 KB
/
index.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
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
'use strict';
const proxyquire = require('proxyquire');
const _ = require('lodash');
const q = require('q');
const EventEmitter = require('events').EventEmitter;
const utils = require('lib/utils');
const globExtra = require('glob-extra');
describe('test-reader', () => {
const sandbox = sinon.sandbox.create();
const testsApi = sandbox.stub();
let readTests;
const readTests_ = (opts) => {
const REQUIRED_OPTS = {
system: {
projectRoot: '/root'
}
};
opts = _.defaults(opts || {}, {
paths: [],
config: {},
emitter: new EventEmitter()
});
opts.config = _.merge(opts.config, REQUIRED_OPTS);
return readTests({paths: opts.paths, sets: opts.cliSets}, opts.config, opts.emitter);
};
beforeEach(() => {
sandbox.stub(utils, 'requireWithNoCache');
sandbox.stub(globExtra, 'expandPaths').returns(q([]));
readTests = proxyquire('lib/test-reader', {
'../tests-api': testsApi
});
});
afterEach(() => {
sandbox.restore();
testsApi.reset();
});
describe('global "gemini" variable', () => {
let gemini;
let config;
beforeEach(() => {
config = {
sets: {
set: {
files: ['some/files']
}
},
getBrowserIds: () => []
};
utils.requireWithNoCache.restore();
});
it('should use global "gemini" variable', () => {
sandbox.stub(utils, 'requireWithNoCache', () => gemini = global.gemini);
const api = {suite: 'api'};
testsApi.returns(api);
globExtra.expandPaths.returns(q(['some-test.js']));
return readTests_({config})
.then(() => assert.deepEqual(gemini, api));
});
it('should rewrite global "gemini" variable for each file', () => {
let globalGemini = [];
globExtra.expandPaths.returns(q(['/some/path/file1.js', '/some/path/file2.js']));
testsApi
.onFirstCall().returns({suite: 'apiInstance'})
.onSecondCall().returns({suite: 'anotherApiInstance'});
sandbox.stub(utils, 'requireWithNoCache', () => {
globalGemini.push(global.gemini.suite);
});
return readTests_({config})
.then(() => assert.deepEqual(globalGemini, ['apiInstance', 'anotherApiInstance']));
});
it('should delete global "gemini" variable after test reading', () => {
testsApi.returns({suite: 'api'});
globExtra.expandPaths.returns(q(['some-test.js']));
sandbox.stub(utils, 'requireWithNoCache');
return readTests_({config}).then(() => assert.notProperty(global, 'gemini'));
});
});
it('should use gemini folder if sets are not specified in config and paths are not passed', () => {
const config = {
getBrowserIds: () => []
};
globExtra.expandPaths.withArgs(['/root/gemini']).returns(q(['/root/gemini/file.js']));
return readTests_({config})
.then(() => assert.calledWith(utils.requireWithNoCache, '/root/gemini/file.js'));
});
it('should load suites related to sets from config', () => {
const config = {
sets: {
set: {
files: ['some/path']
}
}
};
globExtra.expandPaths
.withArgs(['some/path']).returns(q(['/some/path/file1.js', '/some/path/file2.js']));
return readTests_({config})
.then(() => {
assert.calledWith(utils.requireWithNoCache, '/some/path/file1.js');
assert.calledWith(utils.requireWithNoCache, '/some/path/file2.js');
});
});
it('should load suites related to sets from cli', () => {
const config = {
sets: {
set1: {
files: ['some/path']
},
set2: {
files: ['other/path']
}
}
};
globExtra.expandPaths.withArgs(['some/path']).returns(q(['/some/path/file1.js']));
return readTests_({cliSets: ['set1'], config})
.then(() => assert.alwaysCalledWithExactly(utils.requireWithNoCache, '/some/path/file1.js'));
});
it('should load suites related to paths from cli', () => {
const config = {
sets: {
set1: {
files: ['some/path']
},
set2: {
files: ['other/path']
}
}
};
globExtra.expandPaths
.withArgs(['some/path']).returns(q(['/some/path/file1.js']));
return readTests_({paths: ['some/path'], config})
.then(() => {
assert.alwaysCalledWithExactly(utils.requireWithNoCache, '/some/path/file1.js');
});
});
it('should load suites related to sets and paths from cli', () => {
const config = {
sets: {
set1: {
files: ['some/path', 'other/path']
}
}
};
globExtra.expandPaths
.withArgs(['some/path']).returns(q(['/some/path/file1.js']))
.withArgs(['some/path', 'other/path']).returns(q(['/some/path/file1.js', '/other/path/file2.js']));
return readTests_({cliSets: ['set1'], paths: ['some/path'], config})
.then(() => {
assert.calledWith(utils.requireWithNoCache, '/some/path/file1.js');
assert.neverCalledWith(utils.requireWithNoCache, '/other/path/file2.js');
});
});
it('should not load suites if sets do not contain paths from cli', () => {
const config = {
sets: {
set1: {
files: ['some/path']
}
}
};
return readTests_({paths: ['other/path'], config})
.then(() => assert.notCalled(utils.requireWithNoCache));
});
describe('events', () => {
function readTestsWithEmitter(absolutePath, emitter) {
const relativePath = 'some/path';
const config = {
sets: {
all: {
files: relativePath
}
}
};
globExtra.expandPaths
.withArgs(relativePath).returns(q([absolutePath]));
return readTests_({
paths: [relativePath],
emitter: emitter,
config: config
});
}
it('should emit "beforeFileRead" before reading each file', () => {
const filePath = '/some/path/file.js';
const beforeReadSpy = sandbox.spy().named('OnBeforeFileRead');
const emitter = new EventEmitter();
emitter.on('beforeFileRead', beforeReadSpy);
return readTestsWithEmitter(filePath, emitter)
.then(() => {
assert.calledWithExactly(beforeReadSpy, filePath);
assert.callOrder(beforeReadSpy, utils.requireWithNoCache);
});
});
it('should emit "afterFileRead" after reading each file', () => {
const filePath = '/some/path/file.js';
const afterReadSpy = sandbox.spy().named('OnAfterFileRead');
const emitter = new EventEmitter();
emitter.on('afterFileRead', afterReadSpy);
return readTestsWithEmitter(filePath, emitter)
.then(() => {
assert.calledWithExactly(afterReadSpy, filePath);
assert.callOrder(utils.requireWithNoCache, afterReadSpy);
});
});
});
});