Skip to content

Nock causes error when using a HTTPS_PROXY #2923

@SanderDeclerck

Description

@SanderDeclerck

Please avoid duplicates

Reproducible test case

https://github.com/SanderDeclerck/safe-chain-nock-bug

Nock Version

14.0.10

Node Version

v24.4.1

TypeScript Version

No response

What happened?

When the environment variable HTTPS_PROXY is set, it causes errors if nock is being used. The tests without nock run fine, but after requiring nock, tests start to fail.

In the linked repo, a minimal reproducible example can be found:

  • tinyproxy is setup in the github actions workflow
  • tests are run with HTTPS_PROXY set

dependencies

  • nock@14.0.10
  • axios@1.13.0

index.test.js

// Function under test
async function fetchData() {
  const axios = require("axios");

  const response = await axios.get("https://example.com/");

  return response.status;
}

// Tests
describe("HTTP request test", () => {
  test("without nock", async () => {
    const result = await fetchData();

    expect(result).toBe(200);
  }, 20000);

  test("with nock", async () => {
    const nock = require("nock");
    nock.disableNetConnect();
    nock("https://example.com").get("/").reply(200);

    const result = await fetchData();

    expect(result).toBe(200);
  }, 20000);
});

test.yaml (GH Actions workflow)

name: Run Tests

on:
  push:
    branches: ["*"]
  pull_request:
    branches: ["*"]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup tinyproxy
        run: |
          sudo apt-get update
          sudo apt-get install -y tinyproxy
          echo "Port 8888" | sudo tee /tmp/tinyproxy.conf
          echo "Listen 127.0.0.1" | sudo tee -a /tmp/tinyproxy.conf
          echo "Timeout 600" | sudo tee -a /tmp/tinyproxy.conf
          sudo tinyproxy -c /tmp/tinyproxy.conf -d &
          sleep 2

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: "npm"

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: |
          # Ensure tinyproxy is fully started
          sleep 2
          # Verify that the proxy is working
          curl -x http://127.0.0.1:8888 https://example.com/
          # Run tests with proxy environment variables
          npm test
        env:
          HTTPS_PROXY: http://127.0.0.1:8888

Test output


FAIL ./index.test.js
  HTTP request test
    ✓ without nock (2006 ms)
    ✕ with nock (22 ms)

  ● HTTP request test › with nock

    TypeError: Invalid URL

      2 |   const axios = require("axios");
      3 |
    > 4 |   const response = await axios.get("https://example.com/");
        |                                ^
      5 |
      6 |   return response.status;
      7 | }

      at getUrlByRequestOptions (node_modules/@mswjs/interceptors/src/utils/getUrlByRequestOptions.ts:145:15)
      at normalizeClientRequestArgs (node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts:221:11)
      at Object.apply (node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/index.ts:61:42)
      at RedirectableRequest.Object.<anonymous>.RedirectableRequest._performRequest (node_modules/follow-redirects/index.js:337:24)
      at new RedirectableRequest (node_modules/follow-redirects/index.js:111:8)
      at Object.request (node_modules/follow-redirects/index.js:543:14)
      at dispatchHttpRequest (node_modules/axios/lib/adapters/http.js:660:21)
      at node_modules/axios/lib/adapters/http.js:255:5
      at wrapAsync (node_modules/axios/lib/adapters/http.js:235:10)
      at http (node_modules/axios/lib/adapters/http.js:313:10)
      at Axios.dispatchRequest (node_modules/axios/lib/core/dispatchRequest.js:51:10)
      at Axios._request (node_modules/axios/lib/core/Axios.js:185:33)
      at Axios.request (node_modules/axios/lib/core/Axios.js:40:25)
      at Axios.<computed> [as get] (node_modules/axios/lib/core/Axios.js:211:17)
      at Function.wrap [as get] (node_modules/axios/lib/helpers/bind.js:12:15)
      at get (index.js:4:32)
      at Object.fetchData (index.test.js:15:26)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        2.367 s
Ran all test suites.
Error: Process completed with exit code 1.

Would you be interested in contributing a fix?

  • yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions