Files
hyperframes/packages/producer/scripts/no-network.mjs
Miguel Ángel 04736006b1 test(producer): unit tests never reach the network (#5135)
* test(producer): unit tests never reach the network

* test(producer): the unit lane refuses sockets to other hosts, not only fetch

* test(producer): refuse outside hosts at the lookup, not by parsing socket calls

* test(producer): keep dns.lookup promisify-compatible under the no-network guard
2026-10-07 00:52:01 +00:00

17 lines
797 B
JavaScript

import dns from "node:dns";
// Unit tests run offline: fetch throws, and a lookup of any host but this machine fails, which also covers a test
// environment's own fetch (happy-dom's) and node:http. A test that needs a response stubs fetch itself.
const LOCAL = new Set(["localhost"]);
const refusal = (target) => new TypeError(`No network in unit tests: ${target}`);
globalThis.fetch = async (input) => {
throw refusal(input?.url ?? input);
};
const { lookup } = dns;
const guarded = (host, ...rest) =>
LOCAL.has(host) ? lookup(host, ...rest) : process.nextTick(rest.at(-1), refusal(host));
// Keeps the original's own properties, among them what util.promisify reads to resolve { address, family }.
dns.lookup = Object.defineProperties(guarded, Object.getOwnPropertyDescriptors(lookup));