Skip to content

Commit 7167eb2

Browse files
committed
test: increase coverage for dns.promises.lookup()
Add coverage for uv_getaddrinfo() returning an error. PR-URL: #27299 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 0fc27f6 commit 7167eb2

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

test/parallel/test-dns-lookup.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ const common = require('../common');
44
const assert = require('assert');
55
const { internalBinding } = require('internal/test/binding');
66
const cares = internalBinding('cares_wrap');
7+
8+
// Stub `getaddrinfo` to *always* error. This has to be done before we load the
9+
// `dns` module to guarantee that the `dns` module uses the stub.
10+
cares.getaddrinfo = () => internalBinding('uv').UV_ENOMEM;
11+
712
const dns = require('dns');
813
const dnsPromises = dns.promises;
914

10-
// Stub `getaddrinfo` to *always* error.
11-
cares.getaddrinfo = () => internalBinding('uv').UV_ENOENT;
12-
1315
{
1416
const err = {
1517
code: 'ERR_INVALID_ARG_TYPE',
@@ -144,15 +146,19 @@ dns.lookup('127.0.0.1', {
144146

145147
let tickValue = 0;
146148

149+
// Should fail due to stub.
147150
dns.lookup('example.com', common.mustCall((error, result, addressType) => {
148151
assert(error);
149152
assert.strictEqual(tickValue, 1);
150-
assert.strictEqual(error.code, 'ENOENT');
153+
assert.strictEqual(error.code, 'ENOMEM');
151154
const descriptor = Object.getOwnPropertyDescriptor(error, 'message');
152155
// The error message should be non-enumerable.
153156
assert.strictEqual(descriptor.enumerable, false);
154157
}));
155158

156-
// Make sure that the error callback is called
157-
// on next tick.
159+
// Make sure that the error callback is called on next tick.
158160
tickValue = 1;
161+
162+
// Should fail due to stub.
163+
assert.rejects(dnsPromises.lookup('example.com'),
164+
{ code: 'ENOMEM', hostname: 'example.com' });

0 commit comments

Comments
 (0)