Skip to main content

jest-environment-webgpu-node

Classes​

default​

Jest environment that puts real headless WebGPU (Dawn via the webgpu package) on the sandbox global: navigator.gpu, every GPU* class and constant, and a HeadlessCanvas shim. CommonJS tests can rely on createCanvas being a global; ESM tests can also import it from this package.

Extends​

  • TestEnvironment

Constructors​

Constructor​
new default(config, context): default;

Creates the environment; Jest calls this once per test file.

Parameters​
ParameterTypeDescription
configJestEnvironmentConfigJest's environment config; testEnvironmentOptions.dawnOptions is read from it.
contextEnvironmentContextJest's environment context.
Returns​

default

Overrides​

TestEnvironment.constructor

Properties​

context​
context: Context | null;
Inherited from​

TestEnvironment.context

customExportConditions​
customExportConditions: string[];
Inherited from​

TestEnvironment.customExportConditions

fakeTimers​
fakeTimers: LegacyFakeTimers<Timer> | null;
Inherited from​

TestEnvironment.fakeTimers

fakeTimersModern​
fakeTimersModern: ModernFakeTimers | null;
Inherited from​

TestEnvironment.fakeTimersModern

global​
global: Global_3;
Inherited from​

TestEnvironment.global

moduleMocker​
moduleMocker: ModuleMocker | null;
Inherited from​

TestEnvironment.moduleMocker

Methods​

exportConditions()​
exportConditions(): string[];
Returns​

string[]

Inherited from​

TestEnvironment.exportConditions

getVmContext()​
getVmContext(): Context | null;
Returns​

Context | null

Inherited from​

TestEnvironment.getVmContext

setup()​
setup(): Promise<void>;

Loads Dawn, then installs navigator.gpu, the GPU* globals, the DOM shims and createCanvas on the sandbox.

Returns​

Promise<void>

Overrides​

TestEnvironment.setup

teardown()​
teardown(): Promise<void>;

Cancels pending animation frames and removes everything setup() installed from the sandbox.

Returns​

Promise<void>

Overrides​

TestEnvironment.teardown


HeadlessCanvas​

Enough of HTMLCanvasElement for three.js WebGPURenderer, Babylon Lite and friends.

Constructors​

Constructor​
new HeadlessCanvas(width?, height?): HeadlessCanvas;

Creates a canvas of the given size; see also createCanvas().

Parameters​
ParameterTypeDefault valueDescription
width?number300Canvas width in pixels.
height?number150Canvas height in pixels.
Returns​

HeadlessCanvas

Properties​

height​
height: number = 150;

Canvas height in pixels. Change it to resize the render target.

style​
readonly style: Record<string, string> = {};

Style properties libraries may set; stored but not applied to anything.

width​
width: number = 300;

Canvas width in pixels. Change it to resize the render target.

Accessors​

clientHeight​
Get Signature​
get clientHeight(): number;

Same as height.

Returns​

number

clientWidth​
Get Signature​
get clientWidth(): number;

Same as width.

Returns​

number

Methods​

addEventListener()​
addEventListener(): void;

Does nothing; the canvas dispatches no events.

Returns​

void

asElement()​
asElement(): HTMLCanvasElement;

This same object, typed as an element for libraries whose signatures demand one.

Returns​

HTMLCanvasElement

dispatchEvent()​
dispatchEvent(): boolean;

Does nothing and returns true.

Returns​

boolean

getAttribute()​
getAttribute(name): string | null;

Returns an attribute stored with setAttribute(), or null when unset.

Parameters​
ParameterTypeDescription
namestringAttribute name.
Returns​

string | null

getBoundingClientRect()​
getBoundingClientRect(): object;

Returns a rectangle at the origin with the canvas's size.

Returns​
NameTypeDescription
heightnumberThe canvas height.
leftnumberAlways 0.
topnumberAlways 0.
widthnumberThe canvas width.
xnumberAlways 0.
ynumberAlways 0.
getContext()​
Call Signature​
getContext(id): HeadlessCanvasContext;

Returns this canvas's WebGPU context.

Parameters​
ParameterTypeDescription
id"webgpu"The context type, 'webgpu'.
Returns​

HeadlessCanvasContext

Call Signature​
getContext(id): HeadlessCanvasContext | null;

Returns this canvas's WebGPU context for 'webgpu', or null for any other type.

Parameters​
ParameterTypeDescription
idstringThe context type requested.
Returns​

HeadlessCanvasContext | null

readPixels()​
readPixels(): Promise<RgbaImage>;

Reads back the canvas's current texture as RGBA8 pixels; see HeadlessCanvasContext.readPixels().

Returns​

Promise<RgbaImage>

removeEventListener()​
removeEventListener(): void;

Does nothing.

Returns​

void

setAttribute()​
setAttribute(name, value): void;

Stores an attribute for getAttribute().

Parameters​
ParameterTypeDescription
namestringAttribute name.
valuestringAttribute value.
Returns​

void


HeadlessCanvasContext​

Minimal GPUCanvasContext: getCurrentTexture() is a texture the size of the canvas, kept until reconfigured or resized.

Constructors​

Constructor​
new HeadlessCanvasContext(canvas): HeadlessCanvasContext;

Creates a context for a canvas; HeadlessCanvas does this for you.

Parameters​
ParameterTypeDescription
canvasHeadlessCanvasThe canvas this context renders into; its current size sets the texture size.
Returns​

HeadlessCanvasContext

Properties​

canvas​
readonly canvas: HeadlessCanvas;

The canvas that owns this context.

Methods​

configure()​
configure(config): void;

Stores the configuration and discards any current texture, as GPUCanvasContext.configure() does.

Parameters​
ParameterTypeDescription
configGPUCanvasConfigurationThe device, format and usage to render with.
Returns​

void

getConfiguration()​
getConfiguration(): GPUCanvasConfiguration | null;

Returns the configuration passed to configure(), or null before it is called or after unconfigure().

Returns​

GPUCanvasConfiguration | null

getCurrentTexture()​
getCurrentTexture(): GPUTexture;

Returns the texture to render into, creating it at the canvas's current size on first use or after a resize.

Returns​

GPUTexture

Throws​

If configure() has not been called.

readPixels()​
readPixels(): Promise<RgbaImage>;

Copies the current texture to the CPU. BGRA formats are swizzled to RGBA.

Returns​

Promise<RgbaImage>

Throws​

If the context is not configured or the format is not 8-bit RGBA or BGRA.

unconfigure()​
unconfigure(): void;

Clears the configuration and destroys the current texture.

Returns​

void

Type Aliases​

RgbaImage​

type RgbaImage = object;

RGBA8 pixels, top row first. Feed it to a screenshot matcher such as jest-image-snapshot after encoding to PNG.

Properties​

data​
data: Uint8Array;

Pixel bytes, four per pixel in R, G, B, A order, rows top to bottom with no padding.

height​
height: number;

Height in pixels.

width​
width: number;

Width in pixels.


WebgpuNodeOptions​

type WebgpuNodeOptions = object;

Set under testEnvironmentOptions in your Jest config.

Properties​

dawnOptions?​
dawnOptions?: string[];

Dawn options, e.g. ['backend=vulkan', 'enable-dawn-features=allow_unsafe_apis'].

Functions​

createCanvas()​

function createCanvas(width, height): HeadlessCanvas;

Creates a headless canvas whose getContext('webgpu') returns a texture-backed GPUCanvasContext. Also installed as a global in the test environment.

Parameters​

ParameterTypeDescription
widthnumberCanvas width in pixels.
heightnumberCanvas height in pixels.

Returns​

HeadlessCanvas