vitest-environment-webgl-node 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ben Houston
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # vitest-environment-webgl-node
2
+
3
+ Real, headless WebGL 1 and WebGL 2 inside Vitest, with no browser. Powered by
4
+ [`@onirenaud/node-webgl`](https://github.com/RenaudRohlinger/node-webgl) (Chrome's ANGLE, statically linked).
5
+
6
+ The environment installs `window`, `document`, `HTMLCanvasElement`, `Image`, `requestAnimationFrame`,
7
+ the `WebGL*` classes and a file-reading `fetch()` before each test file, and removes them afterwards.
8
+ `document.createElement('canvas').getContext('webgl2')` returns a GPU-backed context, so three.js,
9
+ regl, pixi and plain WebGL code run unchanged.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ pnpm add -D vitest vitest-environment-webgl-node
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ // vitest.config.ts
21
+ export default defineConfig({
22
+ test: {
23
+ environment: 'webgl-node',
24
+ environmentOptions: { webglNode: { backend: 'swiftshader', baseDir: 'assets' } },
25
+ },
26
+ });
27
+ ```
28
+
29
+ ```ts
30
+ import * as THREE from 'three';
31
+ import { expect, it } from 'vitest';
32
+
33
+ it('renders', () => {
34
+ const canvas = document.createElement('canvas');
35
+ const renderer = new THREE.WebGLRenderer({ canvas });
36
+ renderer.setSize(64, 64, false);
37
+ renderer.render(new THREE.Scene(), new THREE.PerspectiveCamera());
38
+ expect(canvas.getImageData().width).toBe(64);
39
+ });
40
+ ```
41
+
42
+ Options are those of node-webgl's `init()` (`backend`, `api`) and `installDOM()`
43
+ (`baseDir`, `fetch`, `devicePixelRatio`, `innerWidth`, `innerHeight`, `frameInterval`).
44
+ Pair with [`vitest-screenshot`](../vitest-screenshot) for pixel-diff assertions.
45
+
46
+ Types for the installed globals: add `import 'vitest-environment-webgl-node/globals';` to a `.d.ts` file your tsconfig includes.
47
+
48
+ On Linux CI install Mesa (`apt-get install libegl1 libgles2 libgl1-mesa-dri`) and set
49
+ `LIBGL_ALWAYS_SOFTWARE=1`. macOS and Windows use prebuilt binaries.
@@ -0,0 +1,7 @@
1
+ import { type InitOptions, type InstallDOMOptions } from '@onirenaud/node-webgl';
2
+ import type { Environment } from 'vitest/environments';
3
+ /** Set under `test.environmentOptions.webglNode` in your Vitest config. */
4
+ export type WebglNodeOptions = InitOptions & InstallDOMOptions;
5
+ declare const _default: Environment;
6
+ export default _default;
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,WAAW,EAAE,KAAK,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AACnG,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD,2EAA2E;AAC3E,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,iBAAiB,CAAC;wBAE/C,WAAW"}
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ import { init, installDOM } from '@onirenaud/node-webgl';
2
+ export default {
3
+ name: 'webgl-node',
4
+ viteEnvironment: 'ssr',
5
+ setup(global, { webglNode = {} }) {
6
+ const { backend, api, ...dom } = webglNode;
7
+ if (backend || api)
8
+ init({ backend, api });
9
+ const before = new Set(Object.getOwnPropertyNames(global));
10
+ installDOM(dom);
11
+ // node-webgl's window/document have no event methods; engines like Babylon.js register resize/blur listeners.
12
+ const { window, document } = global;
13
+ for (const target of [window, document]) {
14
+ if (!target)
15
+ continue;
16
+ target.addEventListener ??= () => { };
17
+ target.removeEventListener ??= () => { };
18
+ }
19
+ const added = Object.getOwnPropertyNames(global).filter((key) => !before.has(key));
20
+ return {
21
+ teardown(g) {
22
+ for (const key of added)
23
+ delete g[key];
24
+ },
25
+ };
26
+ },
27
+ };
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,UAAU,EAA4C,MAAM,uBAAuB,CAAC;AAMnG,eAA4B;IAC1B,IAAI,EAAE,YAAY;IAClB,eAAe,EAAE,KAAK;IACtB,KAAK,CAAC,MAAyB,EAAE,EAAE,SAAS,GAAG,EAAE,EAAoC;QACnF,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,GAAG,EAAE,GAAG,SAAS,CAAC;QAC3C,IAAI,OAAO,IAAI,GAAG;YAAE,IAAI,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;QAC3C,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAC;QAC3D,UAAU,CAAC,GAAG,CAAC,CAAC;QAChB,8GAA8G;QAC9G,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,MAA4D,CAAC;QAC1F,KAAK,MAAM,MAAM,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,EAAE,CAAC;YACxC,IAAI,CAAC,MAAM;gBAAE,SAAS;YACtB,MAAM,CAAC,gBAAgB,KAAK,GAAG,EAAE,GAAE,CAAC,CAAC;YACrC,MAAM,CAAC,mBAAmB,KAAK,GAAG,EAAE,GAAE,CAAC,CAAC;QAC1C,CAAC;QACD,MAAM,KAAK,GAAG,MAAM,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;QACnF,OAAO;YACL,QAAQ,CAAC,CAA0B;gBACjC,KAAK,MAAM,GAAG,IAAI,KAAK;oBAAE,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC;YACzC,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC"}
package/globals.d.ts ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Types for the globals `vitest-environment-webgl-node` installs (node-webgl's `installDOM()`).
3
+ * Pull in with `import 'vitest-environment-webgl-node/globals';` from a `.d.ts` your tsconfig includes.
4
+ */
5
+ import type {
6
+ Canvas,
7
+ Image as NodeImage,
8
+ ImageBitmap as NodeImageBitmap,
9
+ ImageData as NodeImageData,
10
+ WebGL2RenderingContext as NodeWebGL2RenderingContext,
11
+ WebGLRenderingContext as NodeWebGLRenderingContext,
12
+ createImageBitmap as nodeCreateImageBitmap,
13
+ } from '@onirenaud/node-webgl';
14
+
15
+ type Listener = (type: string, listener: (event: unknown) => void, options?: unknown) => void;
16
+
17
+ interface ShimDocument {
18
+ createElement(tag: 'canvas'): Canvas;
19
+ createElement(tag: 'img'): NodeImage;
20
+ createElement(tag: string): unknown;
21
+ createElementNS(namespace: string, tag: 'canvas'): Canvas;
22
+ createElementNS(namespace: string, tag: string): unknown;
23
+ addEventListener: Listener;
24
+ removeEventListener: Listener;
25
+ }
26
+
27
+ interface ShimWindow {
28
+ innerWidth: number;
29
+ innerHeight: number;
30
+ devicePixelRatio: number;
31
+ addEventListener: Listener;
32
+ removeEventListener: Listener;
33
+ }
34
+
35
+ declare global {
36
+ var window: ShimWindow & typeof globalThis;
37
+ var self: ShimWindow & typeof globalThis;
38
+ var document: ShimDocument;
39
+ var innerWidth: number;
40
+ var innerHeight: number;
41
+ var devicePixelRatio: number;
42
+ var HTMLCanvasElement: typeof Canvas;
43
+ var OffscreenCanvas: typeof Canvas;
44
+ var HTMLImageElement: typeof NodeImage;
45
+ var Image: typeof NodeImage;
46
+ var ImageData: typeof NodeImageData;
47
+ var ImageBitmap: typeof NodeImageBitmap;
48
+ var createImageBitmap: typeof nodeCreateImageBitmap;
49
+ var WebGLRenderingContext: typeof NodeWebGLRenderingContext;
50
+ var WebGL2RenderingContext: typeof NodeWebGL2RenderingContext;
51
+ function requestAnimationFrame(callback: (time: number) => void): number;
52
+ function cancelAnimationFrame(handle: number): void;
53
+ }
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "vitest-environment-webgl-node",
3
+ "version": "0.1.0",
4
+ "description": "Vitest environment that provides real headless WebGL 1/2 (ANGLE via @onirenaud/node-webgl) in Node",
5
+ "keywords": [
6
+ "headless",
7
+ "testing",
8
+ "three.js",
9
+ "vitest",
10
+ "vitest-environment",
11
+ "webgl",
12
+ "webgl2"
13
+ ],
14
+ "homepage": "https://github.com/bhouston/vitest-gpu#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/bhouston/vitest-gpu/issues"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Ben Houston <ben@ben3d.ca> (https://ben3d.ca)",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/bhouston/vitest-gpu.git",
23
+ "directory": "packages/vitest-environment-webgl-node"
24
+ },
25
+ "files": [
26
+ "globals.d.ts",
27
+ "dist/**/*.js",
28
+ "dist/**/*.js.map",
29
+ "dist/**/*.d.ts",
30
+ "dist/**/*.d.ts.map",
31
+ "CHANGELOG.md"
32
+ ],
33
+ "type": "module",
34
+ "main": "./dist/index.js",
35
+ "types": "./dist/index.d.ts",
36
+ "exports": {
37
+ ".": {
38
+ "types": "./dist/index.d.ts",
39
+ "import": "./dist/index.js"
40
+ },
41
+ "./globals": {
42
+ "types": "./globals.d.ts"
43
+ }
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "scripts": {
49
+ "build": "tsc -b",
50
+ "tsc": "tsc -b"
51
+ },
52
+ "dependencies": {
53
+ "@onirenaud/node-webgl": "^0.2.0"
54
+ },
55
+ "peerDependencies": {
56
+ "vitest": ">=3 <5"
57
+ },
58
+ "engines": {
59
+ "node": ">=20.0.0"
60
+ }
61
+ }