nock
semver
>=9.0.0postconditions13functions9last verified2026-04-18coverage score89%Postconditions — what we check
- nock · unmatched-request-throwserrorWhenAn HTTP request is made that does not match any defined interceptor and nock.disableNetConnect() has been calledThrows
NetConnectNotAllowedError (error.code === 'ENETUNREACH')Required handlingTest assertions MUST account for this error. The most common cause is a URL path, method, query parameter, header, or body mismatch between the real request and the mock definition. Fix the mock to match the actual request, or allow the specific host with nock.enableNetConnect(host).costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - disableNetConnect · real-requests-blockedinfoWhencalled without argumentsReturnsundefined; all subsequent unmatched HTTP requests will throwRequired handlingNo action required — use the returned value as needed.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1]
- cleanAll · all-interceptors-removedinfoWhencalled at any pointReturnsundefined; all pending interceptors are clearedRequired handlingNo action required — use the returned value as needed.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[3]
- restore · interception-deactivatedinfoWhencalled after interception is no longer neededReturnsundefined; http module is restored to its original stateRequired handlingNo action required — use the returned value as needed.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[4]
- isDone · returns-false-for-unused-mockswarningWhenone or more interceptors on the scope have not been matched by any requestReturnsfalseRequired handlingTests SHOULD assert scope.isDone() after the code under test runs. A false return indicates the code did not make the expected request, the mock URL/method/body does not match the real request, or there is a bug in the test logic.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
- isDone · returns-true-when-all-consumedinfoWhenall interceptors on the scope have been matched at least onceReturnstrueRequired handlingNo action required — use the returned value as needed.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[5]
- load · load-file-not-founderrorWhenThe path argument does not point to an existing file, or the process lacks read permission for the fileThrows
Error with code ENOENT from fs.readFileSync — "no such file or directory, open '<path>'"Required handlingWrap nock.load() in a try-catch. A missing fixture file means all interceptors are absent and all HTTP calls in the test will fail or hit the real network. Test setup MUST validate the fixture path exists before calling load().costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - load · load-invalid-jsonerrorWhenThe fixture file exists but contains invalid JSON (e.g. truncated recording, manual edit error, encoding issue)Throws
SyntaxError from JSON.parse — "Unexpected token ... in JSON at position N"Required handlingValidate fixture files are well-formed JSON before committing them. A SyntaxError from nock.load() crashes the entire test suite setup (beforeAll/beforeEach fails), causing all tests in the suite to error rather than fail, making the root cause harder to diagnose.costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[7] - loadDefs · loaddefs-file-not-founderrorWhenThe path argument does not point to an existing fileThrows
Error with code ENOENT from fs.readFileSyncRequired handlingSame as nock.load() — wrap in try-catch. Missing fixture means all definitions are absent. Callers that transform definitions before passing to nock.define() must guard against this error in the transformation pipeline.costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[7] - Scope.done · scope-done-unused-mocks-throwerrorWhenOne or more interceptors on the scope have not been matched by a real HTTP request when scope.done() is calledThrows
AssertionError: "Mocks not yet satisfied:\n<method> <url>" — lists each pending mockRequired handlingCall scope.done() only after the code under test has had the opportunity to make all expected HTTP calls. If done() throws, the test has a logic error: either the code under test did not make the expected request, or the mock URL/method/body does not match the actual request. Do not suppress the AssertionError — it is an intentional test signal.costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[5] - Scope.done · scope-done-swallowed-in-async-callbackwarningWhenscope.done() is called inside an async callback (setTimeout, promise .then, or event handler) without the test framework being aware of the async assertionRequired handlingAlways call scope.done() synchronously at the end of the test body, or ensure the test framework is awaiting async assertions (use expect.assertions(n) in Jest, or return the promise from the test function).costlowin prodimmediate exceptionusers seeservice unavailablevisibilitysilentSources[5]
- back · back-fixtures-not-seterrorWhennock.back.fixtures has not been set to a directory path before calling nock.back()Throws
Error: "Back requires nock.back.fixtures to be set\n\tnock.back.fixtures = '/path/to/fixtures/'"Required handlingSet nock.back.fixtures = path.join(__dirname, 'fixtures') in a top-level beforeAll hook before any test that uses nock.back(). Missing this causes an immediate throw (not a rejected Promise) — the error surfaces synchronously during the call.costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[9] - back · back-unknown-modeerrorWhennock.back.setMode() is called with a string that is not one of 'wild', 'dryrun', 'record', 'update', or 'lockdown'Throws
Error — "Unknown mode: <value>"Required handlingUse only the five documented BackMode values. The mode is typically set from an environment variable (process.env.NOCK_BACK_MODE). Validate the env var value against the allowed set before passing to setMode().costlowin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[9]
Sources
Every postcondition cites at least one of these. Numbered to match the footnotes above.
- [1]github.com/nock/nockhttps://github.com/nock/nock#disabling-real-http-requests
- [2]github.com/nock/nockhttps://github.com/nock/nock/issues/884
- [3]github.com/nock/nockhttps://github.com/nock/nock#cleanall
- [4]github.com/nock/nockhttps://github.com/nock/nock#restoring-http-intercepting
- [5]github.com/nock/nockhttps://github.com/nock/nock#asserting-expectations-after-execution
- [6]snyk.io/advisor/npm-packagehttps://snyk.io/advisor/npm-package/nock/functions/nock.isDone
- [7]github.com/nock/nockhttps://github.com/nock/nock#saving-and-loading-interceptors
- [8]nodejs.org/api/fs.htmlhttps://nodejs.org/api/fs.html#fsreadfilesyncpath-options
- [9]github.com/nock/nockhttps://github.com/nock/nock#nock-back
- [10]github.com/nock/nockhttps://github.com/nock/nock/issues/2077
Need a different package?
Request a profile