nock / nock

HTTP server mocking and expectations library for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DNS resolve errors and not mocking a lot of requests

b-brefeld opened this issue · comments

Please avoid duplicates

Reproducible test case

Versions not available on RunKit

Nock Version

13.5.1

Node Version

20.9.0

TypeScript Version

Version: 5.3.3

{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"moduleResolution": "node",
"module": "es2020",
"target": "es2020",
"strict": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strictPropertyInitialization": true,
"noImplicitAny": true,
"rootDir": "tests",
"outDir": "compiled-tests"
},
"references": [{ "path": "../" }]
}

What happened?

After updating to 13.5.1 all my fake hostnames failed resolving through node DNS. I'm using nock to mock hostnames that would be present within my docker compose network.
Additionally, nock fails to mock an external hostname when trying to fix the issue in several ways.
Applying nock.disableNetConnect(); gets ignored completely.

The following nock would result in a DNS lookup error on 13.5.1 but not on 13.5.0:
nock('http://tokens-service', { reqheaders: { 'tenant': 'test', }, }) .get('/api/tokens/users/14') .reply(200, { response_code: 'RES.T.API.02.01', payload: [ { id: '33', user_id: '14', client_id: 22, token: 'exampleToken', token_valid_till: '2024-02-23T12:53:20Z', created_at: '2021-01-01T15:15:15Z', }, ], })

Reverting to 13.5.0 fixed all my issues.

Aditionally, having nodemailer active as a package will fix the DNS lookup errors (still have no clue what causes this).
Having the following nodemailer code in a file fixed the DNS issues:
createTransport({ host: process.env.mail_host, port: parseInt(process.env.mail_outgoing_port!, 10), secure: true, auth: { user: process.env.mail_username, pass: process.env.mail_password, }, });

I was unable to create an example in RunKit.
If the above information isn't enough I can provide an example later in a public repo when I have more time.
Please contact me if this is required.

Would you be interested in contributing a fix?

  • yes

@VladimirChuprazov

Here is a repo that reproduces the issue as requested:
https://bitbucket.org/brefeld-computers/nock-2575/src/main/

After cloning the repo run the following command:
npm install && npm run build && npm test
The test should fail.

Now change the "nock" version number to 13.5.0 in the package.json and run command:
npm run clean && npm install && npm run build && npm test
The test should now succeed.

If you need more info, you know where to find me

You didn't end your request in your example. This is working for me:

request({method: 'GET', host: hostname, path: pathname, timeout: 5000}, (res) => {
    console.log(res.statusCode)
}).end();

Dear Michael,

Thanks for your info.
I changed the code and tried again. (see: link)
I still have a failing test with nock on 13.5.1

Screenshot when using nock 13.5.0
Screenshot from 2024-02-07 15-38-57

Screenshot when using nock 13.5.1
Screenshot from 2024-02-07 15-32-06

You tried both versions and don't have this behavior?
I'm running on Ubuntu 22.04 if it makes any difference

@b-brefeld This is because request is async. You should await for the response.

@mikicho request does not return a Promise and I can't use await for it.
However I changed the code again to allow awaiting a response.
Please see the link

I still experience the same behaviour

@b-brefeld are you using ESM?
If yes, your example imports the http module before nock does due to how ESM resolves and runs the code.
You can work around this like this:

import nock from 'nock';
const { request } = await import('http');

Another option is to import nock before:

node --import nock test.js

But, I recommend you use 3rd party libraries like axios or got instead of directly using the http module, which would probably solve this without your notice.

Hi @b-brefeld I just checked your reproduction repo, as @mikicho suggested, with both axios and got it works fine, I tried it.
Another option which @mikicho suggested is to run node --import nock test.js, if you want to run all your tests you can try:

node --import nock node_modules/.bin/jasmine

it works in your repo.