-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathopen_project_spec.js
More file actions
278 lines (226 loc) · 9.84 KB
/
open_project_spec.js
File metadata and controls
278 lines (226 loc) · 9.84 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
require('../spec_helper')
const Bluebird = require('bluebird')
const browsers = require(`../../lib/browsers`)
const ProjectBase = require(`../../lib/project-base`).ProjectBase
const { openProject } = require('../../lib/open_project')
const preprocessor = require(`../../lib/plugins/preprocessor`).default
const runEvents = require(`../../lib/plugins/run_events`).default
const Fixtures = require('@tooling/system-tests')
const delay = require('lodash/delay')
const todosPath = Fixtures.projectPath('todos')
describe('lib/open_project', () => {
beforeEach(function () {
this.automation = {
reset: sinon.stub(),
use: sinon.stub(),
}
this.config = {
excludeSpecPattern: '**/*.nope',
projectRoot: todosPath,
proxyServer: 'http://cy-proxy-server',
}
this.onError = sinon.stub()
sinon.stub(browsers, 'get').resolves()
sinon.stub(browsers, 'open')
sinon.stub(browsers, 'connectToNewSpec')
sinon.stub(ProjectBase.prototype, 'initializeConfig').resolves({
specPattern: 'cypress/integration/**/*',
})
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 Fixtures.scaffoldProject('todos').then(() => {
return openProject.create(todosPath, { testingType: 'e2e' }, { onError: this.onError })
})
})
context('#launch', () => {
beforeEach(async function () {
await openProject.create(todosPath, { testingType: 'e2e' }, { onError: this.onError })
openProject.getProject().__setConfig({
browserUrl: 'http://localhost:8888/__/',
projectRoot: todosPath,
specType: 'integration',
e2e: {
specPattern: 'cypress/integration/**/*',
},
})
openProject.getProject().options = {
onError: this.onError,
}
this.spec = {
absolute: 'path/to/spec',
relative: '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 () {
this.beforeEach(function () {
sinon.stub(runEvents, 'execute').resolves()
})
it('executes after:spec on browser close if in interactive mode', function () {
this.config.experimentalInteractiveRunEvents = true
this.config.isTextTerminal = false
const onBrowserClose = () => Promise.resolve()
return openProject.launch(this.browser, this.spec, { onBrowserClose })
.then(() => {
return browsers.open.lastCall.args[1].onBrowserClose()
})
.then(() => {
expect(runEvents.execute).to.be.calledWith('after:spec', 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
const onBrowserClose = () => Promise.resolve()
return openProject.launch(this.browser, this.spec, { onBrowserClose })
.then(() => {
return browsers.open.lastCall.args[1].onBrowserClose()
})
.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
const onBrowserClose = () => Promise.resolve()
return openProject.launch(this.browser, this.spec, { onBrowserClose })
.then(() => {
return browsers.open.lastCall.args[1].onBrowserClose()
})
.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
const onBrowserClose = () => Promise.resolve()
return openProject.launch(this.browser, this.spec, { onBrowserClose })
.then(() => {
openProject.__reset()
return browsers.open.lastCall.args[1].onBrowserClose()
})
.then(() => {
expect(runEvents.execute).not.to.be.calledWith('after:spec')
})
})
it('sends after:spec errors through onError option', function () {
// TODO: fix flaky test https://github.com/cypress-io/cypress/issues/23448
this.retries(15)
const err = new Error('thrown from after:spec handler')
this.config.experimentalInteractiveRunEvents = true
this.config.isTextTerminal = false
runEvents.execute.withArgs('after:spec').rejects(err)
return openProject.launch(this.browser, this.spec, { onError: this.onError })
.then(() => {
return browsers.open.lastCall.args[1].onBrowserClose()
})
.then(() => {
return new Bluebird((res) => {
delay(() => {
expect(runEvents.execute).to.be.calledWith('after:spec')
expect(this.onError).to.be.calledWith(err)
res()
}, 100)
})
})
})
it('calls connectToNewSpec when shouldLaunchNewTab is set and the browser is not electron', async function () {
await openProject.launch(this.browser, this.spec, { shouldLaunchNewTab: true })
expect(browsers.connectToNewSpec.lastCall.args[0]).to.be.equal(this.browser)
})
it('calls open when shouldLaunchNewTab is set and the browser is electron', async function () {
await openProject.launch({ name: 'electron' }, this.spec, { shouldLaunchNewTab: true })
expect(browsers.open).to.have.been.calledOnce
})
})
})
context('#sendFocusBrowserMessage', () => {
it('focuses browser if runner is connected', async () => {
// Stubbing out relaunchBrowser function created during launch
openProject.relaunchBrowser = sinon.stub()
sinon.stub(ProjectBase.prototype, 'isRunnerSocketConnected').returns(true)
sinon.stub(ProjectBase.prototype, 'sendFocusBrowserMessage').resolves()
await openProject.sendFocusBrowserMessage()
expect(ProjectBase.prototype.isRunnerSocketConnected).to.have.been.calledOnce
expect(ProjectBase.prototype.sendFocusBrowserMessage).to.have.been.calledOnce
expect(openProject.relaunchBrowser).not.to.have.been.called
})
it('relaunches browser if runner is not connected and relaunch exists', async () => {
// Stubbing out relaunchBrowser function created during launch
openProject.relaunchBrowser = sinon.stub()
sinon.stub(ProjectBase.prototype, 'isRunnerSocketConnected').returns(false)
sinon.stub(ProjectBase.prototype, 'sendFocusBrowserMessage').resolves()
await openProject.sendFocusBrowserMessage()
expect(ProjectBase.prototype.isRunnerSocketConnected).to.have.been.calledOnce
expect(ProjectBase.prototype.sendFocusBrowserMessage).not.to.have.been.called
expect(openProject.relaunchBrowser).to.have.been.calledOnce
})
it('does not throw if relaunch is not defined', async () => {
// Stubbing out relaunchBrowser function created during launch
openProject.relaunchBrowser = null
sinon.stub(ProjectBase.prototype, 'isRunnerSocketConnected').returns(false)
sinon.stub(ProjectBase.prototype, 'sendFocusBrowserMessage').resolves()
await openProject.sendFocusBrowserMessage()
expect(ProjectBase.prototype.isRunnerSocketConnected).to.have.been.calledOnce
expect(ProjectBase.prototype.sendFocusBrowserMessage).not.to.have.been.called
})
})
context('#connectProtocolToBrowser', () => {
it('connects protocol to browser', async () => {
sinon.stub(browsers, 'connectProtocolToBrowser').resolves()
const options = sinon.stub()
await openProject.connectProtocolToBrowser(options)
expect(browsers.connectProtocolToBrowser).to.be.calledWith(options)
})
})
context('#connectCyPromptToBrowser', () => {
it('connects cy prompt to browser', async () => {
sinon.stub(browsers, 'connectCyPromptToBrowser').resolves()
const options = sinon.stub()
await openProject.connectCyPromptToBrowser(options)
})
})
})