/
test_persist_optionally.js
237 lines (184 loc) · 6.08 KB
/
test_persist_optionally.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
'use strict'
// `persist()` and `optionally()` are closely related. Their tests are both
// contained in this file.
const http = require('http')
const path = require('path')
const assertRejects = require('assert-rejects')
const { expect } = require('chai')
const nock = require('..')
const got = require('./got_client')
require('./setup')
const textFilePath = path.resolve(__dirname, './assets/reply_file_1.txt')
describe('`optionally()`', () => {
it('optional mocks do not appear in `pendingMocks()`', () => {
nock('http://example.test')
.get('/nonexistent')
.optionally()
.reply(200)
expect(nock.pendingMocks()).to.be.empty()
})
it('when called with `true`, makes the mock optional', () => {
nock('http://example.test')
.get('/nonexistent')
.optionally(true)
.reply(200)
expect(nock.pendingMocks()).to.be.empty()
})
it('when called with `false`, the mock is still required', () => {
nock('http://example.test')
.get('/nonexistent')
.optionally(false)
.reply(200)
expect(nock.pendingMocks()).to.have.lengthOf(1)
})
it('when called with non-boolean, throws the expected error', () => {
const interceptor = nock('http://example.test').get('/')
expect(() => interceptor.optionally('foo')).to.throw(
Error,
'Invalid arguments: argument should be a boolean'
)
})
it('optional mocks can be matched', done => {
nock('http://example.test')
.get('/abc')
.optionally()
.reply(200)
http.get({ host: 'example.test', path: '/abc' }, res => {
expect(res.statusCode).to.equal(200)
expect(nock.pendingMocks()).to.be.empty()
done()
})
})
it('before matching, `isDone()` is true', () => {
const scope = nock('http://example.test')
.get('/abc')
.optionally()
.reply(200)
expect(scope.isDone()).to.be.true()
})
describe('in conjunction with `persist()`', () => {
it('when optional mocks are also persisted, they do not appear as pending', async () => {
const scope = nock('http://example.test')
.get('/')
.optionally()
.reply(200)
.persist()
expect(nock.pendingMocks()).to.be.empty()
const response1 = await got('http://example.test/')
expect(response1.statusCode).to.equal(200)
expect(nock.pendingMocks()).to.be.empty()
const response2 = await got('http://example.test/')
expect(response2.statusCode).to.equal(200)
expect(nock.pendingMocks()).to.be.empty()
scope.done()
})
})
it('optional repeated mocks execute repeatedly', done => {
nock('http://example.test')
.get('/456')
.optionally()
.times(2)
.reply(200)
http.get({ host: 'example.test', path: '/456' }, res => {
expect(res.statusCode).to.equal(200)
http.get({ host: 'example.test', path: '/456' }, res => {
expect(res.statusCode).to.equal(200)
done()
})
})
})
it("optional mocks appear in `activeMocks()` only until they're matched", done => {
nock('http://example.test')
.get('/optional')
.optionally()
.reply(200)
expect(nock.activeMocks()).to.deep.equal([
'GET http://example.test:80/optional',
])
http.get({ host: 'example.test', path: '/optional' }, res => {
expect(nock.activeMocks()).to.be.empty()
done()
})
})
})
describe('`persist()`', () => {
it('`activeMocks()` always returns persisted mocks, even after matching', async () => {
const scope = nock('http://example.test')
.get('/persisted')
.reply(200)
.persist()
expect(nock.activeMocks()).to.deep.equal([
'GET http://example.test:80/persisted',
])
await got('http://example.test/persisted')
expect(nock.activeMocks()).to.deep.equal([
'GET http://example.test:80/persisted',
])
scope.done()
})
it('persisted mocks match repeatedly', async () => {
const scope = nock('http://example.test')
.persist()
.get('/')
.reply(200, 'Persisting all the way')
expect(scope.isDone()).to.be.false()
await got('http://example.test/')
expect(scope.isDone()).to.be.true()
await got('http://example.test/')
expect(scope.isDone()).to.be.true()
})
it('persisted mocks appear in `pendingMocks()`', async () => {
const scope = nock('http://example.test')
.get('/abc')
.reply(200, 'Persisted reply')
.persist()
expect(scope.pendingMocks()).to.deep.equal([
'GET http://example.test:80/abc',
])
})
it('persisted mocks are removed from `pendingMocks()` once they are matched once', async () => {
const scope = nock('http://example.test')
.get('/def')
.reply(200, 'Persisted reply')
.persist()
await got('http://example.test/def')
expect(scope.pendingMocks()).to.deep.equal([])
})
it('persisted mocks can use `replyWithFile()`', async () => {
nock('http://example.test')
.persist()
.get('/')
.replyWithFile(200, textFilePath)
.get('/test')
.reply(200, 'Yay!')
for (let i = 0; i < 2; ++i) {
const { statusCode, body } = await got('http://example.test/')
expect(statusCode).to.equal(200)
expect(body).to.equal('Hello from the file!')
}
})
it('can call `persist(false)` to stop persisting', async () => {
const scope = nock('http://example.test')
.persist(true)
.get('/')
.reply(200, 'Persisting all the way')
expect(scope.isDone()).to.be.false()
await got('http://example.test/')
expect(scope.isDone()).to.be.true()
expect(nock.activeMocks()).to.deep.equal(['GET http://example.test:80/'])
scope.persist(false)
await got('http://example.test/')
expect(nock.activeMocks()).to.be.empty()
expect(scope.isDone()).to.be.true()
await assertRejects(
got('http://example.test/'),
/Nock: No match for request/
)
})
it('when called with an invalid argument, throws the expected error', () => {
expect(() => nock('http://example.test').persist('string')).to.throw(
Error,
'Invalid arguments: argument should be a boolean'
)
})
})