Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add listMockedModules #103

Merged
merged 1 commit into from
Oct 19, 2023
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
2 changes: 1 addition & 1 deletion example-esm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/quibble.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ quibble.esm = async function (specifier, namedExportStubs, defaultExportStub) {
})
}

quibble.listMockedModules = function () {
const esmMockedModules = quibbleUserToLoaderCommunication()?.listMockedModules() ?? []
const cjsMockedModules = Object.keys(quibbles).map((modulePath) => pathToFileURL(modulePath).href)

return esmMockedModules.concat(cjsMockedModules)
}

quibble.isLoaderLoaded = function () {
return !!quibbleUserToLoaderCommunication()
}
Expand Down
17 changes: 14 additions & 3 deletions lib/quibble.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ function getStubsInfo (moduleUrl) {
if (!quibbleLoaderState.quibbledModules) return undefined
if (!moduleUrl.includes('__quibble=')) return undefined

const moduleKey = moduleUrl
.replace(/\?.*/, '')
.replace(/#.*/, '')
const moduleKey = moduleUrl.replace(/\?.*/, '').replace(/#.*/, '')

const moduleMockingInfo = quibbleLoaderState.quibbledModules.get(moduleKey)

Expand Down Expand Up @@ -159,6 +157,19 @@ export const globalPreload = ({ port }) => {
++quibbleLoaderState.stubModuleGeneration
Atomics.store(data.hasAddMockedHappened, 0, 1)
Atomics.notify(data.hasAddMockedHappened, 0)
} else if (data.type === 'listMockedModules') {
const mockedModules = Array.from(quibbleLoaderState.quibbledModules.keys())
const serializedMockedModules = mockedModules.join(' ')
const encodedMockedModules = new TextEncoder().encode(serializedMockedModules)

data.mockedModulesListLength[0] = encodedMockedModules.length
if (encodedMockedModules.length <= data.mockedModulesList.length) {
for (let i = 0; i < encodedMockedModules.length; ++i) {
data.mockedModulesList[i] = encodedMockedModules[i]
}
}
Atomics.store(data.hasListMockedModulesHappened, 0, 1)
Atomics.notify(data.hasListMockedModulesHappened, 0)
}
})
port.unref()
Expand Down
25 changes: 24 additions & 1 deletion lib/thisWillRunInUserThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports.thisWillRunInUserThread = (globalThis, port) => {
quibbleLoaderState.stubModuleGeneration++
}
},
async addMockedModule (
addMockedModule (
moduleUrl,
{ namedExportStubs, defaultExportStub }
) {
Expand Down Expand Up @@ -45,6 +45,29 @@ exports.thisWillRunInUserThread = (globalThis, port) => {
})
++quibbleLoaderState.stubModuleGeneration
}
},
listMockedModules () {
if (!loaderAndUserRunInSameThread(globalThis)) {
const hasListMockedModulesHappened = new Int32Array(new SharedArrayBuffer(4))
const mockedModulesListLength = new Int32Array(new SharedArrayBuffer(4))
const mockedModulesList = new Uint8Array(new SharedArrayBuffer(20 * 1024 * 1024)) // 20MB should be sufficient
port.postMessage({
type: 'listMockedModules',
hasListMockedModulesHappened,
mockedModulesListLength,
mockedModulesList
})
Atomics.wait(hasListMockedModulesHappened, 0, 0)
if (mockedModulesListLength[0] > mockedModulesList.length) {
throw new Error('Not enough buffer allocated for result')
}
const serializedMockedModules = new TextDecoder().decode(mockedModulesList.slice(0, mockedModulesListLength[0]))
return serializedMockedModules ? serializedMockedModules.split(' ') : []
} else {
const quibbleLoaderState = globalThis[Symbol.for('__quibbleLoaderState')]

return Array.from(quibbleLoaderState.quibbledModules.keys())
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions test/esm-lib/quibble-esm.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// as it checks how quibble interacts with internal modules
import 'fs'
import quibble from 'quibble'
import { fileURLToPath, pathToFileURL } from 'url'

const __filename = fileURLToPath(import.meta.url)

export default {
afterEach: function () { quibble.reset() },
Expand Down Expand Up @@ -170,6 +173,31 @@ export default {

// Should still allow Proxy.
await quibble.esm('../esm-fixtures/a-module.mjs', new Proxy({}, {}))
},
'list mocked modules': async function () {
await quibble.esm('../esm-fixtures/a-module-with-function.mjs', {
namedExport: 'replacement',
life: 41,
namedFunctionExport: () => 'export replacement'
}, 'default-export-replacement')

assert.deepEqual(quibble.listMockedModules(), [
pathToFileURL(quibble.absolutify('../esm-fixtures/a-module-with-function.mjs', __filename)).href
])
await quibble.esm('../esm-fixtures/a-module.mjs', {
namedExport: 'replacement',
life: 41,
namedFunctionExport: () => 'export replacement'
}, 'default-export-replacement')

assert.deepEqual(quibble.listMockedModules(), [
pathToFileURL(quibble.absolutify('../esm-fixtures/a-module-with-function.mjs', __filename)).href,
pathToFileURL(quibble.absolutify('../esm-fixtures/a-module.mjs', __filename)).href
])

quibble.reset()

assert.deepEqual(quibble.listMockedModules(), [])
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/lib/quibble.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const quibble = require('../../lib/quibble')
const { pathToFileURL } = require('url')

module.exports = {
'basic behavior': function () {
Expand Down Expand Up @@ -127,6 +128,24 @@ module.exports = {

assert.equal(quibbledRequiresANodeModule(), false)
},
'list mocked modules': async function () {
quibble('../fixtures/a-function', function () { return 'kek' })

assert.deepEqual(quibble.listMockedModules(), [
pathToFileURL(quibble.absolutify('../fixtures/a-function', __filename)).href
])

quibble('../fixtures/b-function', function () { return 'kek' })

assert.deepEqual(quibble.listMockedModules(), [
pathToFileURL(quibble.absolutify('../fixtures/a-function', __filename)).href,
pathToFileURL(quibble.absolutify('../fixtures/b-function', __filename)).href
])

quibble.reset()

assert.deepEqual(await quibble.listMockedModules(), [])
},
afterEach: function () {
quibble.reset()
},
Expand Down