Skip to content

Commit

Permalink
feat: afterRecord support for custom formatting after recording. (#…
Browse files Browse the repository at this point in the history
…1682)

* ci(travis): run release stage on [Version].x, next, beta branches

* ci(package): update semantic-release to beta.13

* feat: `afterRecord` support for custom formatting after recording.

#1599
  • Loading branch information
KodiVerse authored and gr2m committed Sep 4, 2019
1 parent 41be884 commit da840fe
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -1437,7 +1437,7 @@ As an optional second parameter you can pass the following options

- `before`: a preprocessing function, gets called before nock.define
- `after`: a postprocessing function, gets called after nock.define
- `afterRecord`: a postprocessing function, gets called after recording. Is passed the array of scopes recorded and should return the array scopes to save to the fixture
- `afterRecord`: a postprocessing function, gets called after recording. Is passed the array of scopes recorded and should return the intact array, a modified version of the array, or if custom formatting is desired, a stringified version of the array to save to the fixture
- `recorder`: custom options to pass to the recorder

##### Example
Expand Down
3 changes: 2 additions & 1 deletion lib/back.js
Expand Up @@ -171,7 +171,8 @@ const record = {
outputs = options.afterRecord(outputs)
}

outputs = JSON.stringify(outputs, null, 4)
outputs =
typeof outputs === 'string' ? outputs : JSON.stringify(outputs, null, 4)
debug('recorder outputs:', outputs)

mkdirp.sync(path.dirname(fixture))
Expand Down
42 changes: 42 additions & 0 deletions tests/test_back.js
Expand Up @@ -408,6 +408,48 @@ test('nockBack record tests', nw => {
})
nw.end()
})

nw.test('it can format after recording', t => {
const fixture = 'filteredFixture.json'
const fixtureLoc = `${nockBack.fixtures}/${fixture}`

t.false(exists(fixtureLoc))

const afterRecord = scopes => 'string-response'

nockBack(fixture, { afterRecord }, function(done) {
const server = http.createServer((request, response) => {
t.pass('server received a request')

response.writeHead(200)
response.write('server served a response')
response.end()
})

server.listen(() => {
const request = http.request(
{
host: 'localhost',
path: '/',
port: server.address().port,
},
response => {
done()

t.is(200, response.statusCode)
t.true(exists(fixtureLoc))
t.is(fs.readFileSync(fixtureLoc, 'utf8'), 'string-response')
fs.unlinkSync(fixtureLoc)

server.close(t.end)
}
)
request.on('error', t.error)
request.end()
})
})
nw.end()
})
})

test('nockBack lockdown tests', nw => {
Expand Down

0 comments on commit da840fe

Please sign in to comment.