Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type QuibbleConfig = {

declare const quibble: ((request: string, stub?: any) => any) & {
config(userConfig: Partial<QuibbleConfig>): QuibbleConfig;
ignoreCallsFromThisFile(file?: string): void;
ignoreCallsFromThisFile(file?: string | URL): void;
reset(hard?: boolean): void;
absolutify(relativePath: string, parentFileName?: string): string;
esm(
Expand Down
12 changes: 12 additions & 0 deletions lib/quibble.js
Original file line number Diff line number Diff line change
Expand Up @@ -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=<generation>` query
file = fileURLToPath(file)
}
ignoredCallerFiles = _.uniq(ignoredCallerFiles.concat(file, pathToFileURL(file).href))
}
Expand Down Expand Up @@ -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=<generation>`. 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
}
Expand Down
12 changes: 12 additions & 0 deletions test/esm-fixtures/a-quibble-esm-wrapper-ignored-by-url.mjs
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions test/esm-fixtures/a-quibble-esm-wrapper-imported-under-a-mock.mjs
Original file line number Diff line number Diff line change
@@ -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=<generation>` 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)
36 changes: 36 additions & 0 deletions test/esm-lib/quibble-esm.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
Expand Down Expand Up @@ -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=<generation>`, 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')
Expand Down
Loading