1 | 'use strict'
|
2 |
|
3 | const assert = require('assert')
|
4 | const recorder = require('./recorder')
|
5 | const {
|
6 | activate,
|
7 | disableNetConnect,
|
8 | enableNetConnect,
|
9 | removeAll: cleanAll,
|
10 | } = require('./intercept')
|
11 | const { loadDefs, define } = require('./scope')
|
12 |
|
13 | const { format } = require('util')
|
14 | const path = require('path')
|
15 | const debug = require('debug')('nock.back')
|
16 |
|
17 | let _mode = null
|
18 |
|
19 | let fs
|
20 |
|
21 | try {
|
22 | fs = require('fs')
|
23 | } catch (err) {
|
24 |
|
25 | }
|
26 |
|
27 | let mkdirp
|
28 | try {
|
29 | mkdirp = require('mkdirp')
|
30 | } catch (err) {
|
31 |
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function Back(fixtureName, options, nockedFn) {
|
55 | if (!Back.fixtures) {
|
56 | throw new Error(
|
57 | 'Back requires nock.back.fixtures to be set\n' +
|
58 | 'Ex:\n' +
|
59 | "\trequire(nock).back.fixtures = '/path/to/fixtures/'"
|
60 | )
|
61 | }
|
62 |
|
63 | if (typeof fixtureName !== 'string') {
|
64 | throw new Error('Parameter fixtureName must be a string')
|
65 | }
|
66 |
|
67 | if (arguments.length === 1) {
|
68 | options = {}
|
69 | } else if (arguments.length === 2) {
|
70 |
|
71 |
|
72 | if (typeof options === 'function') {
|
73 | nockedFn = options
|
74 | options = {}
|
75 | }
|
76 | }
|
77 |
|
78 | _mode.setup()
|
79 |
|
80 | const fixture = path.join(Back.fixtures, fixtureName)
|
81 | const context = _mode.start(fixture, options)
|
82 |
|
83 | const nockDone = function() {
|
84 | _mode.finish(fixture, options, context)
|
85 | }
|
86 |
|
87 | debug('context:', context)
|
88 |
|
89 |
|
90 | if (typeof nockedFn === 'function') {
|
91 | nockedFn.call(context, nockDone)
|
92 | } else {
|
93 | return Promise.resolve({ nockDone, context })
|
94 | }
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | const wild = {
|
102 | setup: function() {
|
103 | cleanAll()
|
104 | recorder.restore()
|
105 | activate()
|
106 | enableNetConnect()
|
107 | },
|
108 |
|
109 | start: function() {
|
110 | return load()
|
111 | },
|
112 |
|
113 | finish: function() {
|
114 |
|
115 | },
|
116 | }
|
117 |
|
118 | const dryrun = {
|
119 | setup: function() {
|
120 | recorder.restore()
|
121 | cleanAll()
|
122 | activate()
|
123 |
|
124 | enableNetConnect()
|
125 | },
|
126 |
|
127 | start: function(fixture, options) {
|
128 | const contexts = load(fixture, options)
|
129 |
|
130 | enableNetConnect()
|
131 | return contexts
|
132 | },
|
133 |
|
134 | finish: function() {
|
135 |
|
136 | },
|
137 | }
|
138 |
|
139 | const record = {
|
140 | setup: function() {
|
141 | recorder.restore()
|
142 | recorder.clear()
|
143 | cleanAll()
|
144 | activate()
|
145 | disableNetConnect()
|
146 | },
|
147 |
|
148 | start: function(fixture, options) {
|
149 | if (!fs) {
|
150 | throw new Error('no fs')
|
151 | }
|
152 | const context = load(fixture, options)
|
153 |
|
154 | if (!context.isLoaded) {
|
155 | recorder.record({
|
156 | dont_print: true,
|
157 | output_objects: true,
|
158 | ...options.recorder,
|
159 | })
|
160 |
|
161 | context.isRecording = true
|
162 | }
|
163 |
|
164 | return context
|
165 | },
|
166 |
|
167 | finish: function(fixture, options, context) {
|
168 | if (context.isRecording) {
|
169 | let outputs = recorder.outputs()
|
170 |
|
171 | if (typeof options.afterRecord === 'function') {
|
172 | outputs = options.afterRecord(outputs)
|
173 | }
|
174 |
|
175 | outputs =
|
176 | typeof outputs === 'string' ? outputs : JSON.stringify(outputs, null, 4)
|
177 | debug('recorder outputs:', outputs)
|
178 |
|
179 | mkdirp.sync(path.dirname(fixture))
|
180 | fs.writeFileSync(fixture, outputs)
|
181 | }
|
182 | },
|
183 | }
|
184 |
|
185 | const lockdown = {
|
186 | setup: function() {
|
187 | recorder.restore()
|
188 | recorder.clear()
|
189 | cleanAll()
|
190 | activate()
|
191 | disableNetConnect()
|
192 | },
|
193 |
|
194 | start: function(fixture, options) {
|
195 | return load(fixture, options)
|
196 | },
|
197 |
|
198 | finish: function() {
|
199 |
|
200 | },
|
201 | }
|
202 |
|
203 | function load(fixture, options) {
|
204 | const context = {
|
205 | scopes: [],
|
206 | assertScopesFinished: function() {
|
207 | assertScopes(this.scopes, fixture)
|
208 | },
|
209 | }
|
210 |
|
211 | if (fixture && fixtureExists(fixture)) {
|
212 | let scopes = loadDefs(fixture)
|
213 | applyHook(scopes, options.before)
|
214 |
|
215 | scopes = define(scopes)
|
216 | applyHook(scopes, options.after)
|
217 |
|
218 | context.scopes = scopes
|
219 | context.isLoaded = true
|
220 | }
|
221 |
|
222 | return context
|
223 | }
|
224 |
|
225 | function applyHook(scopes, fn) {
|
226 | if (!fn) {
|
227 | return
|
228 | }
|
229 |
|
230 | if (typeof fn !== 'function') {
|
231 | throw new Error('processing hooks must be a function')
|
232 | }
|
233 |
|
234 | scopes.forEach(fn)
|
235 | }
|
236 |
|
237 | function fixtureExists(fixture) {
|
238 | if (!fs) {
|
239 | throw new Error('no fs')
|
240 | }
|
241 |
|
242 | return fs.existsSync(fixture)
|
243 | }
|
244 |
|
245 | function assertScopes(scopes, fixture) {
|
246 | const pending = scopes
|
247 | .filter(scope => !scope.isDone())
|
248 | .map(scope => scope.pendingMocks())
|
249 |
|
250 | if (pending.length) {
|
251 | assert.fail(
|
252 | format(
|
253 | '%j was not used, consider removing %s to rerecord fixture',
|
254 | [].concat(...pending),
|
255 | fixture
|
256 | )
|
257 | )
|
258 | }
|
259 | }
|
260 |
|
261 | const Modes = {
|
262 | wild,
|
263 |
|
264 | dryrun,
|
265 |
|
266 | record,
|
267 |
|
268 | lockdown,
|
269 | }
|
270 |
|
271 | Back.setMode = function(mode) {
|
272 | if (!(mode in Modes)) {
|
273 | throw new Error(`Unknown mode: ${mode}`)
|
274 | }
|
275 |
|
276 | Back.currentMode = mode
|
277 | debug('New nock back mode:', Back.currentMode)
|
278 |
|
279 | _mode = Modes[mode]
|
280 | _mode.setup()
|
281 | }
|
282 |
|
283 | Back.fixtures = null
|
284 | Back.currentMode = null
|
285 |
|
286 | module.exports = Back
|