@helderberto/vitest-config 0.1.0 → 0.1.1
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 +1 -1
- package/README.md +65 -1
- package/dist/react-setup.js +15 -9
- package/package.json +1 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,5 +1,69 @@
|
|
|
1
1
|
# @helderberto/vitest-config
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@helderberto/vitest-config)
|
|
4
|
+
|
|
3
5
|
Vitest presets for the `node-app`, `react-app`, `react-library` and `ts-library` profiles.
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
Every preset turns globals on, resets mocks, env and globals between tests, and measures coverage with v8.
|
|
8
|
+
|
|
9
|
+
`node-app` and `ts-library` run in Node. `react-app` and `react-library` run in jsdom, with:
|
|
10
|
+
|
|
11
|
+
- jest-dom matchers
|
|
12
|
+
- Testing Library cleanup after each test
|
|
13
|
+
- `ResizeObserver`, `scrollIntoView` and `<dialog>` filled in where jsdom lacks them
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install -D @helderberto/vitest-config vitest @vitest/coverage-v8
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
React presets also need:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install -D jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Use
|
|
28
|
+
|
|
29
|
+
### `node-app`
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// vitest.config.ts
|
|
33
|
+
import { nodeApp } from '@helderberto/vitest-config/node-app';
|
|
34
|
+
import { defineConfig, mergeConfig } from 'vitest/config';
|
|
35
|
+
|
|
36
|
+
export default mergeConfig(nodeApp, defineConfig({}));
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### `react-app`
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// vitest.config.ts
|
|
43
|
+
import { reactApp } from '@helderberto/vitest-config/react-app';
|
|
44
|
+
import { defineConfig, mergeConfig } from 'vitest/config';
|
|
45
|
+
|
|
46
|
+
export default mergeConfig(reactApp, defineConfig({}));
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### `react-library`
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
// vitest.config.ts
|
|
53
|
+
import { reactLibrary } from '@helderberto/vitest-config/react-library';
|
|
54
|
+
import { defineConfig, mergeConfig } from 'vitest/config';
|
|
55
|
+
|
|
56
|
+
export default mergeConfig(reactLibrary, defineConfig({}));
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### `ts-library`
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
// vitest.config.ts
|
|
63
|
+
import { tsLibrary } from '@helderberto/vitest-config/ts-library';
|
|
64
|
+
import { defineConfig, mergeConfig } from 'vitest/config';
|
|
65
|
+
|
|
66
|
+
export default mergeConfig(tsLibrary, defineConfig({}));
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Project settings, such as coverage thresholds, go in the second `defineConfig`.
|
package/dist/react-setup.js
CHANGED
|
@@ -8,12 +8,18 @@ class NoopResizeObserver {
|
|
|
8
8
|
unobserve() { }
|
|
9
9
|
disconnect() { }
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
function fillBrowserApis() {
|
|
12
|
+
globalThis.ResizeObserver ??= NoopResizeObserver;
|
|
13
|
+
Element.prototype.scrollIntoView ??= () => { };
|
|
14
|
+
HTMLDialogElement.prototype.showModal ??= function showModal() {
|
|
15
|
+
this.open = true;
|
|
16
|
+
};
|
|
17
|
+
HTMLDialogElement.prototype.close ??= function close() {
|
|
18
|
+
this.open = false;
|
|
19
|
+
this.dispatchEvent(new Event('close'));
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
// A test file can opt into `@vitest-environment node`, where there's no DOM to fill in.
|
|
23
|
+
if (typeof Element !== 'undefined') {
|
|
24
|
+
fillBrowserApis();
|
|
25
|
+
}
|