Skip to content

Commit 22d6c5d

Browse files
chrisgervangclaude
andcommitted
docs(test-utils): add Vitest setup guide and examples
Addresses community feedback (discussion #10223) by documenting the `@deck.gl/test-utils/vitest` entry point with setup instructions and usage examples for testLayer and generateLayerTests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6795cc9 commit 22d6c5d

3 files changed

Lines changed: 151 additions & 5 deletions

File tree

docs/api-reference/test-utils/generate-layer-tests.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,32 @@
22

33
This utility generates a series of test cases to be used with [testLayer](../test-utils/test-layer.md) that checks the conformance of a layer class.
44

5-
## Example
5+
## Example (Vitest)
66

7-
Example of layer unit tests using `tape`. The test utility itself is test framework agnostic.
7+
```ts
8+
import {test, expect} from 'vitest';
9+
import {testLayer, generateLayerTests} from '@deck.gl/test-utils/vitest';
10+
import {GeoJsonLayer} from '@deck.gl/layers';
11+
12+
test('GeoJsonLayer#conformance', () => {
13+
const testCases = generateLayerTests({
14+
Layer: GeoJsonLayer,
15+
sampleProps: {
16+
data: SAMPLE_GEOJSON
17+
},
18+
assert: (cond, msg) => expect(cond, msg).toBeTruthy(),
19+
onAfterUpdate: ({layer, subLayers}) => {
20+
expect(layer.state.features).toBeTruthy();
21+
const expected = layer.props.stroked ? 2 : 1;
22+
expect(subLayers.length).toBe(expected);
23+
}
24+
});
25+
26+
testLayer({Layer: GeoJsonLayer, testCases, onError: err => expect(err).toBeFalsy()});
27+
});
28+
```
29+
30+
## Example (tape)
831

932
```js
1033
import test from 'tape-promise/tape';
@@ -33,7 +56,9 @@ test('GeoJsonLayer#tests', t => {
3356

3457
## Usage
3558

36-
```js
59+
```ts
60+
import {generateLayerTests} from '@deck.gl/test-utils/vitest';
61+
3762
generateLayerTests({Layer, sampleProps, assert, onError});
3863
```
3964

docs/api-reference/test-utils/overview.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,79 @@ yarn add -D @deck.gl/test-utils
2020
You typically want the major and minor version of ` @deck.gl/test-utils` to match the version of `@deck.gl/core` that you are using. i.e. you want to use `9.0.x` and `9.0.y` together. Check and if necessary edit your `package.json` to make sure things align.
2121

2222

23+
## Using with Vitest
24+
25+
`@deck.gl/test-utils` ships a dedicated Vitest entry point at `@deck.gl/test-utils/vitest` that automatically wires up `vi.spyOn()` for layer method spies. This is the recommended way to test deck.gl layers with Vitest.
26+
27+
### Quick Start
28+
29+
```bash
30+
npm install --save-dev @deck.gl/test-utils vitest @vitest/browser playwright
31+
```
32+
33+
Import from the vitest-specific entry point:
34+
35+
```ts
36+
import {test, expect} from 'vitest';
37+
import {testLayer, generateLayerTests} from '@deck.gl/test-utils/vitest';
38+
```
39+
40+
### Setup
41+
42+
Layer tests require a real WebGL2 context, so tests must run in [Vitest Browser Mode](https://vitest.dev/guide/browser/) with Playwright. JSDOM does not support WebGL and is not a supported environment for `@deck.gl/test-utils`.
43+
44+
Install browser testing dependencies:
45+
46+
```bash
47+
npm install --save-dev @vitest/browser playwright
48+
```
49+
50+
Configure `vitest.config.ts`:
51+
52+
```ts
53+
import {defineConfig} from 'vitest/config';
54+
import {playwright} from '@vitest/browser-playwright';
55+
56+
export default defineConfig({
57+
test: {
58+
browser: {
59+
enabled: true,
60+
provider: playwright({
61+
launchOptions: {
62+
args: ['--use-angle=swiftshader', '--enable-unsafe-swiftshader']
63+
}
64+
}),
65+
instances: [{browser: 'chromium'}],
66+
headless: true
67+
}
68+
}
69+
});
70+
```
71+
72+
Write a test:
73+
74+
```ts
75+
import {test, expect} from 'vitest';
76+
import {testLayer, generateLayerTests} from '@deck.gl/test-utils/vitest';
77+
import {ScatterplotLayer} from '@deck.gl/layers';
78+
79+
test('ScatterplotLayer', () => {
80+
const testCases = generateLayerTests({
81+
Layer: ScatterplotLayer,
82+
sampleProps: {
83+
data: [{position: [0, 0]}],
84+
getPosition: d => d.position
85+
},
86+
assert: (cond, msg) => expect(cond, msg).toBeTruthy()
87+
});
88+
89+
testLayer({Layer: ScatterplotLayer, testCases, onError: err => expect(err).toBeFalsy()});
90+
});
91+
```
92+
93+
See [testLayer](./test-layer.md) for the full API and more examples including custom layer tests.
94+
95+
2396
## Layer Conformance Tests
2497

2598
Layer conformance tests are designed to verify deck.gl that layers update their internal state correctly in response to various props and prop changes. The layer update test support includes test drivers to initialize a layer and then run a sequence of successive updates, with facilities for validating the layer after each change, and also provides functions to initialize, update and render layers in a test environment.

docs/api-reference/test-utils/test-layer.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,53 @@ The `testLayer` utility initializes a layer, test layer updates and draw calls o
44

55
The `testLayerAsync` utility is like `testLayer`, but designed for layers that need to resolve resources asynchronously.
66

7-
## Example
7+
## Example (Vitest)
8+
9+
Using the dedicated `@deck.gl/test-utils/vitest` entry point which provides built-in `vi.spyOn()` integration:
10+
11+
```ts
12+
import {test, expect} from 'vitest';
13+
import {testLayer} from '@deck.gl/test-utils/vitest';
14+
import {GeoJsonLayer} from '@deck.gl/layers';
15+
16+
test('GeoJsonLayer#tests', () => {
17+
testLayer({
18+
Layer: GeoJsonLayer,
19+
testCases: [
20+
// Test case 1
21+
{
22+
props: {data: []}
23+
},
24+
// Test case 2
25+
{
26+
props: {
27+
data: SAMPLE_GEOJSON
28+
},
29+
onAfterUpdate({layer, oldState, subLayers}) {
30+
expect(layer.state.features).not.toBe(oldState.features);
31+
expect(subLayers.length).toBe(2);
32+
}
33+
},
34+
// Test case 3
35+
{
36+
updateProps: {
37+
// will be merged with the previous props
38+
lineWidthScale: 3
39+
},
40+
onAfterUpdate({subLayers}) {
41+
const pathLayer = subLayers.find(l => l.id.endsWith('linestrings'));
42+
expect(pathLayer.props.widthScale).toBe(3);
43+
}
44+
}
45+
],
46+
onError: err => expect(err).toBeFalsy()
47+
});
48+
});
49+
```
50+
51+
See the [overview](./overview.md#using-with-vitest) for full Vitest setup instructions.
52+
53+
## Example (tape)
854

955
Example of layer unit tests using `tape`. The test utility itself is test framework agnostic.
1056

@@ -49,7 +95,9 @@ test('GeoJsonLayer#tests', t => {
4995

5096
## Usage
5197

52-
```js
98+
```ts
99+
import {testLayer, testLayerAsync} from '@deck.gl/test-utils/vitest';
100+
53101
testLayer({Layer, spies, testCases, onError});
54102
await testLayerAsync({Layer, spies, testCases, onError});
55103
```

0 commit comments

Comments
 (0)