Skip to content

Commit

Permalink
Add a RunOnEventLoop method to modulestest.Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Jul 10, 2023
1 parent 1b446a4 commit 2b84825
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions js/modulestest/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ func (r *Runtime) SetupModuleSystemFromAnother(another *Runtime) error {
return r.innerSetupModuleSystem()
}

// RunOnEventLoop will run the given code on the event loop.
//
// It is meant as a helper to test code that is expected to be run on the event loop, such
// as code that returns a promise.
//
// A typical usage is to facilitate writing tests for asynchrounous code:
//
// func TestSomething(t *testing.T) {
// runtime := modulestest.NewRuntime(t)
//
// err := runtime.RunOnEventLoop(`
// doSomethingAsync().then(() => {
// // do some assertions
// });
// `)
// require.NoError(t, err)
// }
func (r *Runtime) RunOnEventLoop(code string) error {
err := r.EventLoop.Start(func() error {
_, err := r.VU.Runtime().RunString(code)
return err
})

r.EventLoop.WaitOnRegistered()

return err
}

func (r *Runtime) innerSetupModuleSystem() error {
ms := modules.NewModuleSystem(r.mr, r.VU)
impl := modules.NewLegacyRequireImpl(r.VU, ms, url.URL{})
Expand Down

0 comments on commit 2b84825

Please sign in to comment.