-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathicedfrisby-nock.spec.js
191 lines (170 loc) · 5.46 KB
/
icedfrisby-nock.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
'use strict'
const http = require('http')
const nock = require('nock')
const chai = require('chai')
const fetch = require('node-fetch')
const sinon = require('sinon')
const { mix } = require('mixwith')
chai.use(require('chai-as-promised'))
chai.use(require('sinon-chai'))
chai.use(require('dirty-chai'))
const frisby = mix(require('icedfrisby')).with(require('./icedfrisby-nock'))
const { expect } = chai
let server, port
before(async function () {
server = http.createServer((request, response) => response.end())
await server.listen()
port = server.address().port
})
after(async function () {
await server.close()
})
describe('icedfrisby-nock', function () {
it('sets up a mock which works correctly', async function () {
await frisby
.create(this.test.title)
.post('http://example.test/')
.intercept(nock => nock('http://example.test').post('/').reply(418))
.expectStatus(418)
.run()
})
it('sets hasIntercept to true', function () {
const test = frisby
.create(this.test.title)
.post('http://example.test/')
.intercept(nock => nock('http://example.test').post('/').reply(418))
expect(test.hasIntercept).to.equal(true)
})
it('disables network connections', async function () {
const onBefore = sinon.spy()
await frisby
.create('disables network connection')
.post('http://example.test/')
.intercept(nock => nock('http://example.test').post('/').reply(418))
.before(async () => {
onBefore()
await expect(fetch('http://other.test')).to.be.rejectedWith(
Error,
'request to http://other.test/ failed, reason: Nock: Disallowed net connect for "other.test:80/"'
)
})
.run()
expect(onBefore).to.have.been.calledOnce()
})
describe('when the test has finished', function () {
const itShouldReset = () => {
it('removes the mock', function () {
expect(nock.activeMocks()).to.have.lengthOf(0)
})
it('re-enables network connections', async function () {
await frisby
.create(this.test.title)
.get(`http://localhost:${port}`)
.expectStatus(200)
.run()
})
}
context('and succeeded', async function () {
before(async function () {
await frisby
.create('when the test has finished and succeeded')
.post('http://example.test/')
.intercept(nock => nock('http://example.test').post('/').reply(418))
.expectStatus(418)
.run()
})
itShouldReset()
})
context('and failed', function () {
before(async function () {
const test = frisby
.create('when the test has finished and failed')
.post('http://example.com/test')
.intercept(nock =>
nock('http://example.com').post('/test').reply(418)
)
.expectStatus(200)
// Intercept the raised exception to prevent Mocha from receiving it.
test._invokeExpects = function () {
try {
test.prototype._invokeExpects.call(test)
} catch (e) {
return
}
// If we catch the exeption, as expected, we should never get here.
expect.fail('The failed expectation should have raised an exception')
}
await test.run()
})
itShouldReset()
})
})
it('networkOn() enables network connections', async function () {
await frisby
.create(this.test.title)
.post('http://example.test/')
.intercept(nock => nock('http://example.test').post('/').reply(418))
.before(async () => {
await frisby
.create(this.test.title)
.get(`http://localhost:${port}`)
.expectStatus(200)
.run()
})
.networkOn()
.run()
})
it('networkOff() disables network connections', async function () {
const test = frisby
.create(this.test.title)
.get('http://google.com/')
.networkOff()
await expect(test.run()).to.be.rejectedWith(
Error,
/Nock: Disallowed net connect for "google.com:80\/"/
)
})
describe('conditional intercept', function () {
it('sets up a mock which works correctly if condition true', async function () {
await frisby
.create(this.test.title)
.post('http://example.test/')
.interceptIf(true, nock =>
nock('http://example.test').post('/').reply(418)
)
.expectStatus(418)
.run()
})
it('does not set up a mock if condition false', async function () {
await frisby
.create(this.test.title)
.get(`http://localhost:${port}`)
.interceptIf(false, nock =>
nock(`http://localhost:${port}`).get('/').reply(418)
)
.expectStatus(200)
.run()
})
// This test requires a network connection.
it('does sets hasIntercept to false if condition false', function () {
const test = frisby
.create(this.test.title)
.get(`http://localhost:${port}`)
.interceptIf(false, nock =>
nock(`http://localhost:${port}`).get('/').reply(418)
)
expect(test.hasIntercept).to.equal(false)
})
})
it('`skipIfIntercepted()` skips the test when it has an intercept', async function () {
await frisby
.create(this.test.title)
.post('http://example.test/')
.skipIfIntercepted()
.intercept(() => {})
.before(() => {
throw Error('This should not be run')
})
.run()
})
})