diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a3dbb9..e692d3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ * 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'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` # 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..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() }, @@ -169,6 +170,41 @@ 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') + // 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') + 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') + 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')) + + 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')