modulestest

package
v0.0.0-...-deba56b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2023 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package modulestest contains helpers to test js modules

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Runtime

type Runtime struct {
	VU             *VU
	EventLoop      *eventloop.EventLoop
	CancelContext  func()
	BuiltinMetrics *metrics.BuiltinMetrics
	// contains filtered or unexported fields
}

Runtime is a helper struct that contains what is needed to run a (simple) module test

func NewRuntime

func NewRuntime(t testing.TB) *Runtime

NewRuntime will create a new test runtime and will cancel the context on test/benchmark end

func (*Runtime) MoveToVUContext

func (r *Runtime) MoveToVUContext(state *lib.State)

MoveToVUContext will set the state and nil the InitEnv just as a real VU

func (*Runtime) RunOnEventLoop

func (r *Runtime) RunOnEventLoop(code string) (value goja.Value, err error)

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 (*Runtime) SetupModuleSystem

func (r *Runtime) SetupModuleSystem(goModules map[string]any, loader modules.FileLoader, c *compiler.Compiler) error

SetupModuleSystem sets up the modules system for the Runtime. See modules.NewModuleResolver for the meaning of the parameters.

func (*Runtime) SetupModuleSystemFromAnother

func (r *Runtime) SetupModuleSystemFromAnother(another *Runtime) error

SetupModuleSystemFromAnother sets up the modules system for the Runtime by using the resolver of another runtime.

type VU

type VU struct {
	CtxField              context.Context
	InitEnvField          *common.InitEnvironment
	EventsField           common.Events
	StateField            *lib.State
	RuntimeField          *goja.Runtime
	RegisterCallbackField func() func(f func() error)
}

VU is a modules.VU implementation meant to be used within tests

func (*VU) Context

func (m *VU) Context() context.Context

Context returns internally set field to conform to modules.VU interface

func (*VU) Events

func (m *VU) Events() common.Events

Events returns internally set field to conform to modules.VU interface

func (*VU) InitEnv

func (m *VU) InitEnv() *common.InitEnvironment

InitEnv returns internally set field to conform to modules.VU interface

func (*VU) RegisterCallback

func (m *VU) RegisterCallback() func(f func() error)

RegisterCallback is not really implemented

func (*VU) Runtime

func (m *VU) Runtime() *goja.Runtime

Runtime returns internally set field to conform to modules.VU interface

func (*VU) State

func (m *VU) State() *lib.State

State returns internally set field to conform to modules.VU interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL