Profiles·Public

nock

semver>=9.0.0postconditions13functions9last verified2026-04-18coverage score89%

Postconditions — what we check

  • nock · unmatched-request-throws
    error
    WhenAn HTTP request is made that does not match any defined interceptor and nock.disableNetConnect() has been called
    ThrowsNetConnectNotAllowedError (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
    Sources[1][2]
  • disableNetConnect · real-requests-blocked
    info
    Whencalled without arguments
    Returnsundefined; all subsequent unmatched HTTP requests will throw
    Required handlingNo action required — use the returned value as needed.
    costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
    Sources[1]
  • cleanAll · all-interceptors-removed
    info
    Whencalled at any point
    Returnsundefined; all pending interceptors are cleared
    Required handlingNo action required — use the returned value as needed.
    costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
    Sources[3]
  • restore · interception-deactivated
    info
    Whencalled after interception is no longer needed
    Returnsundefined; http module is restored to its original state
    Required handlingNo action required — use the returned value as needed.
    costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
    Sources[4]
  • isDone · returns-false-for-unused-mocks
    warning
    Whenone or more interceptors on the scope have not been matched by any request
    Returnsfalse
    Required 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
    Sources[5][6]
  • isDone · returns-true-when-all-consumed
    info
    Whenall interceptors on the scope have been matched at least once
    Returnstrue
    Required handlingNo action required — use the returned value as needed.
    costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
    Sources[5]
  • load · load-file-not-found
    error
    WhenThe path argument does not point to an existing file, or the process lacks read permission for the file
    ThrowsError 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
    Sources[7][8]
  • load · load-invalid-json
    error
    WhenThe fixture file exists but contains invalid JSON (e.g. truncated recording, manual edit error, encoding issue)
    ThrowsSyntaxError 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 unavailablevisibilityvisible
    Sources[7]
  • loadDefs · loaddefs-file-not-found
    error
    WhenThe path argument does not point to an existing file
    ThrowsError with code ENOENT from fs.readFileSync
    Required 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 unavailablevisibilityvisible
    Sources[7]
  • Scope.done · scope-done-unused-mocks-throw
    error
    WhenOne or more interceptors on the scope have not been matched by a real HTTP request when scope.done() is called
    ThrowsAssertionError: "Mocks not yet satisfied:\n<method> <url>" — lists each pending mock
    Required 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 unavailablevisibilityvisible
    Sources[5]
  • Scope.done · scope-done-swallowed-in-async-callback
    warning
    Whenscope.done() is called inside an async callback (setTimeout, promise .then, or event handler) without the test framework being aware of the async assertion
    Required 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 unavailablevisibilitysilent
    Sources[5]
  • back · back-fixtures-not-set
    error
    Whennock.back.fixtures has not been set to a directory path before calling nock.back()
    ThrowsError: "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 unavailablevisibilityvisible
    Sources[9]
  • back · back-unknown-mode
    error
    Whennock.back.setMode() is called with a string that is not one of 'wild', 'dryrun', 'record', 'update', or 'lockdown'
    ThrowsError — "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 unavailablevisibilityvisible
    Sources[9]

Sources

Every postcondition cites at least one of these. Numbered to match the footnotes above.

  1. [1]github.com/nock/nockhttps://github.com/nock/nock#disabling-real-http-requests
  2. [2]github.com/nock/nockhttps://github.com/nock/nock/issues/884
  3. [3]github.com/nock/nockhttps://github.com/nock/nock#cleanall
  4. [4]github.com/nock/nockhttps://github.com/nock/nock#restoring-http-intercepting
  5. [5]github.com/nock/nockhttps://github.com/nock/nock#asserting-expectations-after-execution
  6. [6]snyk.io/advisor/npm-packagehttps://snyk.io/advisor/npm-package/nock/functions/nock.isDone
  7. [7]github.com/nock/nockhttps://github.com/nock/nock#saving-and-loading-interceptors
  8. [8]nodejs.org/api/fs.htmlhttps://nodejs.org/api/fs.html#fsreadfilesyncpath-options
  9. [9]github.com/nock/nockhttps://github.com/nock/nock#nock-back
  10. [10]github.com/nock/nockhttps://github.com/nock/nock/issues/2077
Need a different package?
Request a profile