Add mocking module

master
Frédéric Matte 2025-06-02 15:51:28 -04:00
parent 05cc68d84c
commit 1df35e971a
2 changed files with 57 additions and 0 deletions

View File

@ -1,4 +1,5 @@
export * from './reporter/index.js' export * from './reporter/index.js'
export * from './lib/mock.js'
export { suite } from './lib/suite.js' export { suite } from './lib/suite.js'
import { metaltest } from './lib/metaltest.js' import { metaltest } from './lib/metaltest.js'

56
lib/mock.js Normal file
View File

@ -0,0 +1,56 @@
const capture = (actions, name, fn) => async (...args) => {
const count = actions.reduce((count, curr) => {
if (curr.name === name) return count + 1
return count
}, 1)
try {
const result = await fn(...args, { count })
actions.push({ name, args, result })
return result
} catch (error) {
actions.push({ name, args, result: error })
throw error
}
}
const mock = async (mocks, fn) => {
const olds = {}
const actions = []
for (const mock of mocks) {
if (mock.package in olds === false) olds[mock.package] = {}
const old = olds[mock.package]
for (const name in mock.funcs) {
//Save
old[name] = mock.package[name]
//Capture
mock.package[name] = capture(actions, name, mock.funcs[name])
}
}
//Restore even when an error occurs
//Like with a failed assert
try {
await fn()
} catch (error) {
throw error
} finally {
for (const mock of mocks) {
const old = olds[mock.package]
for (const name in mock.funcs) {
//Restore
mock.package[name] = old[name]
}
}
}
return actions
}
export { mock }