Skip to content

Commit 24e5b47

Browse files
parisholleypaulmelnikow
authored andcommitted
feat: Add conditionally() (#1488)
I am building a test framework which depends on nock and to support concurrency, I need to be able shutoff certain scopes (to prevent cross-pollination) when running tests concurrently.
1 parent d6667f0 commit 24e5b47

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,18 @@ const scope = nock('https://api.dropbox.com', {
757757
.reply(200)
758758
```
759759

760+
### Conditional scope filtering
761+
762+
You can also choose to filter out a scope based on your system environment (or any external factor). The filtering function is accepted at the `conditionally` field of the `options` argument.
763+
764+
This can be useful if you only want certain scopes to apply depending on how your tests are executed.
765+
766+
```js
767+
const scope = nock('https://api.myservice.com', {
768+
conditionally: () => true,
769+
})
770+
```
771+
760772
### Path filtering
761773

762774
You can also filter the URLs based on a function.

lib/interceptor.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
254254
return false
255255
}
256256

257+
if (
258+
this.scope.scopeOptions &&
259+
this.scope.scopeOptions.conditionally &&
260+
!this.scope.scopeOptions.conditionally()
261+
) {
262+
return false
263+
}
264+
257265
function reqheaderContains(header) {
258266
return _.has(options.headers, header)
259267
}

tests/test_intercept.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const mikealRequest = require('request')
88
const superagent = require('superagent')
99
const needle = require('needle')
1010
const restify = require('restify-clients')
11+
const assertRejects = require('assert-rejects')
1112
const hyperquest = require('hyperquest')
1213
const got = require('got')
1314
const nock = require('..')
@@ -1228,6 +1229,36 @@ test('test request timeout option', t => {
12281229
})
12291230
})
12301231

1232+
test('do not match when conditionally = false but should match after trying again when = true', async t => {
1233+
t.plan(2)
1234+
let enabled = false
1235+
1236+
const scope = nock('http://example.test', {
1237+
conditionally: function() {
1238+
return enabled
1239+
},
1240+
})
1241+
.get('/')
1242+
.reply(200)
1243+
1244+
// now the scope has been used, should fail on second try
1245+
await assertRejects(
1246+
got('http://example.test/'),
1247+
Error,
1248+
'Nock: No match for request'
1249+
)
1250+
t.throws(() => scope.done(), {
1251+
message: 'Mocks not yet satisfied',
1252+
})
1253+
1254+
enabled = true
1255+
1256+
const { statusCode } = await got('http://example.test/')
1257+
1258+
t.equal(statusCode, 200)
1259+
scope.done()
1260+
})
1261+
12311262
test('get correct filtering with scope and request headers filtering', t => {
12321263
const responseText = 'OK!'
12331264
const responseHeaders = { 'Content-Type': 'text/plain' }

0 commit comments

Comments
 (0)