Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .eslintignore

This file was deleted.

49 changes: 0 additions & 49 deletions .eslintrc.yml

This file was deleted.

1 change: 0 additions & 1 deletion .github/workflows/continuous-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ jobs:
fail-fast: false
matrix:
node-version:
- 18
- 20
- 22
os:
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
.vscode
coverage
node_modules
npm-debug.log
tests/browserify-public/browserify-bundle.js
npm-debug.log
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package-lock.json
tsconfig.json
/.nyc_output
/coverage
/tests/browserify-public/browserify-bundle.js
migration_guides
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ Nock’s changelog can be found directly in the [GitHub release notes](https://g
These are automatically created by [semantic-release](https://github.com/semantic-release/semantic-release) based on their [commit message conventions](https://semantic-release.gitbook.io/semantic-release#commit-message-format).

Migration guides are available for major versions in the [migration guides directory](https://github.com/nock/nock/tree/main/migration_guides).

This release aim to modernize and refresh `nock`, remove long-supported hacks and create more expected library.

TODO:

1. remove @definitelytyped/dtslint and generate the types instead. maybe use tools like tsd to test the types (not sure if needed in case we generate the types)
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ nock.removeInterceptor({
hostname: 'localhost',
path: '/mockedResource',
// method defaults to `GET`
// proto defaullts to `http`
// proto defaults to `http`
})
```

Expand Down Expand Up @@ -1353,9 +1353,39 @@ A scope emits the following events:
You can also listen for no match events like this:

```js
nock.emitter.on('no match', req => {})
nock.emitter.on('no match', (req, interceptorResults) => {
console.log('Request did not match any interceptors:', req.url)

if (interceptorResults && interceptorResults.length > 0) {
interceptorResults.forEach(({ interceptor, reasons }) => {
console.log(
'Interceptor:',
interceptor.method,
interceptor.basePath + interceptor.path,
)
console.log('Reasons:', reasons)
})
}
})
```

The callback receives two parameters:

- `req` - The request object that didn't match
- `interceptorResults` - An array of objects containing detailed information about each interceptor that was tested.
Each object contains:
- `interceptor` - The interceptor that was tested against the request
- `reasons` - An array of strings describing why the request didn't match this interceptor
> ⚠️ **Experimental**: The structure and format of the detailed mismatch information may change in future versions as we gather user feedback and refine the API. The feature itself is stable and ready for use and we're seeking community input on the API design before marking it stable.

Common mismatch reasons include:

- **Method mismatch**: `"Method mismatch: expected GET, got POST"`
- **Path mismatch**: `"Path mismatch: expected /api/users, got /api/posts"`
- **Header mismatch**: `"Header mismatch: expected authorization to match Bearer token, got null"`
- **Body mismatch**: `"Body mismatch: expected "expected body", got actual body"`
- **Query mismatch**: `"query matching failed"`

## Nock Back

Fixture recording support and playback.
Expand Down
36 changes: 36 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import js from "@eslint/js";
import globals from "globals";
import { defineConfig } from "eslint/config";
import prettier from "eslint-config-prettier/flat";
import mocha from "eslint-plugin-mocha";
import node from "eslint-plugin-n";

export default defineConfig([
{
files: ["**/*.js"],
plugins: { js },
extends: ["js/recommended"],
languageOptions: { globals: globals.node, sourceType: 'commonjs' },
ignores: ['node_modules', 'coverage'],
},
{
plugins: { n: node },
extends: [node.configs["flat/recommended"]],
rules: {
'n/no-unsupported-features/node-builtins': ['error', { allowExperimental: true }],
'n/prefer-node-protocol': 'error',
}
},
{
plugins: { mocha },
extends: [mocha.configs.recommended],
files: ["tests/**/*.js"],
rules: {
"mocha/no-pending-tests": "off", // until we will fix all tests
"mocha/no-mocha-arrows": "off",
"mocha/no-identical-title": "off",
"mocha/no-top-level-hooks": "off",
},
},
prettier
]);
6 changes: 3 additions & 3 deletions examples/binary-reply.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const http = require('http')
const fs = require('fs')
const path = require('path')
const http = require('node:http')
const fs = require('node:fs')
const path = require('node:path')

const nock = require('../')

Expand Down
15 changes: 0 additions & 15 deletions examples/delay-connection.js

This file was deleted.

2 changes: 1 addition & 1 deletion examples/delay-response.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const http = require('http')
const http = require('node:http')
const nock = require('../')
const log = require('./_log')
const events = ['socket', 'response', 'end', 'data']
Expand Down
2 changes: 1 addition & 1 deletion examples/net-connect-default-no-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const log = require('./_log')

const events = ['socket', 'response', 'end', 'data', 'error']

const http = require('http')
const http = require('node:http')
console.log('making request...')
const req = http.get('http://www.google.com/', function (res) {
console.log('request result: res.statusCode = %j', res.statusCode)
Expand Down
2 changes: 1 addition & 1 deletion examples/net-connect-default-other-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const nock = require('../')

nock('http://someotherservice.com').get('/').reply(200, 'whaaa')

const http = require('http')
const http = require('node:http')
const req = http.get('http://www.google.com/', function (res) {
console.log('request result: res.statusCode = %j', res.statusCode)
events.forEach(log(res, 'res'))
Expand Down
2 changes: 1 addition & 1 deletion examples/net-connect-disabled-different-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ nock.disableNetConnect()

nock('http://someotherservice.com').get('/').reply(200, 'whaaa')

const http = require('http')
const http = require('node:http')
const req = http.get('http://www.google.com/')

req.once('error', function (err) {
Expand Down
2 changes: 1 addition & 1 deletion examples/net-connect-mock-same-host-different-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const nock = require('../')

nock('http://example.com').get('/path').reply(200, 'whaaa')

const http = require('http')
const http = require('node:http')
const req = http.get('http://example.com/other-path')

req.once('error', function (err) {
Expand Down
2 changes: 1 addition & 1 deletion examples/socket-delay-abort.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const http = require('http')
const http = require('node:http')
const nock = require('../')
const log = require('./_log')
const events = ['socket', 'response', 'end', 'data', 'timeout', 'error']
Expand Down
2 changes: 1 addition & 1 deletion examples/socket-delay-no-abort.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const http = require('http')
const http = require('node:http')
const nock = require('../')
const log = require('./_log')
const events = ['socket', 'response', 'end', 'data', 'timeout', 'error']
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
} = require('./lib/intercept')
const recorder = require('./lib/recorder')
const { Scope, load, loadDefs, define } = require('./lib/scope')
const { getDecompressedGetBody } = require('./lib/utils/node')

module.exports = (basePath, options) => new Scope(basePath, options)

Expand All @@ -42,6 +43,7 @@ Object.assign(module.exports, {
},
restore: recorder.restore,
back,
getDecompressedGetBody,
})

// We always activate Nock on import, overriding the globals.
Expand Down
19 changes: 6 additions & 13 deletions lib/back.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const assert = require('assert')
const fs = require('node:fs')
const assert = require('node:assert')
const recorder = require('./recorder')
const {
activate,
Expand All @@ -10,19 +11,11 @@ const {
} = require('./intercept')
const { loadDefs, define } = require('./scope')
const { back: debug } = require('./debug')
const { format } = require('util')
const path = require('path')
const { format } = require('node:util')
const path = require('node:path')

let _mode = null

let fs

try {
fs = require('fs')
} catch (err) {
// do nothing, probably in browser
}

/**
* nock the current function with the fixture given
*
Expand Down Expand Up @@ -198,7 +191,7 @@ const update = {
return context
},

finish: function (fixture, options, context) {
finish: function (fixture, options) {
let outputs = recorder.outputs()

if (typeof options.afterRecord === 'function') {
Expand Down Expand Up @@ -271,7 +264,7 @@ function load(fixture, options) {
return context
}

function removeFixture(fixture, options) {
function removeFixture(fixture) {
const context = {
scopes: [],
assertScopesFinished: function () {},
Expand Down
Loading
Loading