-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock.mjs
72 lines (63 loc) · 1.84 KB
/
mock.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {randomBytes} from 'crypto'
import StackUtils from 'stack-utils'
import {pathToFileURL, fileURLToPath} from 'url'
import {resolve, dirname} from 'path'
const stack = new StackUtils()
const loader = `${pathToFileURL(
resolve(dirname(fileURLToPath(import.meta.url)), 'loader.mjs')
)}`
const getStack = fn => {
const obj = {}
Error.captureStackTrace(obj, fn)
return obj.stack.split('\n').slice(1).join('\n')
}
export const mock = async (module, mocks) => {
const at = Object.freeze(stack.at(mock))
const {file} = at
const path = file.startsWith('file:///') ? fileURLToPath(file)
: resolve(file)
const dir = dirname(path)
const url = file.startsWith('file:///') ? file
: pathToFileURL(resolve(file))
if (global.__tapmockLoader !== loader) {
const msg = `Cannot mock ESM. Run with --loader=${loader} to enable.`
const er = Object.assign(new Error(msg), {
found: global.__tapmockLoader,
wanted: loader,
})
Error.captureStackTrace(er, mock)
throw er
}
const key = randomBytes(8).toString('hex')
mocks = Object.entries(mocks).map(([k, m]) => {
m = m && typeof m === 'object' ? m : { default: m }
if (/^(node:|file:\/\/\/|https?:\/\/)/.test(k)) {
return [k, m]
} else if (/^\.\.?\//.test(k)) {
return [pathToFileURL(resolve(dir, k)), m]
}
}).reduce((o, kv) => {
o[kv[0]] = kv[1]
return o
}, Object.create(null))
Object.defineProperty(global, `__tapmock${key}`, {
value: Object.freeze({
unmock: () => mocks = null,
mocks,
key,
caller: Object.freeze({
path,
dir,
url,
at,
stack: getStack(mock),
}),
}),
enumerable: false,
configurable: false,
writable: false,
})
const start = new URL(module, url)
start.searchParams.set('tapmock', key)
return await import(`${start}`)
}