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
| Parameter | Type | Description |
|---|---|---|
config | JestEnvironmentConfig | Jest's environment config; testEnvironmentOptions.dawnOptions is read from it. |
context | EnvironmentContext | Jest's environment context. |
Returns
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
width? | number | 300 | Canvas width in pixels. |
height? | number | 150 | Canvas height in pixels. |
Returns
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
| Parameter | Type | Description |
|---|---|---|
name | string | Attribute name. |
Returns
string | null
getBoundingClientRect()
getBoundingClientRect(): object;
Returns a rectangle at the origin with the canvas's size.
Returns
| Name | Type | Description |
|---|---|---|
height | number | The canvas height. |
left | number | Always 0. |
top | number | Always 0. |
width | number | The canvas width. |
x | number | Always 0. |
y | number | Always 0. |
getContext()
Call Signature
getContext(id): HeadlessCanvasContext;
Returns this canvas's WebGPU context.
Parameters
| Parameter | Type | Description |
|---|---|---|
id | "webgpu" | The context type, 'webgpu'. |
Returns
Call Signature
getContext(id): HeadlessCanvasContext | null;
Returns this canvas's WebGPU context for 'webgpu', or null for any other type.
Parameters
| Parameter | Type | Description |
|---|---|---|
id | string | The 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
| Parameter | Type | Description |
|---|---|---|
name | string | Attribute name. |
value | string | Attribute 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
| Parameter | Type | Description |
|---|---|---|
canvas | HeadlessCanvas | The canvas this context renders into; its current size sets the texture size. |
Returns
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
| Parameter | Type | Description |
|---|---|---|
config | GPUCanvasConfiguration | The 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
| Parameter | Type | Description |
|---|---|---|
width | number | Canvas width in pixels. |
height | number | Canvas height in pixels. |