forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_project_spec.js
More file actions
300 lines (245 loc) · 9.83 KB
/
open_project_spec.js
File metadata and controls
300 lines (245 loc) · 9.83 KB
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
require('../spec_helper')
const path = require('path')
const os = require('os')
const chokidar = require('chokidar')
const browsers = require(`${root}lib/browsers`)
const ProjectBase = require(`${root}lib/project-base`).ProjectBase
const { openProject } = require('../../lib/open_project')
const preprocessor = require(`${root}lib/plugins/preprocessor`)
const runEvents = require(`${root}lib/plugins/run_events`)
const Fixtures = require('@tooling/system-tests/lib/fixtures')
const todosPath = Fixtures.projectPath('todos')
describe('lib/open_project', () => {
beforeEach(function () {
this.automation = {
reset: sinon.stub(),
use: sinon.stub(),
}
this.config = {
integrationFolder: '/user/foo/cypress/integration',
testFiles: '**/*.*',
ignoreTestFiles: '**/*.nope',
projectRoot: '/project/root',
}
sinon.stub(browsers, 'get').resolves()
sinon.stub(browsers, 'open')
sinon.stub(ProjectBase.prototype, 'initializeConfig').resolves()
sinon.stub(ProjectBase.prototype, 'open').resolves()
sinon.stub(ProjectBase.prototype, 'reset').resolves()
sinon.stub(ProjectBase.prototype, 'getConfig').returns(this.config)
sinon.stub(ProjectBase.prototype, 'getAutomation').returns(this.automation)
sinon.stub(preprocessor, 'removeFile')
return openProject.create('/project/root', {}, {})
})
context('#create', () => {
// @see https://github.com/cypress-io/cypress/issues/18094
it('warns on win 32bit', async () => {
sinon.stub(os, 'platform').returns('win32')
sinon.stub(os, 'arch').returns('ia32')
const onWarning = sinon.stub()
await openProject.create('/root', {}, { onWarning })
expect(onWarning.getCall(0).args[0].message).to.include('You are running a 32-bit build')
})
})
context('#launch', () => {
beforeEach(async function () {
await openProject.create('/root', {}, {})
openProject.getProject().__setConfig({
browserUrl: 'http://localhost:8888/__/',
componentFolder: path.join(todosPath, 'component'),
integrationFolder: path.join(todosPath, 'tests'),
projectRoot: todosPath,
specType: 'integration',
})
openProject.getProject().options = {}
this.spec = {
absolute: 'path/to/spec',
}
this.browser = { name: 'chrome' }
})
it('tells preprocessor to remove file on browser close', function () {
return openProject.launch(this.browser, this.spec)
.then(() => {
browsers.open.lastCall.args[1].onBrowserClose()
expect(preprocessor.removeFile).to.be.calledWith('path/to/spec')
})
})
it('does not tell preprocessor to remove file if no spec', function () {
return openProject.launch(this.browser, {})
.then(() => {
browsers.open.lastCall.args[1].onBrowserClose()
expect(preprocessor.removeFile).not.to.be.called
})
})
it('runs original onBrowserClose callback on browser close', function () {
const onBrowserClose = sinon.stub()
const options = { onBrowserClose }
return openProject.launch(this.browser, this.spec, options)
.then(() => {
browsers.open.lastCall.args[1].onBrowserClose()
expect(onBrowserClose).to.be.called
})
})
it('calls project.reset on launch', function () {
return openProject.launch(this.browser, this.spec)
.then(() => {
expect(ProjectBase.prototype.reset).to.be.called
})
})
it('sets isHeaded + isHeadless if not already defined', function () {
expect(this.browser.isHeaded).to.be.undefined
expect(this.browser.isHeadless).to.be.undefined
return openProject.launch(this.browser, this.spec)
.then(() => {
expect(this.browser.isHeaded).to.be.true
expect(this.browser.isHeadless).to.be.false
})
})
describe('spec events', function () {
beforeEach(function () {
sinon.stub(runEvents, 'execute').resolves()
})
it('executes before:spec if in interactive mode', function () {
this.config.experimentalInteractiveRunEvents = true
this.config.isTextTerminal = false
return openProject.launch(this.browser, this.spec).then(() => {
expect(runEvents.execute).to.be.calledWith('before:spec', this.config, this.spec)
})
})
it('does not execute before:spec if not in interactive mode', function () {
this.config.experimentalInteractiveRunEvents = true
this.config.isTextTerminal = true
return openProject.launch(this.browser, this.spec).then(() => {
expect(runEvents.execute).not.to.be.calledWith('before:spec')
})
})
it('does not execute before:spec if experimental flag is not enabled', function () {
this.config.experimentalInteractiveRunEvents = false
this.config.isTextTerminal = false
return openProject.launch(this.browser, this.spec).then(() => {
expect(runEvents.execute).not.to.be.calledWith('before:spec')
})
})
it('executes after:spec on browser close if in interactive mode', function () {
this.config.experimentalInteractiveRunEvents = true
this.config.isTextTerminal = false
return openProject.launch(this.browser, this.spec)
.then(() => {
browsers.open.lastCall.args[1].onBrowserClose()
})
.delay(100) // needs a tick or two for the event to fire
.then(() => {
expect(runEvents.execute).to.be.calledWith('after:spec', this.config, this.spec)
})
})
it('does not execute after:spec on browser close if not in interactive mode', function () {
this.config.experimentalInteractiveRunEvents = true
this.config.isTextTerminal = true
return openProject.launch(this.browser, this.spec)
.then(() => {
browsers.open.lastCall.args[1].onBrowserClose()
})
.delay(10) // wait a few ticks to make sure it hasn't fired
.then(() => {
expect(runEvents.execute).not.to.be.calledWith('after:spec')
})
})
it('does not execute after:spec on browser close if experimental flag is not enabled', function () {
this.config.experimentalInteractiveRunEvents = false
this.config.isTextTerminal = false
return openProject.launch(this.browser, this.spec)
.then(() => {
browsers.open.lastCall.args[1].onBrowserClose()
})
.delay(10) // wait a few ticks to make sure it hasn't fired
.then(() => {
expect(runEvents.execute).not.to.be.calledWith('after:spec')
})
})
it('does not execute after:spec on browser close if the project is no longer open', function () {
this.config.experimentalInteractiveRunEvents = true
this.config.isTextTerminal = false
return openProject.launch(this.browser, this.spec)
.then(() => {
openProject.__reset()
browsers.open.lastCall.args[1].onBrowserClose()
})
.delay(10) // wait a few ticks to make sure it hasn't fired
.then(() => {
expect(runEvents.execute).not.to.be.calledWith('after:spec')
})
})
it('sends after:spec errors through onError option', function () {
const err = new Error('thrown from after:spec handler')
const onError = sinon.stub()
this.config.experimentalInteractiveRunEvents = true
this.config.isTextTerminal = false
runEvents.execute.withArgs('after:spec').rejects(err)
openProject.getProject().options.onError = onError
return openProject.launch(this.browser, this.spec)
.then(() => {
browsers.open.lastCall.args[1].onBrowserClose()
})
.delay(100) // needs a tick or two for the event to fire
.then(() => {
expect(runEvents.execute).to.be.calledWith('after:spec')
expect(onError).to.be.calledWith(err)
})
})
})
})
context('#getSpecChanges', () => {
beforeEach(function () {
this.watcherStub = {
on: sinon.stub(),
close: sinon.stub(),
}
sinon.stub(chokidar, 'watch').returns(this.watcherStub)
})
it('watches spec files', function () {
return openProject.getSpecChanges({}).then(() => {
expect(chokidar.watch).to.be.calledWith(this.config.testFiles, {
cwd: this.config.integrationFolder,
ignored: this.config.ignoreTestFiles,
ignoreInitial: true,
})
})
})
it('calls onChange callback when file is added', function () {
const onChange = sinon.spy()
this.watcherStub.on.withArgs('add').yields()
return openProject.getSpecChanges({ onChange }).then(() => {
expect(onChange).to.be.called
})
})
it('calls onChange callback when file is removed', function () {
const onChange = sinon.spy()
this.watcherStub.on.withArgs('unlink').yields()
return openProject.getSpecChanges({ onChange }).then(() => {
expect(onChange).to.be.called
})
})
it('only calls onChange once if there are multiple changes in a row', function () {
const onChange = sinon.spy()
this.watcherStub.on.withArgs('unlink').yields()
this.watcherStub.on.withArgs('add').yields()
this.watcherStub.on.withArgs('unlink').yields()
this.watcherStub.on.withArgs('add').yields()
return openProject.getSpecChanges({ onChange }).then(() => {
expect(onChange).to.be.calledOnce
})
})
it('destroys and creates specsWatcher as expected', function () {
return openProject.getSpecChanges()
.then(() => {
expect(openProject.specsWatcher).to.exist
openProject.stopSpecsWatcher()
expect(openProject.specsWatcher).to.be.null
return openProject.getSpecChanges()
.then(() => {
expect(openProject.specsWatcher).to.exist
})
})
})
})
})