Skip to main content

Jest · WebGPU · WebGL

Test your renderer.
Skip the browser.

Two Jest environments put a real GPU context on the test global. three.js, Babylon.js and plain WebGPU or WebGL code run unchanged in Node, and native GPU testing runs up to 2.4x faster than a browser.

A WebGPU test in Node
// gpu.test.js
const { createCanvas } = require('jest-environment-webgpu-node');

it('clears to red on a real GPU', async () => {
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const canvas = createCanvas(4, 4);
const context = canvas.getContext('webgpu');
context.configure({ device, format: 'rgba8unorm' });

const encoder = device.createCommandEncoder();
encoder
.beginRenderPass({
colorAttachments: [
{
view: context.getCurrentTexture().createView(),
loadOp: 'clear',
storeOp: 'store',
clearValue: [1, 0, 0, 1],
},
],
})
.end();
device.queue.submit([encoder.finish()]);

const { data } = await canvas.readPixels();
expect([...data.slice(0, 4)]).toEqual([255, 0, 0, 255]);
});
01Install

Add Jest and one environment package

02Configure

Set testEnvironment in your Jest config

03Render

Use WebGPU or WebGL in a normal test

04Assert

Read pixels back and check them

WebGPU through Dawn

Headless canvas,
pixels you can read.

navigator.gpu and every GPU* global are installed, with a headless canvas that three.js, Babylon Lite and vgpu accept. Both packages ship ESM and CommonJS builds.

Read about WebGPU →
A three.js WebGPU render and readback
import { createCanvas } from 'jest-environment-webgpu-node';

const canvas = createCanvas(256, 256);
const renderer = new THREE.WebGPURenderer({ canvas: canvas.asElement() });
await renderer.init();
await renderer.renderAsync(scene, camera);
const { width, height, data } = await canvas.readPixels();
A WebGL test
it('renders', () => {
const canvas = document.createElement('canvas');
const renderer = new THREE.WebGLRenderer({ canvas });
renderer.setSize(64, 64, false);
renderer.render(scene, camera);

const gl = renderer.getContext();
const pixels = new Uint8Array(4);
gl.readPixels(32, 32, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
expect(pixels[3]).toBe(255);
});

WebGL 1 and 2

Existing WebGL code
works too.

document.createElement('canvas').getContext('webgl2') returns a GPU-backed context. Each test file gets a fresh document, Jest fake timers control animation frames, and canvases are released at teardown.

Read about WebGL →

Keep reading

Find the part you need.

Jest 29 and 30 · Node.js 22+ · MIT

From install to a passing GPU test.

pnpm add -D jest jest-environment-webgpu-node

Using Vitest? Try vitest-gpu.