Skip to content

Commit eda89eb

Browse files
committed
feat: wrap workerd loader to match others
1 parent f7c668e commit eda89eb

3 files changed

Lines changed: 12 additions & 15 deletions

File tree

bin/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -718,12 +718,13 @@ if (options.pure) {
718718
export default {
719719
async test(ctrl, env, ctx) {
720720
await import('./' + ${JSON.stringify(jsRelativePath)})
721-
let exitCode = EXODUS_TEST_PROCESS.exitCode // can fail early
722-
if (exitCode === 0) {
723-
if (typeof globalThis.EXODUS_TEST_RUN !== 'function') throw new Error('node:test not loaded')
724-
exitCode = await globalThis.EXODUS_TEST_RUN()
721+
await globalThis.EXODUS_TEST_LOAD()
722+
if (globalThis.EXODUS_TEST_PROMISE) {
723+
const exitCode = await globalThis.EXODUS_TEST_PROMISE
724+
if (exitCode !== 0) throw new Error(\`Tests failed with exit code \${exitCode}\`)
725+
} else {
726+
console.log('WARNING: node:test not loaded, asynchronous tests might be missed')
725727
}
726-
if (exitCode !== 0) throw new Error(\`Tests failed with exit code \${exitCode}\`)
727728
}
728729
}`
729730
await writeFile(bundled.fileWrapper, wrapperContent)

bundler/bundle.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,11 @@ export const build = async (...files) => {
292292
const buildWrap = async (opts) => esbuild.build(opts).catch((err) => err)
293293
let main = input.join(';\n')
294294
const exit = `EXODUS_TEST_PROCESS.exitCode = 1; EXODUS_TEST_PROCESS._maybeProcessExitCode();`
295-
if (process.env.EXODUS_TEST_IS_BAREBONE) {
295+
if (process.env.EXODUS_TEST_PLATFORM === 'workerd') {
296+
main = `globalThis.EXODUS_TEST_LOAD = async () => {\n${main}\n}`
297+
} else if (process.env.EXODUS_TEST_IS_BAREBONE) {
296298
main = `try {\n${main}\n} catch (err) { print(err); ${exit} }`
297-
} else if (process.env.EXODUS_TEST_IS_BROWSER || process.env.EXODUS_TEST_PLATFORM === 'workerd') {
299+
} else if (process.env.EXODUS_TEST_IS_BROWSER) {
298300
main = `try {\n${main}\n} catch (err) { console.error(err); ${exit} }`
299301
}
300302

src/engine.pure.cjs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let willstart
1515

1616
const abstractProcess = globalThis.process || globalThis.EXODUS_TEST_PROCESS
1717

18-
if (process.env.EXODUS_TEST_IS_BROWSER) {
18+
if (process.env.EXODUS_TEST_IS_BROWSER || process.env.EXODUS_TEST_PLATFORM === 'workerd') {
1919
globalThis.EXODUS_TEST_PROMISE = new Promise((resolve) => (abstractProcess._exitHook = resolve))
2020
if (!abstractProcess._maybeProcessExitCode && abstractProcess === globalThis.process) {
2121
// Electron with Node.js integration has real process
@@ -104,7 +104,7 @@ function enterContext(name, options) {
104104
function exitContext() {
105105
check(context !== context.root)
106106
context = context.parent
107-
if (context === context.root && run !== globalThis.EXODUS_TEST_RUN) willstart = setTimeout(run, 0)
107+
if (context === context.root) willstart = setTimeout(run, 0)
108108
}
109109

110110
async function runFunction(fn, context) {
@@ -185,8 +185,6 @@ async function runContext(context) {
185185
async function run() {
186186
check(!running)
187187
running = true
188-
const manual = globalThis.EXODUS_TEST_RUN === run
189-
const res = manual ? new Promise((resolve) => (abstractProcess._exitHook = resolve)) : undefined
190188
check(context === context.root)
191189
await runContext(context).catch((error) => {
192190
// Should not throw under regular circumstances
@@ -195,12 +193,8 @@ async function run() {
195193
})
196194
// Let unhandled errors be processed (and set the error code)
197195
setTimeout(() => abstractProcess._maybeProcessExitCode?.(), 0)
198-
return res
199196
}
200197

201-
// For workerd, expose run as a global so the wrapper can call it
202-
if (process.env.EXODUS_TEST_PLATFORM === 'workerd') globalThis.EXODUS_TEST_RUN = run
203-
204198
async function describe(...args) {
205199
const { name, options, fn } = parseArgs(args)
206200
enterContext(name, options)

0 commit comments

Comments
 (0)