From f5a2c4c7d5809eaf6aec0c331688e49b1043fba1 Mon Sep 17 00:00:00 2001 From: Ross Brandes Date: Mon, 12 Oct 2026 11:20:36 -0400 Subject: [PATCH 1/2] Honor ignoreCallsFromThisFile() for ES modules with a quibble query Fixes #99 quibble.esm() and quibble.esmImportWithPath() compared raw ESM stack-frame URLs against the ignore list. A wrapper imported while a mock existed has a `?__quibble=N` URL, so it was never skipped and specifiers resolved from the wrapper's directory instead of its caller's. Drop the query and hash from file: stack-frame URLs, as fileURLToPath() already does for path-mode callers. Also let ignoreCallsFromThisFile() take a file: URL such as import.meta.url, which the first change would otherwise stop matching. --- CHANGELOG.md | 5 ++++ index.d.ts | 2 +- lib/quibble.js | 12 ++++++++ .../a-quibble-esm-wrapper-ignored-by-url.mjs | 12 ++++++++ ...bble-esm-wrapper-imported-under-a-mock.mjs | 10 +++++++ test/esm-lib/quibble-esm.test.mjs | 29 +++++++++++++++++++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 test/esm-fixtures/a-quibble-esm-wrapper-ignored-by-url.mjs create mode 100644 test/esm-fixtures/a-quibble-esm-wrapper-imported-under-a-mock.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a3dbb9..b6ecd2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ * Importing a module that doesn't exist (and isn't mocked) now fails with Node's `ERR_MODULE_NOT_FOUND` instead of `ENOENT` [#100](https://github.com/testdouble/quibble/issues/100) +* Fix `ignoreCallsFromThisFile()` not being honored by `quibble.esm()` and + `quibble.esmImportWithPath()` when the ignored ES module was imported while + a mock existed [#99](https://github.com/testdouble/quibble/issues/99) +* `ignoreCallsFromThisFile()` now accepts a `file:` URL such as + `import.meta.url` # 0.12.1 diff --git a/index.d.ts b/index.d.ts index f7b00d3..6b558ae 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,7 +4,7 @@ export type QuibbleConfig = { declare const quibble: ((request: string, stub?: any) => any) & { config(userConfig: Partial): QuibbleConfig; - ignoreCallsFromThisFile(file?: string): void; + ignoreCallsFromThisFile(file?: string | URL): void; reset(hard?: boolean): void; absolutify(relativePath: string, parentFileName?: string): string; esm( diff --git a/lib/quibble.js b/lib/quibble.js index f0fc526..f3fdd99 100644 --- a/lib/quibble.js +++ b/lib/quibble.js @@ -55,6 +55,9 @@ config = quibble.config() quibble.ignoreCallsFromThisFile = function (file) { if (file == null) { file = hackErrorStackToGetCallerFile(false) + } else if (String(file).startsWith('file:')) { + // e.g. import.meta.url, which may carry a `?__quibble=` query + file = fileURLToPath(file) } ignoredCallerFiles = _.uniq(ignoredCallerFiles.concat(file, pathToFileURL(file).href)) } @@ -285,6 +288,15 @@ function convertStackUrlToPath (fileUrl) { function convertStackPathToUrl (filePath) { if (path.isAbsolute(filePath)) { return pathToFileURL(filePath).href + } else if (filePath.startsWith('file:')) { + // ESM frames are URLs, and quibble's loader tags the URLs of modules it + // re-imports with `?__quibble=`. Drop it (and any other + // query or hash) so they match the files in ignoredCallerFiles, just as + // fileURLToPath() does for convertStackUrlToPath() + const url = new URL(filePath) + url.search = '' + url.hash = '' + return url.href } else { return filePath } diff --git a/test/esm-fixtures/a-quibble-esm-wrapper-ignored-by-url.mjs b/test/esm-fixtures/a-quibble-esm-wrapper-ignored-by-url.mjs new file mode 100644 index 0000000..bb5f533 --- /dev/null +++ b/test/esm-fixtures/a-quibble-esm-wrapper-ignored-by-url.mjs @@ -0,0 +1,12 @@ +// Ignores itself by URL, like a wrapper passing import.meta.url would. Only +// ever imported while a mock exists, so that URL has a `?__quibble=` query +import quibble from '../../lib/quibble.mjs' + +quibble.ignoreCallsFromThisFile(import.meta.url) + +export const url = import.meta.url + +export const absolutify = (relativePath) => quibble.absolutify(relativePath) + +export default (specifier, namedExportStubs, defaultExportStub) => + quibble.esm(specifier, namedExportStubs, defaultExportStub) diff --git a/test/esm-fixtures/a-quibble-esm-wrapper-imported-under-a-mock.mjs b/test/esm-fixtures/a-quibble-esm-wrapper-imported-under-a-mock.mjs new file mode 100644 index 0000000..7e94c32 --- /dev/null +++ b/test/esm-fixtures/a-quibble-esm-wrapper-imported-under-a-mock.mjs @@ -0,0 +1,10 @@ +// Like a-quibble-esm-wrapper.mjs, but only ever imported while a mock +// exists, so quibble's loader gives it a `?__quibble=` URL +import quibble from '../../lib/quibble.mjs' + +quibble.ignoreCallsFromThisFile() + +export const url = import.meta.url + +export default (specifier, namedExportStubs, defaultExportStub) => + quibble.esm(specifier, namedExportStubs, defaultExportStub) diff --git a/test/esm-lib/quibble-esm.test.mjs b/test/esm-lib/quibble-esm.test.mjs index 8a98006..0b6ecdd 100644 --- a/test/esm-lib/quibble-esm.test.mjs +++ b/test/esm-lib/quibble-esm.test.mjs @@ -169,6 +169,35 @@ export default { assert.equal(result.default, 'default-export-replacement') assert.equal(result.namedExport, 'replacement') }, + 'a wrapper that ignores itself is ignored even when its URL has a quibble query': async function () { + // Importing the wrapper while a mock exists tags its URL with + // `?__quibble=`, which has to be ignored too. Without this + // mock nothing gets tagged, so the test would pass on the bug + await quibble.esm('../esm-fixtures/a-module.mjs', undefined, 'unrelated') + const { default: wrappedQuibbleEsm, url } = await import('../esm-fixtures/a-quibble-esm-wrapper-imported-under-a-mock.mjs') + assert.match(url, /[?&]__quibble=/) + + // Only resolvable from this file's directory, not the wrapper's + await wrappedQuibbleEsm('./a-module-next-to-this-test.mjs', { namedExport: 'replacement' }, 'default-export-replacement') + await wrappedQuibbleEsm('a-dependency-of-the-tests', { namedExport: 'replacement' }, 'default-export-replacement') + + const relative = await import('./a-module-next-to-this-test.mjs') + assert.equal(relative.default, 'default-export-replacement') + const bare = await import('a-dependency-of-the-tests') + assert.equal(bare.default, 'default-export-replacement') + }, + 'a wrapper can ignore itself by passing its import.meta.url': async function () { + // Without this mock nothing gets tagged, see the test above + await quibble.esm('../esm-fixtures/a-module.mjs', undefined, 'unrelated') + const { default: wrappedQuibbleEsm, absolutify, url } = await import('../esm-fixtures/a-quibble-esm-wrapper-ignored-by-url.mjs') + assert.match(url, /[?&]__quibble=/) + + assert.equal(absolutify('./a-module-next-to-this-test.mjs'), path.join(path.dirname(__filename), 'a-module-next-to-this-test.mjs')) + + await wrappedQuibbleEsm('./a-module-next-to-this-test.mjs', { namedExport: 'replacement' }, 'default-export-replacement') + const result = await import('./a-module-next-to-this-test.mjs') + assert.equal(result.default, 'default-export-replacement') + }, 'mock a module by a package.json imports specifier (#src/...) only the caller can resolve': async function () { const usesImports = await import('../esm-fixtures/package-with-imports/uses-imports.mjs') await usesImports.mock({ namedExport: 'replacement' }, 'default-export-replacement') From a9a9acc249bc1dc1a176d50707fcc6097248b1fc Mon Sep 17 00:00:00 2001 From: Ross Brandes Date: Mon, 12 Oct 2026 11:28:12 -0400 Subject: [PATCH 2/2] Only expect a quibble-tagged wrapper URL on Node 22+ Before Node 22, a module's import.meta.url and stack frames never carry quibble's `?__quibble=N` query, so the #99 bug can't happen there and the tests' tag check failed on Node 20 in CI. Keep the check on Node 22+, where it guards against the tests silently passing, and run the rest of each test everywhere. Also say in the changelog when the query is there: Node 22+ for a module imported while a mock existed, or a loader that adds one. --- CHANGELOG.md | 5 +++-- test/esm-lib/quibble-esm.test.mjs | 11 +++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6ecd2f..e692d3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,9 @@ Node's `ERR_MODULE_NOT_FOUND` instead of `ENOENT` [#100](https://github.com/testdouble/quibble/issues/100) * Fix `ignoreCallsFromThisFile()` not being honored by `quibble.esm()` and - `quibble.esmImportWithPath()` when the ignored ES module was imported while - a mock existed [#99](https://github.com/testdouble/quibble/issues/99) + `quibble.esmImportWithPath()` when the ignored ES module's URL has a query, + as it does on Node 22+ when the module was imported while a mock existed, or + when another loader adds one [#99](https://github.com/testdouble/quibble/issues/99) * `ignoreCallsFromThisFile()` now accepts a `file:` URL such as `import.meta.url` diff --git a/test/esm-lib/quibble-esm.test.mjs b/test/esm-lib/quibble-esm.test.mjs index 0b6ecdd..d960334 100644 --- a/test/esm-lib/quibble-esm.test.mjs +++ b/test/esm-lib/quibble-esm.test.mjs @@ -8,6 +8,7 @@ import os from 'os' import path from 'path' const __filename = fileURLToPath(import.meta.url) +const nodeMajorVersion = parseInt(process.versions.node, 10) export default { afterEach: function () { quibble.reset() }, @@ -175,7 +176,11 @@ export default { // mock nothing gets tagged, so the test would pass on the bug await quibble.esm('../esm-fixtures/a-module.mjs', undefined, 'unrelated') const { default: wrappedQuibbleEsm, url } = await import('../esm-fixtures/a-quibble-esm-wrapper-imported-under-a-mock.mjs') - assert.match(url, /[?&]__quibble=/) + // Before Node 22, a module's URL (and so its stack frames) never has the + // query, so there the bug can't happen and only the rest of the test runs + if (nodeMajorVersion >= 22) { + assert.match(url, /[?&]__quibble=/) + } // Only resolvable from this file's directory, not the wrapper's await wrappedQuibbleEsm('./a-module-next-to-this-test.mjs', { namedExport: 'replacement' }, 'default-export-replacement') @@ -190,7 +195,9 @@ export default { // Without this mock nothing gets tagged, see the test above await quibble.esm('../esm-fixtures/a-module.mjs', undefined, 'unrelated') const { default: wrappedQuibbleEsm, absolutify, url } = await import('../esm-fixtures/a-quibble-esm-wrapper-ignored-by-url.mjs') - assert.match(url, /[?&]__quibble=/) + if (nodeMajorVersion >= 22) { + assert.match(url, /[?&]__quibble=/) + } assert.equal(absolutify('./a-module-next-to-this-test.mjs'), path.join(path.dirname(__filename), 'a-module-next-to-this-test.mjs'))