-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathcypress_spec.js
278 lines (227 loc) · 7.51 KB
/
cypress_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
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 os = require('os')
const path = require('path')
const R = require('ramda')
const snapshot = require('../support/snapshot')
const Promise = require('bluebird')
const tmp = Promise.promisifyAll(require('tmp'))
const mockfs = require('mock-fs')
const fs = require(`${lib}/fs`)
const open = require(`${lib}/exec/open`)
const run = require(`${lib}/exec/run`)
const cypress = require(`${lib}/cypress`)
describe('cypress', function () {
beforeEach(function () {
mockfs({})
})
afterEach(() => {
mockfs.restore()
})
context('.open', function () {
beforeEach(function () {
sinon.stub(open, 'start').resolves()
})
const getCallArgs = R.path(['lastCall', 'args', 0])
const getStartArgs = () => {
expect(open.start).to.be.called
return getCallArgs(open.start)
}
it('calls open#start, passing in options', function () {
return cypress.open({ foo: 'foo' })
.then(getStartArgs)
.then((args) => {
expect(args.foo).to.equal('foo')
})
})
it('normalizes config object', () => {
const config = {
pageLoadTime: 10000,
watchForFileChanges: false,
}
return cypress.open({ config })
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq({ config: JSON.stringify(config) })
})
})
it('passes configFile: false', () => {
const opts = {
configFile: false,
}
return cypress.open(opts)
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq(opts)
})
})
})
context('.run fails to write results file', function () {
it('resolves with error object', function () {
const outputPath = path.join(os.tmpdir(), 'cypress/monorepo/cypress_spec/output.json')
sinon.stub(tmp, 'fileAsync').resolves(outputPath)
sinon.stub(run, 'start').resolves(2)
sinon.stub(fs, 'readJsonAsync').withArgs(outputPath).resolves()
return cypress.run().then((result) => {
expect(result).to.deep.equal({
status: 'failed',
failures: 2,
message: 'Could not find Cypress test run results',
})
})
})
})
context('.run', function () {
let outputPath
beforeEach(function () {
outputPath = path.join(os.tmpdir(), 'cypress/monorepo/cypress_spec/output.json')
sinon.stub(tmp, 'fileAsync').resolves(outputPath)
sinon.stub(run, 'start').resolves()
return fs.outputJsonAsync(outputPath, {
code: 0,
failingTests: [],
})
})
const getCallArgs = R.path(['lastCall', 'args', 0])
const normalizeCallArgs = (args) => {
expect(args.outputPath).to.equal(outputPath)
delete args.outputPath
return args
}
const getStartArgs = () => {
expect(run.start).to.be.called
return normalizeCallArgs(getCallArgs(run.start))
}
it('calls run#start, passing in options', () => {
return cypress.run({ spec: 'foo' })
.then(getStartArgs)
.then((args) => {
expect(args.spec).to.equal('foo')
})
})
it('normalizes config object', () => {
const config = {
pageLoadTime: 10000,
watchForFileChanges: false,
}
return cypress.run({ config })
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq({ config: JSON.stringify(config) })
})
})
it('normalizes env option if passed an object', () => {
const env = { foo: 'bar', another: 'one' }
return cypress.run({ env })
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq({ env: JSON.stringify(env) })
})
})
it('gets random tmp file and passes it to run#start', function () {
return cypress.run().then(() => {
expect(run.start.lastCall.args[0].outputPath).to.equal(outputPath)
})
})
it('resolves with contents of tmp file', () => {
return cypress.run().then(snapshot)
})
it('passes configFile: false', () => {
const opts = {
configFile: false,
}
return cypress.run(opts)
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq(opts)
})
})
it('rejects if project is an empty string', () => {
return expect(cypress.run({ project: '' })).to.be.rejected
})
it('rejects if project is true', () => {
return expect(cypress.run({ project: true })).to.be.rejected
})
it('rejects if project is false', () => {
return expect(cypress.run({ project: false })).to.be.rejected
})
it('passes quiet: true', () => {
const opts = {
quiet: true,
}
return cypress.run(opts)
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq(opts)
})
})
})
context('cli', function () {
describe('.parseRunArguments', function () {
it('parses CLI cypress run arguments', async () => {
const args = 'cypress run --browser chrome --spec my/test/spec.js'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
browser: 'chrome',
spec: 'my/test/spec.js',
})
})
it('parses CLI cypress run shorthand arguments', async () => {
const args = 'cypress run -b firefox -p 5005 --headed --quiet'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
browser: 'firefox',
port: 5005,
headed: true,
quiet: true,
})
})
it('coerces --record and --dev', async () => {
const args = 'cypress run --record false --dev true'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
record: false,
dev: true,
})
})
it('coerces --config-file false to boolean', async () => {
const args = 'cypress run --config-file false'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
configFile: false,
})
})
it('coerces --config-file cypress.config.js to string', async () => {
const args = 'cypress run --config-file cypress.config.js'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
configFile: 'cypress.config.js',
})
})
it('parses config file false', async () => {
const args = 'cypress run --config-file false'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
configFile: false,
})
})
it('parses config', async () => {
const args = 'cypress run --config baseUrl=localhost,video=true'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
// we don't need to convert the config into an object
// since the logic inside cypress.run handles that
expect(options).to.deep.equal({
config: 'baseUrl=localhost,video=true',
})
})
it('parses env', async () => {
const args = 'cypress run --env MY_NUMBER=42,MY_FLAG=true'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
// we don't need to convert the environment into an object
// since the logic inside cypress.run handles that
expect(options).to.deep.equal({
env: 'MY_NUMBER=42,MY_FLAG=true',
})
})
})
})
})