-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathicedfrisby-nock.js
92 lines (80 loc) · 2.47 KB
/
icedfrisby-nock.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
'use strict'
const nock = require('nock')
function nockActivate() {
if (!nock.isActive()) {
nock.activate()
}
nock.disableNetConnect()
}
function nockCleanup() {
nock.abortPendingRequests()
nock.cleanAll()
nock.restore()
}
// This is a subclass factory. It returns a constructor function (i.e., a
// Frisby subclass) with these methods added in.
//
// Read more about this pattern:
// http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
const factory = superclass =>
class IcedFrisbyNock extends superclass {
constructor(...args) {
super(...args)
this.hasIntercept = false
}
skipIfIntercepted() {
return this.skipWhen(() => this.hasIntercept)
}
// Set up intercepts. Pass in a setup function which takes one argument,
// `nock`, and returns a nock scope. The function is invoked before the
// test, and the returned scope is asserted afterward.
//
// By default, disables remote network connections (other than localhost).
// To override this, chain on a call to `.enableNetConnect()`.
//
// You can only call this once per test.
//
// @param setup The setup function, receives `nock` and returns a nock object
intercept(setup) {
this.hasIntercept = true
let nockScope
this.before(() => {
nockActivate()
nockScope = setup(nock)
})
// https://github.com/node-nock/nock#expectations
this.after(() => {
nockScope.done()
})
this.finally(nockCleanup)
return this
}
// Delegates to `intercept()` only if the condition is truthy.
//
// @param condition The condition on which the intercept will be set or not
// @param setup The setup function, receives `nock` and returns a nock object
interceptIf(condition, setup) {
return condition ? this.intercept(setup) : this
}
// Disallow unexpected remote network connections by simulating failure.
// Allows connections to localhost. Invoked automatically by `intercept()`.
networkOff() {
return this.before(() => {
nockActivate()
nock.enableNetConnect(/(localhost|127\.0\.0\.1)/)
}).finally(() => {
nockCleanup()
nock.enableNetConnect()
})
}
// Enable remote network connections.
networkOn() {
return this.before(() => {
nockActivate()
nock.enableNetConnect()
}).finally(() => {
nockCleanup()
})
}
}
module.exports = factory