vitest-environment-webgl-node 3.0.1 → 3.0.2
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/README.md +35 -29
- package/dist/index.d.ts +21 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +22 -1
package/README.md
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://npmjs.com/package/vitest-environment-webgl-node"><img src="https://img.shields.io/npm/v/vitest-environment-webgl-node.svg" alt="npm package"></a>
|
|
3
|
+
<a href="https://www.npmtrends.com/vitest-environment-webgl-node"><img src="https://img.shields.io/npm/dw/vitest-environment-webgl-node.svg" alt="npm downloads"></a>
|
|
4
|
+
<a href="https://codecov.io/gh/bhouston/vitest-gpu"><img src="https://codecov.io/gh/bhouston/vitest-gpu/branch/main/graph/badge.svg" alt="coverage"></a>
|
|
5
|
+
<a href="https://discord.gg/fwupDN493R"><img src="https://img.shields.io/badge/chat-discord-blue?style=flat&logo=discord" alt="discord chat"></a>
|
|
6
|
+
<a href="https://vitest-gpu.ben3d.ca/"><img src="https://img.shields.io/badge/docs-read-blue?style=flat" alt="documentation"></a>
|
|
7
|
+
</p>
|
|
8
|
+
<br/>
|
|
9
|
+
|
|
1
10
|
# vitest-environment-webgl-node
|
|
2
11
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
12
|
+
> Real WebGL 1 and 2 tests in Vitest, with no browser.
|
|
13
|
+
|
|
14
|
+
- 🖥️ Real WebGL 1 and 2
|
|
15
|
+
- 🚫 No Browser or Mocks
|
|
16
|
+
- 🧩 Runs three.js Unchanged
|
|
17
|
+
- 🖼️ Pairs With vitest-screenshot
|
|
18
|
+
- ⚡ Up to 2.4x Faster Than Browsers
|
|
19
|
+
|
|
20
|
+
Headless GPU testing for CI: `canvas.getContext('webgl2')` returns a GPU-backed context, so three.js, regl,
|
|
21
|
+
PixiJS and plain WebGL code run unchanged. Native GPU testing is up to 2.4x faster than running the same tests
|
|
22
|
+
in a browser, see the [blog post](https://ben3d.ca/blog/native-gpu-testing-for-vitest-and-jest).
|
|
23
|
+
Powered by [`@onirenaud/node-webgl`](https://github.com/RenaudRohlinger/node-webgl) (Chrome's ANGLE, statically linked).
|
|
24
|
+
|
|
25
|
+
Part of [vitest-gpu](https://github.com/bhouston/vitest-gpu), which also provides WebGPU and image snapshot packages.
|
|
8
26
|
|
|
9
|
-
|
|
10
|
-
Headless GPU testing for CI: `canvas.getContext('webgl2')` returns a GPU-backed context, so three.js,
|
|
11
|
-
regl, PixiJS and plain WebGL code run unchanged. Native GPU testing is up to 2.4x faster than running
|
|
12
|
-
the same tests in a browser, see the
|
|
13
|
-
[blog post](https://ben3d.ca/blog/native-gpu-testing-for-vitest-and-jest).
|
|
14
|
-
Powered by [`@onirenaud/node-webgl`](https://github.com/RenaudRohlinger/node-webgl) (Chrome's ANGLE,
|
|
15
|
-
statically linked).
|
|
27
|
+
[Read the Docs to Learn More](https://vitest-gpu.ben3d.ca/docs/webgl/) about the WebGL environment: what it installs, its options, TypeScript types for the globals and CI setup.
|
|
16
28
|
|
|
17
29
|
```sh
|
|
18
30
|
pnpm add -D vitest vitest-environment-webgl-node
|
|
@@ -46,7 +58,7 @@ the `WebGL*` classes and a file-reading `fetch()` before each test file, and rem
|
|
|
46
58
|
|
|
47
59
|
Using Jest instead? See the equivalent
|
|
48
60
|
[`jest-environment-webgl-node`](https://github.com/bhouston/jest-gpu/tree/main/packages/jest-environment-webgl-node)
|
|
49
|
-
in [jest-gpu](https://github.com/bhouston/jest-gpu).
|
|
61
|
+
in [jest-gpu](https://github.com/bhouston/jest-gpu), documented at [jest-gpu.ben3d.ca](https://jest-gpu.ben3d.ca/).
|
|
50
62
|
|
|
51
63
|
## Stability
|
|
52
64
|
|
|
@@ -65,9 +77,9 @@ Breaking changes will be released as a new major version.
|
|
|
65
77
|
## Supported versions and platforms
|
|
66
78
|
|
|
67
79
|
- Vitest 4 and 5 (CI tests both). Vitest 3 is not supported.
|
|
68
|
-
- Node 22 or later. CI runs Node 22 and 26 on `macos-latest` and `
|
|
80
|
+
- Node 22 or later. CI runs Node 22 and 26 on `macos-latest`, `ubuntu-latest` and `windows-latest`.
|
|
69
81
|
- macOS uses Metal. Linux uses Mesa (see below). Windows uses Direct3D 11 per the
|
|
70
|
-
[node-webgl docs](https://github.com/RenaudRohlinger/node-webgl#platforms)
|
|
82
|
+
[node-webgl docs](https://github.com/RenaudRohlinger/node-webgl#platforms).
|
|
71
83
|
|
|
72
84
|
## Useful for
|
|
73
85
|
|
|
@@ -146,24 +158,18 @@ it('renders a cube', async () => {
|
|
|
146
158
|
- [`demo/`](https://github.com/bhouston/vitest-gpu/tree/main/demo/test/webgl) for WebGL contexts, uploads, readbacks, three.js and Babylon.js tests, and the
|
|
147
159
|
[repository README](https://github.com/bhouston/vitest-gpu#readme).
|
|
148
160
|
|
|
149
|
-
##
|
|
161
|
+
## Resource cleanup
|
|
150
162
|
|
|
151
|
-
|
|
163
|
+
Environment teardown destroys contexts first created through `Canvas.getContext()` during the test file, including DOM canvases, `OffscreenCanvas` and imported canvas factories. It also cancels pending callbacks scheduled through its animation-frame shim. Contexts created directly with the WebGL context constructors, or before setup, remain the caller's responsibility: call `destroy()` on them. Dispose renderers and canvases during a long-running file to release resources sooner.
|
|
164
|
+
|
|
165
|
+
## Contribution
|
|
166
|
+
|
|
167
|
+
See [Contributing Guide](https://github.com/bhouston/vitest-gpu/blob/main/CONTRIBUTING.md).
|
|
152
168
|
|
|
153
169
|
## License
|
|
154
170
|
|
|
155
171
|
MIT
|
|
156
172
|
|
|
157
|
-
|
|
158
|
-
[npm-url]: https://www.npmjs.com/package/vitest-environment-webgl-node
|
|
159
|
-
[downloads-badge]: https://img.shields.io/npm/dm/vitest-environment-webgl-node.svg
|
|
160
|
-
[tests-badge]: https://github.com/bhouston/vitest-gpu/actions/workflows/ci.yml/badge.svg
|
|
161
|
-
[tests-url]: https://github.com/bhouston/vitest-gpu/actions/workflows/ci.yml
|
|
162
|
-
[discord-badge]: https://img.shields.io/badge/Discord-Join%20Chat-5865F2?logo=discord&logoColor=white
|
|
163
|
-
[discord-url]: https://discord.gg/fwupDN493R
|
|
164
|
-
[coverage-badge]: https://codecov.io/gh/bhouston/vitest-gpu/branch/main/graph/badge.svg
|
|
165
|
-
[coverage-url]: https://codecov.io/gh/bhouston/vitest-gpu
|
|
166
|
-
|
|
167
|
-
## Resource cleanup
|
|
173
|
+
## Author
|
|
168
174
|
|
|
169
|
-
|
|
175
|
+
[Ben Houston](https://ben3d.ca), Sponsored by [Land of Assets](https://landofassets.com).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
1
|
import { type InitOptions, type InstallDOMOptions } from '@onirenaud/node-webgl';
|
|
2
2
|
/** Set under `test.environmentOptions.webglNode` in your Vitest config. */
|
|
3
3
|
export type WebglNodeOptions = InitOptions & InstallDOMOptions;
|
|
4
|
+
/**
|
|
5
|
+
* The Vitest environment registered as `webgl-node`. Before each test file it installs node-webgl's `window`,
|
|
6
|
+
* `document`, `HTMLCanvasElement`, `Image`, `requestAnimationFrame` and `WebGL*` classes plus a file-reading
|
|
7
|
+
* `fetch()`, and it restores the original globals afterwards.
|
|
8
|
+
*/
|
|
4
9
|
declare const _default: {
|
|
10
|
+
/** The name Vitest uses to select this environment: `test.environment: 'webgl-node'`. */
|
|
5
11
|
name: string;
|
|
12
|
+
/** Runs test files through Vite's `ssr` environment, as Node code. */
|
|
6
13
|
viteEnvironment: "ssr";
|
|
14
|
+
/**
|
|
15
|
+
* Installs the GL display and the DOM globals on the test file's global object.
|
|
16
|
+
*
|
|
17
|
+
* @param global - The global object of the test file's context.
|
|
18
|
+
* @param __namedParameters - The environment options from the Vitest config; `webglNode` holds {@link WebglNodeOptions}.
|
|
19
|
+
* @returns A `teardown` hook that destroys contexts and restores the globals this call changed.
|
|
20
|
+
*/
|
|
7
21
|
setup(global: typeof globalThis, { webglNode }: {
|
|
8
22
|
webglNode?: WebglNodeOptions;
|
|
9
23
|
}): {
|
|
10
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Destroys the WebGL contexts created during the test file, cancels pending animation-frame callbacks and
|
|
26
|
+
* restores the globals that `setup` replaced.
|
|
27
|
+
*
|
|
28
|
+
* @param g - The global object of the test file's context.
|
|
29
|
+
*/
|
|
30
|
+
teardown: (g: Record<string, unknown>) => void;
|
|
11
31
|
};
|
|
12
32
|
};
|
|
13
33
|
export default _default;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACvB,MAAM,uBAAuB,CAAC;AAM/B,2EAA2E;AAC3E,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,iBAAiB,CAAC;AA+E/D;;;;GAIG;;IAED,yFAAyF;;IAEzF,sEAAsE;;IAEtE;;;;;;OAMG;kBACW,OAAO,UAAU,iBAAsB;QAAE,SAAS,CAAC,EAAE,gBAAgB,CAAA;KAAE;QA0FjF;;;;;WAKG;sBACW,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;;;AA5G3C,wBAyHwB"}
|
package/dist/index.js
CHANGED
|
@@ -73,9 +73,23 @@ const installFetch = (global, baseDir) => {
|
|
|
73
73
|
};
|
|
74
74
|
Object.defineProperty(global, 'fetch', { value: localFetch, writable: true, configurable: true });
|
|
75
75
|
};
|
|
76
|
+
/**
|
|
77
|
+
* The Vitest environment registered as `webgl-node`. Before each test file it installs node-webgl's `window`,
|
|
78
|
+
* `document`, `HTMLCanvasElement`, `Image`, `requestAnimationFrame` and `WebGL*` classes plus a file-reading
|
|
79
|
+
* `fetch()`, and it restores the original globals afterwards.
|
|
80
|
+
*/
|
|
76
81
|
export default {
|
|
82
|
+
/** The name Vitest uses to select this environment: `test.environment: 'webgl-node'`. */
|
|
77
83
|
name: 'webgl-node',
|
|
84
|
+
/** Runs test files through Vite's `ssr` environment, as Node code. */
|
|
78
85
|
viteEnvironment: 'ssr',
|
|
86
|
+
/**
|
|
87
|
+
* Installs the GL display and the DOM globals on the test file's global object.
|
|
88
|
+
*
|
|
89
|
+
* @param global - The global object of the test file's context.
|
|
90
|
+
* @param __namedParameters - The environment options from the Vitest config; `webglNode` holds {@link WebglNodeOptions}.
|
|
91
|
+
* @returns A `teardown` hook that destroys contexts and restores the globals this call changed.
|
|
92
|
+
*/
|
|
79
93
|
setup(global, { webglNode = {} }) {
|
|
80
94
|
const { backend, api, ...dom } = webglNode;
|
|
81
95
|
if (backend || api) {
|
|
@@ -111,6 +125,7 @@ export default {
|
|
|
111
125
|
// Intercept the shared canvas API so imported factories and OffscreenCanvas are covered too.
|
|
112
126
|
const canvasBefore = snapshot(Canvas.prototype);
|
|
113
127
|
nested.push({ target: Canvas.prototype, before: canvasBefore });
|
|
128
|
+
// oxlint-disable-next-line typescript/unbound-method -- Canvas.prototype.getContext is patched/compared by identity, never called unbound
|
|
114
129
|
const getContext = Canvas.prototype.getContext;
|
|
115
130
|
Canvas.prototype.getContext = function (...args) {
|
|
116
131
|
if (disposed)
|
|
@@ -171,7 +186,13 @@ export default {
|
|
|
171
186
|
}
|
|
172
187
|
const changed = changedProperties(before, global);
|
|
173
188
|
return {
|
|
174
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Destroys the WebGL contexts created during the test file, cancels pending animation-frame callbacks and
|
|
191
|
+
* restores the globals that `setup` replaced.
|
|
192
|
+
*
|
|
193
|
+
* @param g - The global object of the test file's context.
|
|
194
|
+
*/
|
|
195
|
+
teardown: (g) => {
|
|
175
196
|
if (disposed)
|
|
176
197
|
return;
|
|
177
198
|
try {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|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5B,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC;oBAClB,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjB,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACoB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitest-environment-webgl-node",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "Vitest environment for real headless WebGL 1/2 tests in Node (ANGLE via @onirenaud/node-webgl): no browser, Playwright or canvas mocks",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"babylonjs",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
"node": ">=22"
|
|
67
67
|
},
|
|
68
68
|
"scripts": {
|
|
69
|
-
"build": "tsc",
|
|
70
|
-
"tsc": "tsc"
|
|
69
|
+
"build": "tsc -p tsconfig.build.json",
|
|
70
|
+
"tsc": "tsc --noEmit"
|
|
71
71
|
}
|
|
72
72
|
}
|
package/src/index.ts
CHANGED
|
@@ -91,9 +91,23 @@ const installFetch = (global: typeof globalThis, baseDir: string): void => {
|
|
|
91
91
|
Object.defineProperty(global, 'fetch', { value: localFetch, writable: true, configurable: true });
|
|
92
92
|
};
|
|
93
93
|
|
|
94
|
+
/**
|
|
95
|
+
* The Vitest environment registered as `webgl-node`. Before each test file it installs node-webgl's `window`,
|
|
96
|
+
* `document`, `HTMLCanvasElement`, `Image`, `requestAnimationFrame` and `WebGL*` classes plus a file-reading
|
|
97
|
+
* `fetch()`, and it restores the original globals afterwards.
|
|
98
|
+
*/
|
|
94
99
|
export default {
|
|
100
|
+
/** The name Vitest uses to select this environment: `test.environment: 'webgl-node'`. */
|
|
95
101
|
name: 'webgl-node',
|
|
102
|
+
/** Runs test files through Vite's `ssr` environment, as Node code. */
|
|
96
103
|
viteEnvironment: 'ssr',
|
|
104
|
+
/**
|
|
105
|
+
* Installs the GL display and the DOM globals on the test file's global object.
|
|
106
|
+
*
|
|
107
|
+
* @param global - The global object of the test file's context.
|
|
108
|
+
* @param __namedParameters - The environment options from the Vitest config; `webglNode` holds {@link WebglNodeOptions}.
|
|
109
|
+
* @returns A `teardown` hook that destroys contexts and restores the globals this call changed.
|
|
110
|
+
*/
|
|
97
111
|
setup(global: typeof globalThis, { webglNode = {} }: { webglNode?: WebglNodeOptions }) {
|
|
98
112
|
const { backend, api, ...dom } = webglNode;
|
|
99
113
|
if (backend || api) {
|
|
@@ -129,6 +143,7 @@ export default {
|
|
|
129
143
|
// Intercept the shared canvas API so imported factories and OffscreenCanvas are covered too.
|
|
130
144
|
const canvasBefore = snapshot(Canvas.prototype);
|
|
131
145
|
nested.push({ target: Canvas.prototype, before: canvasBefore });
|
|
146
|
+
// oxlint-disable-next-line typescript/unbound-method -- Canvas.prototype.getContext is patched/compared by identity, never called unbound
|
|
132
147
|
const getContext = Canvas.prototype.getContext;
|
|
133
148
|
Canvas.prototype.getContext = function (this: Canvas, ...args: Parameters<Canvas['getContext']>) {
|
|
134
149
|
if (disposed) throw new Error('WebGL environment has been torn down');
|
|
@@ -183,7 +198,13 @@ export default {
|
|
|
183
198
|
}
|
|
184
199
|
const changed = changedProperties(before, global);
|
|
185
200
|
return {
|
|
186
|
-
|
|
201
|
+
/**
|
|
202
|
+
* Destroys the WebGL contexts created during the test file, cancels pending animation-frame callbacks and
|
|
203
|
+
* restores the globals that `setup` replaced.
|
|
204
|
+
*
|
|
205
|
+
* @param g - The global object of the test file's context.
|
|
206
|
+
*/
|
|
207
|
+
teardown: (g: Record<string, unknown>) => {
|
|
187
208
|
if (disposed) return;
|
|
188
209
|
try {
|
|
189
210
|
dispose();
|