Files
leporello-js/test/run_utils.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-02-15 17:03:26 +08:00
/*
For node.js tests
It forces node.js to load Response (which is loaded lazily)
Without this, `Response` loading code would be executed in record_io.js and
break test by calling `now()`
*/
globalThis.Response
if(globalThis.process != null) {
globalThis.NodeVM = await import('node:vm')
}
2024-02-13 16:04:35 +08:00
let iframe
2024-02-13 16:04:35 +08:00
export function create_app_window() {
if(globalThis.process != null) {
// We are in node.js
// `NodeVM` was preloaded earlier
2024-02-13 16:04:35 +08:00
const context = globalThis.NodeVM.createContext({
2024-02-13 16:04:35 +08:00
process,
2024-02-13 16:04:35 +08:00
// for some reason URL is not available inside VM
URL,
2024-02-13 16:04:35 +08:00
console,
setTimeout,
// break fetch because we dont want it to be accidentally called in unit test
fetch: () => {
console.error('Error! fetch called')
},
2024-02-13 16:04:35 +08:00
})
const get_global_object = globalThis.NodeVM.compileFunction(
'return this',
[], // args
{parsingContext: context}
)
return get_global_object()
2024-02-13 16:04:35 +08:00
} else {
// We are in browser
if(iframe != null) {
globalThis.document.body.removeChild(iframe)
2024-02-13 16:04:35 +08:00
}
iframe = globalThis.document.createElement('iframe')
document.body.appendChild(iframe)
return iframe.contentWindow
2024-02-13 16:04:35 +08:00
}
}