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
8 changes: 8 additions & 0 deletions test/esm-fixtures/package-with-imports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "package-with-imports",
"description": "Fixture whose package.json maps #src/* imports, which only files in this package can resolve (#69)",
"private": true,
"imports": {
"#src/*": "./src/*"
}
}
2 changes: 2 additions & 0 deletions test/esm-fixtures/package-with-imports/src/a-module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default 'default-export'
export const namedExport = 'named-export'
14 changes: 14 additions & 0 deletions test/esm-fixtures/package-with-imports/uses-imports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Lives in a package whose package.json maps `#src/*`, so `#src/...` specifiers
// only resolve from files in this package, not from the tests or from quibble
import quibble from '../../../lib/quibble.mjs'
import wrappedQuibbleEsm from '../a-quibble-esm-wrapper.mjs'

export const mock = (namedExportStubs, defaultExportStub) =>
quibble.esm('#src/a-module.mjs', namedExportStubs, defaultExportStub)

export const mockThroughWrapper = (namedExportStubs, defaultExportStub) =>
wrappedQuibbleEsm('#src/a-module.mjs', namedExportStubs, defaultExportStub)

export const importWithPath = () => quibble.esmImportWithPath('#src/a-module.mjs')

export const importModule = () => import('#src/a-module.mjs')
27 changes: 27 additions & 0 deletions test/esm-lib/quibble-esm.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,33 @@ export default {
assert.equal(result.default, 'default-export-replacement')
assert.equal(result.namedExport, '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')

const result = await usesImports.importModule()
assert.equal(result.default, 'default-export-replacement')
assert.equal(result.namedExport, 'replacement')

const viaPath = await import('../esm-fixtures/package-with-imports/src/a-module.mjs')
assert.equal(viaPath.default, 'default-export-replacement')
},
'mock a module by a package.json imports specifier through a wrapper that ignores itself': async function () {
const usesImports = await import('../esm-fixtures/package-with-imports/uses-imports.mjs')
await usesImports.mockThroughWrapper({ namedExport: 'replacement' }, 'default-export-replacement')

const result = await usesImports.importModule()
assert.equal(result.default, 'default-export-replacement')
assert.equal(result.namedExport, 'replacement')
},
'esmImportWithPath resolves a package.json imports specifier from the caller': async function () {
const usesImports = await import('../esm-fixtures/package-with-imports/uses-imports.mjs')
const { moduleUrl, module } = await usesImports.importWithPath()

assert.equal(moduleUrl, new URL('../esm-fixtures/package-with-imports/src/a-module.mjs', import.meta.url).href)
assert.equal(module.default, 'default-export')
assert.equal(module.namedExport, 'named-export')
},
'isLoaderLoader returns true if loader as loaded': async function () {
assert.equal(quibble.isLoaderLoaded(), true)
},
Expand Down
Loading