forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-fixtures-spec.js
134 lines (115 loc) · 3.67 KB
/
load-fixtures-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
/// <reference types="cypress" />
describe('Loading multiple fixtures', () => {
context('before each test using closure variables', () => {
let city
let country
beforeEach(() => {
cy.fixture('city').then((c) => {
city = c
})
cy.fixture('country').then((c) => {
country = c
})
})
it('has loaded fixtures', () => {
expect({ city, country }).to.deep.equal({
city: { name: 'Atlanta' },
country: { name: 'United States' },
})
})
it('still has fixtures in the second test', () => {
expect({ city, country }).to.deep.equal({
city: { name: 'Atlanta' },
country: { name: 'United States' },
})
})
})
context('once before all tests', () => {
let city
let country
before(() => {
// load fixtures once before any tests
// and they are kept in closure variables
cy.fixture('city').then((c) => {
city = c
})
cy.fixture('country').then((c) => {
country = c
})
})
it('has loaded fixtures', () => {
expect({ city, country }).to.deep.equal({
city: { name: 'Atlanta' },
country: { name: 'United States' },
})
})
it('still has loaded fixtures', () => {
// we have loaded the fixtures and stored them
// in the two variables and they should remain there
expect({ city, country }).to.deep.equal({
city: { name: 'Atlanta' },
country: { name: 'United States' },
})
})
})
context('using Mocha context', () => {
// notice how "beforeEach" callback uses "function"
// form to make sure Mocha context points correctly at "this"
beforeEach(function () {
cy.fixture('city').then((c) => {
this.city = c
})
cy.fixture('country').then((c) => {
this.country = c
})
})
it('has loaded fixtures', function () {
// again, the test has to use "function" callback
// to make sure "this" points at the Mocha context
expect(this.city).to.deep.equal({ name: 'Atlanta' })
expect(this.country).to.deep.equal({ name: 'United States' })
})
})
context('using @ as shortcut to the Mocha context', () => {
beforeEach(() => {
// we can ask Cypress to save the loaded fixture
// in the Mocha context using "cy.as" command
// in this case, the callback can be "function" or "=>" expression
cy.fixture('city').as('city')
cy.fixture('country').as('country')
})
it('has loaded fixtures', function () {
// again, the test has to use "function" callback
// to make sure "this" points at the Mocha context
expect(this.city).to.deep.equal({ name: 'Atlanta' })
expect(this.country).to.deep.equal({ name: 'United States' })
})
})
context('loading once and using @', () => {
let city
let country
before(() => {
// load fixtures just once, need to store in
// closure variables because Mocha context is cleared
// before each test
cy.fixture('city').then((c) => {
city = c
})
cy.fixture('country').then((c) => {
country = c
})
})
beforeEach(() => {
// we can put data back into the empty Mocha context before each test
// by the time this callback executes, "before" hook has finished
cy.wrap(city).as('city')
cy.wrap(country).as('country')
})
it('has loaded fixtures', function () {
// again, the test has to use "function" callback
// to make sure "this" points at the Mocha context
expect(this.city).to.deep.equal({ name: 'Atlanta' })
expect(this.country).to.deep.equal({ name: 'United States' })
})
})
})