-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathfixture_spec.ts
More file actions
512 lines (455 loc) · 14.7 KB
/
fixture_spec.ts
File metadata and controls
512 lines (455 loc) · 14.7 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import path from 'path'
import Promise from 'bluebird'
import * as fixture from '../../lib/fixture'
import { fs } from '../../lib/util/fs'
import FixturesHelper from '@tooling/system-tests'
import snapshot from 'snap-shot-it'
import { setCtx, makeDataContext, clearCtx } from '../../lib/makeDataContext'
import { getCtx } from '@packages/data-context'
let ctx
describe('lib/fixture', () => {
before(async function () {
// Clear and set up DataContext
await clearCtx()
// @ts-expect-error
setCtx(makeDataContext({}))
ctx = getCtx()
FixturesHelper.scaffold()
this.todosPath = FixturesHelper.projectPath('todos')
await ctx.actions.project.setCurrentProjectAndTestingTypeForTestSetup(this.todosPath)
// needed to run these tests locally
// sinon.stub(ctx.browser, 'machineBrowsers').resolves([
// {
// channel: 'stable',
// displayName: 'Electron',
// family: 'chromium',
// majorVersion: '123',
// name: 'electron',
// path: 'path-to-browser-one',
// version: '123.45.67',
// },
// ])
const cfg = await ctx.lifecycleManager.getFullInitialConfig()
this.fixturesFolder = cfg.fixturesFolder
})
beforeEach(function () {
this.read = (folder, image, encoding) => {
return fs.readFileAsync(path.join(folder, image), encoding)
}
})
after(() => {
return FixturesHelper.remove()
})
context('file not found', () => {
it('throws when file cannot be found', function () {
const p = 'does-not-exist.json'
return fixture.get(this.fixturesFolder, p)
.then(() => {
throw new Error('should have failed but did not')
}).catch((err) => {
expect(err.message).to.include('A fixture file could not be found')
expect(err.message).to.include(p)
})
})
})
context('unicode escape syntax', () => {
it('can parse unicode escape in JSON', function () {
return fixture.get(this.fixturesFolder, 'unicode_escape.json').then((obj) => {
expect(obj).to.deep.eq({
name: '\u2665',
})
})
})
})
context('nested fixtures', () => {
it('can pass path to nested fixture', function () {
return fixture.get(this.fixturesFolder, 'nested/fixture.js').then((obj) => {
expect(obj).to.deep.eq({
nested: 'fixture',
})
})
})
})
context('json files', () => {
it('throws when json is invalid', function () {
return fixture.get(this.fixturesFolder, 'bad_json.json')
.then(() => {
throw new Error('should have failed but did not')
}).catch((err) => {
snapshot('invalid json error message', err.message)
})
})
it('does not reformat json on parse error', function () {
return fixture.get(this.fixturesFolder, 'bad_json.json')
.then(() => {
throw new Error('should have failed but did not')
}).catch((err) => {
// ensure the bad_json file was kept as before
return fs.readFileAsync(`${this.fixturesFolder}/bad_json.json`, 'utf8').then((str) => {
expect(str).to.eq(`\
{
"bad": "json"
"should": "not parse"
}\
`)
})
})
})
it('does not reformat json or write fixture file', function () {
return fixture.get(this.fixturesFolder, 'no_format.json').then((obj) => {
return fs.readFileAsync(`${this.fixturesFolder}/no_format.json`, 'utf8').then((json) => {
expect(json).to.eq('{"id": 1, "name": "brian"}')
})
})
})
it('does not remove string whitespace', function () {
return fixture.get(this.fixturesFolder, 'words.json').then((obj) => {
return fs.readFileAsync(`${this.fixturesFolder}/words.json`, 'utf8').then((json) => {
expect(json).to.eq(`\
{
"some": "multiple space separate words",
"that": "should keep their spaces"
}\
`)
})
})
})
it('parses json to valid JS object', function () {
return fixture.get(this.fixturesFolder, 'users.json').then((users) => {
expect(users).to.deep.eq([
{
id: 1,
name: 'brian',
}, {
id: 2,
name: 'jennifer',
},
])
})
})
it('does not reformat empty objects', function () {
// TODO: fix flaky test https://github.com/cypress-io/cypress/issues/23457
this.retries(15)
const fn = () => {
return fixture.get(this.fixturesFolder, 'empty_objects')
}
return Promise.map(Array(500), fn, { concurrency: 5 }).then(() => {
return fs.readFileAsync(`${this.fixturesFolder}/empty_objects.json`, 'utf8').then((str) => {
expect(str).to.eq(`\
{
"empty": {
"object": {},
"array": [],
"object2": {\n\n },
"array2": [\n\n ]
}
}\
`)
})
})
})
// https://github.com/cypress-io/cypress/issues/3739
it('can load a fixture with no extension when a same-named folder also exists', async () => {
// needed to run these tests locally
// sinon.stub(ctx.browser, 'machineBrowsers').resolves([
// {
// channel: 'stable',
// displayName: 'Electron',
// family: 'chromium',
// majorVersion: '123',
// name: 'electron',
// path: 'path-to-browser-one',
// version: '123.45.67',
// },
// ])
const projectPath = FixturesHelper.projectPath('folder-same-as-fixture')
await ctx.actions.project.setCurrentProjectAndTestingTypeForTestSetup(projectPath)
return ctx.lifecycleManager.getFullInitialConfig()
.then((cfg) => {
return fixture.get(cfg.fixturesFolder, 'foo')
.then((result) => {
expect(result).to.deep.eq({ 'bar': 'baz' })
})
})
})
it('should return encoded JSON', async function () {
const fixtures = [
{ encoding: '', content: Buffer.from('[{"json": true}]') },
{ encoding: 'base64', content: 'W3sianNvbiI6IHRydWV9XQ==' },
{ encoding: 'utf8', content: '[{"json": true}]' },
{ encoding: null, content: Buffer.from('[{"json": true}]') },
{ encoding: undefined, content: [{ json: true }] },
]
for (const { encoding, content } of fixtures) {
const result = await fixture.get(this.fixturesFolder, 'foo', { encoding })
expect(result).to.deep.equal(content)
}
})
})
context('js files', () => {
it('returns valid JS object', function () {
return fixture.get(this.fixturesFolder, 'user.js').then((user) => {
expect(user).to.deep.eq({
id: 1,
name: 'brian',
age: 29,
posts: [],
})
})
})
it('does not rewrite file as a formated valid JS object', function () {
return fixture.get(this.fixturesFolder, 'no_format.js').then((obj) => {
return fs.readFileAsync(`${this.fixturesFolder}/no_format.js`, 'utf8').then((str) => {
expect(str).to.eq('{foo: "bar", baz: "quux"}')
})
})
})
it('throws on a bad JS object', function () {
const e =
`\
bad_js.js:3
bar: "bar
^
ParseError: Unterminated string constant\
`
return fixture.get(this.fixturesFolder, 'bad_js.js')
.then(() => {
throw new Error('should have failed but did not')
}).catch((err) => {
expect(err.message).to.eq(`'bad_js.js' is not a valid JavaScript object.\n\n${e}`)
})
})
})
context('coffee files', () => {
it('returns valid coffee object', function () {
return fixture.get(this.fixturesFolder, 'valid_coffee_obj.coffee').then((coffeeObj) => {
expect(coffeeObj).to.deep.eq({
name: 'cypress',
users: [],
})
})
})
it('does not rewrite coffee files', function () {
return fixture.get(this.fixturesFolder, 'no_format_coffee.coffee').then(() => {
return fs.readFileAsync(`${this.fixturesFolder}/no_format_coffee.coffee`, 'utf8').then((str) => {
expect(str).to.eq(`\
[
{id: 1}
{id: 2}
]\
`)
})
})
})
it('throws on bad coffee object', function () {
return fixture.get(this.fixturesFolder, 'bad_coffee.coffee')
.then(() => {
throw new Error('should have failed but did not')
}).catch((err) => {
expect(err.message).to.eq(`\
'bad_coffee.coffee is not a valid CoffeeScript object.
[stdin]:1:1: error: missing }
{
^\
`)
})
})
})
context('html files', () => {
it('returns html as a string', function () {
return fixture.get(this.fixturesFolder, 'index.html').then((index) => {
expect(index).to.eq(`\
<!doctype html>
<html>
<head>
<title>index.html</title>
</head>
<body>
index
</body>
</html>\
`)
})
})
it('does not rewrite file as formatted html', function () {
return fixture.get(this.fixturesFolder, 'index.html').then(() => {
return fs.readFileAsync(`${this.fixturesFolder}/index.html`, 'utf8').then((str) => {
expect(str).to.eq(`\
<!doctype html>
<html>
<head>
<title>index.html</title>
</head>
<body>
index
</body>
</html>\
`)
})
})
})
})
context('txt files', () => {
it('returns text as string', function () {
return fixture.get(this.fixturesFolder, 'message.txt').then((index) => {
expect(index).to.eq('foobarbaz')
})
})
})
context('csv files', () => {
it('returns text as string', function () {
return fixture.get(this.fixturesFolder, 'data.csv').then((index) => {
expect(index).to.eq(`\
Name,Occupation,Birth Year
Jane,Engineer,1976
John,Chef,1982
\
`)
})
})
})
context('file with unknown extension', () => {
it('returns text as string', function () {
return fixture.get(this.fixturesFolder, 'unknown_ext.yaml').then((index) => {
expect(index).to.eq(`\
- foo
- bar
-
\
`)
})
})
})
// https://github.com/cypress-io/cypress/issues/1558
context('binary files', () => {
it('returns file as buffer regardless of extension when passed null encoding', function () {
return fixture.get(this.fixturesFolder, 'nested/fixture.js', { encoding: null }).then((index) => {
expect(index).to.eql(Buffer.from('{nested: "fixture"}'))
})
})
})
context('file with unknown extension and encoding specified', () => {
it('returns text encoded as specified', function () {
return fixture.get(this.fixturesFolder, 'ascii.foo', { encoding: 'ascii' }).then((index) => {
expect(index).to.eq('o#?\n')
})
})
})
context('image files', () => {
it('returns png as buffer', function () {
return this.read(this.fixturesFolder, 'images/flower.png')
.then((file) => {
return fixture.get(this.fixturesFolder, 'images/flower.png')
.then((result) => {
expect(result).to.eql(file)
})
})
})
it('returns jpg as buffer', function () {
return this.read(this.fixturesFolder, 'images/sample.jpg')
.then((file) => {
return fixture.get(this.fixturesFolder, 'images/sample.jpg')
.then((result) => {
expect(result).to.eql(file)
})
})
})
it('returns gif as buffer', function () {
return this.read(this.fixturesFolder, 'images/word.gif')
.then((file) => {
return fixture.get(this.fixturesFolder, 'images/word.gif')
.then((result) => {
expect(result).to.eql(file)
})
})
})
it('returns tif as buffer', function () {
return this.read(this.fixturesFolder, 'images/sample.tif')
.then((file) => {
return fixture.get(this.fixturesFolder, 'images/sample.tif')
.then((result) => {
expect(result).to.eql(file)
})
})
})
it('returns png as binary if that encoding is requested', function () {
return this.read(this.fixturesFolder, 'images/flower.png', 'binary')
.then((file) => {
return fixture.get(this.fixturesFolder, 'images/flower.png', { encoding: 'binary' })
.then((result) => {
expect(result).to.eq(file)
})
})
})
})
context('zip files', () => {
it('returns zip as buffer', function () {
return this.read(this.fixturesFolder, 'example.zip')
.then((file) => {
return fixture.get(this.fixturesFolder, 'example.zip').then((result) => {
expect(result).to.eql(file)
})
})
})
})
context('extension omitted', () => {
it('#1 finds json', function () {
return fixture.get(this.fixturesFolder, 'foo').then((obj) => {
expect(obj).to.deep.eq([
{ json: true },
])
})
})
it('#2 finds js', function () {
return fixture.get(this.fixturesFolder, 'bar').then((obj) => {
expect(obj).to.deep.eq({ js: true })
})
})
it('throws when no file by any extension can be found', function () {
return fixture.get(this.fixturesFolder, 'does-not-exist')
.then(() => {
throw new Error('should have failed but did not')
}).catch((err) => {
expect(err.message).to.include('A fixture file could not be found')
expect(err.message).to.include('/does-not-exist')
})
})
})
context('new lines', () => {
it('does not remove trailing new lines on .txt', function () {
return fixture.get(this.fixturesFolder, 'trailing_new_line.txt').then((str) => {
return fs.readFileAsync(`${this.fixturesFolder}/trailing_new_line.txt`, 'utf8').then((str2) => {
expect(str2).to.eq('foo\nbar\nbaz\n')
})
})
})
it('does not remove trailing new lines on .json', function () {
return fixture.get(this.fixturesFolder, 'trailing_new_line.json').then((str) => {
return fs.readFileAsync(`${this.fixturesFolder}/trailing_new_line.json`, 'utf8').then((str2) => {
expect(str2).to.eq('{"foo": "bar"}\n')
})
})
})
it('does not remove trailing new lines on .js', function () {
return fixture.get(this.fixturesFolder, 'trailing_new_line.js').then((str) => {
return fs.readFileAsync(`${this.fixturesFolder}/trailing_new_line.js`, 'utf8').then((str2) => {
expect(str2).to.eq('{foo: "bar"}\n')
})
})
})
it('does not remove trailing new lines on .coffee', function () {
return fixture.get(this.fixturesFolder, 'trailing_new_line_coffee.coffee').then((str) => {
return fs.readFileAsync(`${this.fixturesFolder}/trailing_new_line_coffee.coffee`, 'utf8').then((str2) => {
expect(str2).to.eq('{ foo: "bar" }\n')
})
})
})
it('does not remove trailing new lines on .html', function () {
return fixture.get(this.fixturesFolder, 'trailing_new_line.html').then((str) => {
return fs.readFileAsync(`${this.fixturesFolder}/trailing_new_line.html`, 'utf8').then((str2) => {
expect(str2).to.eq('<html><body>foo</body></html>\n')
})
})
})
})
})