From 96f36fe5abfc08411d74b7e40603bf6e7fb4c455 Mon Sep 17 00:00:00 2001 From: farisbasha Date: Wed, 18 Nov 2026 12:19:29 +0530 Subject: [PATCH 1/2] test_runner: remove stray quotes from module mock error The generated CommonJS mock source threw new Error('${JSONStringify(kBadExportsMessage)}'). JSONStringify already returns a quoted string literal, so the extra single quotes made the double quotes part of the message: "Cannot create mock because ...". Pass the JSON literal directly and check the start of the message in the test. Signed-off-by: farisbasha --- lib/internal/test_runner/mock/loader.js | 2 +- test/parallel/test-runner-module-mocking.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/test_runner/mock/loader.js b/lib/internal/test_runner/mock/loader.js index 266525c8d52a..49b52162b34c 100644 --- a/lib/internal/test_runner/mock/loader.js +++ b/lib/internal/test_runner/mock/loader.js @@ -124,7 +124,7 @@ function namedExportsSource(useESM, exportNames) { if (!useESM && exportNames.length > 0) { source += ` if (module.exports === null || typeof module.exports !== 'object') { - throw new Error('${JSONStringify(kBadExportsMessage)}'); + throw new Error(${JSONStringify(kBadExportsMessage)}); } `; } diff --git a/test/parallel/test-runner-module-mocking.js b/test/parallel/test-runner-module-mocking.js index 7f2c76663ba2..7db4d71d8e30 100644 --- a/test/parallel/test-runner-module-mocking.js +++ b/test/parallel/test-runner-module-mocking.js @@ -394,7 +394,7 @@ test('ESM mocking with namedExports option', async (t) => { }); await assert.rejects(async () => { await import(fixture); - }, /Cannot create mock/); + }, { message: /^Cannot create mock/ }); t.mock.reset(); assert.strictEqual(original, await import(fixture)); From f7a0dac68f804608c626cbe857fd879bec73a154 Mon Sep 17 00:00:00 2001 From: farisbasha Date: Fri, 20 Nov 2026 00:58:33 +0530 Subject: [PATCH 2/2] test_runner: stringify the bad exports message once Signed-off-by: farisbasha --- lib/internal/test_runner/mock/loader.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/internal/test_runner/mock/loader.js b/lib/internal/test_runner/mock/loader.js index 49b52162b34c..e907819ce69b 100644 --- a/lib/internal/test_runner/mock/loader.js +++ b/lib/internal/test_runner/mock/loader.js @@ -7,6 +7,7 @@ const { const kMockSearchParam = 'node-test-mock'; const kBadExportsMessage = 'Cannot create mock because named exports ' + 'cannot be applied to the provided default export.'; +const kBadExportsMessageSource = JSONStringify(kBadExportsMessage); const { URL, URLParse } = require('internal/url'); let debug = require('internal/util/debuglog').debuglog('test_runner', (fn) => { @@ -124,7 +125,7 @@ function namedExportsSource(useESM, exportNames) { if (!useESM && exportNames.length > 0) { source += ` if (module.exports === null || typeof module.exports !== 'object') { - throw new Error(${JSONStringify(kBadExportsMessage)}); + throw new Error(${kBadExportsMessageSource}); } `; }