From 7c6dfec3b29c4add0627457c067a131a61464806 Mon Sep 17 00:00:00 2001 From: Phil Edwards Date: Tue, 16 Jul 2024 16:56:36 -0400 Subject: [PATCH] =?UTF-8?q?Make=20tests=20run=20in=20Node=20>=3D17.=20Wow?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Here's the error we were encountering when running mocha-chrome on Node >=17: ``` Promise Rejection: Error: connect ECONNREFUSED ::1:39193 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '::1', port: 39193 } ``` Here's the patch that fixes this for us: // node_modules/mocha-chrome/lib/client.js - const client = await CDP({ port: instance.port }); + const client = await CDP({ port: instance.port, host: '127.0.0.1' }); An alternative patch that would also succeed: // node_modules/chrome-remote-interface/lib/defaults.js - module.exports.HOST = 'localhost'; + module.exports.HOST = '127.0.0.1'; Another option: // node_modules/chrome-remote-interface/lib/external-request.js - const {address} = await util.promisify(dns.lookup)(options.host); + const {address} = await util.promisify(dns.lookup)( + options.host, + {family:'IPv4'}, + ); Thank you @nsainaney for writing this comment in nodejs/node#40702 [^1]: > It appears to be a breaking change with how DNS.lookup works. With > node 16, the lookup would return a IPv4 address but with node 17, it > returns an IPv6 address which will break most REST clients that > hardcode URLS like http://localhost:4040/api if the upstream server > only binds to the IPv4 address (e.g. server.listen('127.0.0.1'…) etc… [^1]: https://github.com/nodejs/node/issues/40702#issuecomment-958143154 Before committing to the 'patch' strategy, I checked to see if either mocha-chrome or chrome-remote-interface had been updated later with this workaround. - chrome-remote-interface accepts a parameter for host, so nope. Makes sense, since you can specify the host manually. - mocha-chrome doesn't parameterize host, and the author of mocha-chrome considers it an obsolete package and is no longer updating it. --- patches/mocha-chrome+2.2.0.patch | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 patches/mocha-chrome+2.2.0.patch diff --git a/patches/mocha-chrome+2.2.0.patch b/patches/mocha-chrome+2.2.0.patch new file mode 100644 index 0000000000..147d8086a1 --- /dev/null +++ b/patches/mocha-chrome+2.2.0.patch @@ -0,0 +1,13 @@ +diff --git a/node_modules/mocha-chrome/lib/client.js b/node_modules/mocha-chrome/lib/client.js +index 7629126..cd3d163 100644 +--- a/node_modules/mocha-chrome/lib/client.js ++++ b/node_modules/mocha-chrome/lib/client.js +@@ -9,7 +9,7 @@ module.exports = async function connectClient(instance, log, options) { + return fs.readFileSync(filePath, 'utf-8'); + } + +- const client = await CDP({ port: instance.port }); ++ const client = await CDP({ port: instance.port, host: '127.0.0.1' }); + const { DOM, DOMStorage, Console, Network, Page, Runtime } = client; + const mochaOptions = `window.mochaOptions = ${JSON.stringify(options.mocha)}`; +