vitest-browser-vue 0.0.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/README.md +59 -0
- package/dist/chunk-S4673CTL.js +48 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +19 -0
- package/dist/pure.d.ts +29 -0
- package/dist/pure.js +8 -0
- package/package.json +76 -0
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# vitest-browser-vue
|
|
2
|
+
|
|
3
|
+
Render Vue components in Vitest Browser Mode. This library follows `testing-library` principles and exposes only [locators](https://vitest.dev/guide/browser/locators) and utilities that encourage you to write tests that closely resemble how your Vue components are used.
|
|
4
|
+
|
|
5
|
+
Requires `vitest` and `@vitest/browser` 2.1.0 or higher.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { render } from 'vitest-browser-vue'
|
|
9
|
+
import { expect, test } from 'vitest'
|
|
10
|
+
|
|
11
|
+
test('counter button increments the count', async () => {
|
|
12
|
+
const screen = render(Component, {
|
|
13
|
+
props: {
|
|
14
|
+
initialCount: 1,
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
await screen.getByRole('button', { name: 'Increment' }).click()
|
|
19
|
+
|
|
20
|
+
await expect.element(screen.getByText('Count is 2')).toBeVisible()
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`vitest-browser-vue` also automatically injects `render` and `cleanup` methods on the `page`. Example:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// vitest.config.ts
|
|
28
|
+
import { defineConfig } from 'vitest/config'
|
|
29
|
+
|
|
30
|
+
export default defineConfig({
|
|
31
|
+
test: {
|
|
32
|
+
// if the types are not picked up, add `vitest-browser-vue` to
|
|
33
|
+
// "compilerOptions.types" in your tsconfig or
|
|
34
|
+
// import `vitest-browser-vue` manually so TypeScript can pick it up
|
|
35
|
+
setupFiles: ['vitest-browser-vue'],
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { page } from '@vitest/browser/context'
|
|
42
|
+
|
|
43
|
+
test('counter button increments the count', async () => {
|
|
44
|
+
const screen = page.render(Component, {
|
|
45
|
+
props: {
|
|
46
|
+
initialCount: 1,
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
screen.cleanup()
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Unlike `@testing-library/vue`, `vitest-browser-vue` cleans up the component before the test starts instead of after, so you can see the rendered result in your UI. To avoid auto-cleanup, import the `render` function from `vitest-browser-vue/pure`.
|
|
55
|
+
|
|
56
|
+
## Special thanks
|
|
57
|
+
|
|
58
|
+
- Powered by [`@vue/test-utils`](https://github.com/vuejs/test-utils/)
|
|
59
|
+
- Inspired by [`@testing-library/vue`](https://github.com/testing-library/vue-testing-library)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// src/pure.ts
|
|
2
|
+
import { mount } from "@vue/test-utils";
|
|
3
|
+
import { debug, getElementLocatorSelectors } from "@vitest/browser/utils";
|
|
4
|
+
var mountedWrappers = /* @__PURE__ */ new Set();
|
|
5
|
+
function render(Component, {
|
|
6
|
+
container: customContainer,
|
|
7
|
+
baseElement: customBaseElement,
|
|
8
|
+
...mountOptions
|
|
9
|
+
} = {}) {
|
|
10
|
+
const div = document.createElement("div");
|
|
11
|
+
const baseElement = customBaseElement || customContainer || document.body;
|
|
12
|
+
const container = customContainer || baseElement.appendChild(div);
|
|
13
|
+
if (mountOptions.attachTo) {
|
|
14
|
+
throw new Error("`attachTo` is not supported, use `container` instead");
|
|
15
|
+
}
|
|
16
|
+
const wrapper = mount(Component, {
|
|
17
|
+
...mountOptions,
|
|
18
|
+
attachTo: container
|
|
19
|
+
});
|
|
20
|
+
unwrapNode(wrapper.parentElement);
|
|
21
|
+
mountedWrappers.add(wrapper);
|
|
22
|
+
return {
|
|
23
|
+
container,
|
|
24
|
+
baseElement,
|
|
25
|
+
debug: (el = baseElement, maxLength, options) => debug(el, maxLength, options),
|
|
26
|
+
unmount: () => wrapper.unmount(),
|
|
27
|
+
emitted: (name) => wrapper.emitted(name),
|
|
28
|
+
rerender: (props) => wrapper.setProps(props),
|
|
29
|
+
...getElementLocatorSelectors(baseElement)
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function cleanup() {
|
|
33
|
+
mountedWrappers.forEach((wrapper) => {
|
|
34
|
+
if (wrapper.element?.parentNode?.parentNode === document.body) {
|
|
35
|
+
document.body.removeChild(wrapper.element.parentNode);
|
|
36
|
+
}
|
|
37
|
+
wrapper.unmount();
|
|
38
|
+
mountedWrappers.delete(wrapper);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function unwrapNode(node) {
|
|
42
|
+
node.replaceWith(...node.childNodes);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
render,
|
|
47
|
+
cleanup
|
|
48
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { render } from './pure.js';
|
|
2
|
+
export { ComponentRenderOptions, RenderResult, cleanup } from './pure.js';
|
|
3
|
+
import '@vitest/browser/context';
|
|
4
|
+
import '@vue/test-utils';
|
|
5
|
+
import 'vue';
|
|
6
|
+
import '@vitest/browser/utils';
|
|
7
|
+
|
|
8
|
+
declare module '@vitest/browser/context' {
|
|
9
|
+
interface BrowserPage {
|
|
10
|
+
render: typeof render;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { render };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cleanup,
|
|
3
|
+
render
|
|
4
|
+
} from "./chunk-S4673CTL.js";
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
import { page } from "@vitest/browser/context";
|
|
8
|
+
import { beforeEach } from "vitest";
|
|
9
|
+
page.extend({
|
|
10
|
+
render,
|
|
11
|
+
[Symbol.for("vitest:component-cleanup")]: cleanup
|
|
12
|
+
});
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
cleanup();
|
|
15
|
+
});
|
|
16
|
+
export {
|
|
17
|
+
cleanup,
|
|
18
|
+
render
|
|
19
|
+
};
|
package/dist/pure.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { LocatorSelectors, Locator } from '@vitest/browser/context';
|
|
2
|
+
import { ComponentMountingOptions } from '@vue/test-utils';
|
|
3
|
+
import { DefineComponent } from 'vue';
|
|
4
|
+
import { PrettyDOMOptions } from '@vitest/browser/utils';
|
|
5
|
+
|
|
6
|
+
type ComponentProps<T> = T extends new (...angs: any) => {
|
|
7
|
+
$props: infer P;
|
|
8
|
+
} ? NonNullable<P> : T extends (props: infer P, ...args: any) => any ? P : {};
|
|
9
|
+
interface RenderResult<Props> extends LocatorSelectors {
|
|
10
|
+
container: HTMLElement;
|
|
11
|
+
baseElement: HTMLElement;
|
|
12
|
+
debug(el?: HTMLElement | HTMLElement[] | Locator | Locator[], maxLength?: number, options?: PrettyDOMOptions): void;
|
|
13
|
+
unmount(): void;
|
|
14
|
+
emitted<T = unknown>(): Record<string, T[]>;
|
|
15
|
+
emitted<T = unknown[]>(eventName: string): undefined | T[];
|
|
16
|
+
rerender(props: Partial<Props>): void;
|
|
17
|
+
}
|
|
18
|
+
interface ComponentRenderOptions<C, P extends ComponentProps<C>> extends ComponentMountingOptions<C, P> {
|
|
19
|
+
container?: HTMLElement;
|
|
20
|
+
baseElement?: HTMLElement;
|
|
21
|
+
}
|
|
22
|
+
declare function render<T, C = T extends ((...args: any) => any) | (new (...args: any) => any) ? T : T extends {
|
|
23
|
+
props?: infer Props;
|
|
24
|
+
} ? DefineComponent<Props extends Readonly<(infer PropNames)[]> | (infer PropNames)[] ? {
|
|
25
|
+
[key in PropNames extends string ? PropNames : string]?: any;
|
|
26
|
+
} : Props> : DefineComponent, P extends ComponentProps<C> = ComponentProps<C>>(Component: T, { container: customContainer, baseElement: customBaseElement, ...mountOptions }?: ComponentRenderOptions<C, P>): RenderResult<P>;
|
|
27
|
+
declare function cleanup(): void;
|
|
28
|
+
|
|
29
|
+
export { type ComponentRenderOptions, type RenderResult, cleanup, render };
|
package/dist/pure.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vitest-browser-vue",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "Render Vue components in Vitest Browser Mode",
|
|
6
|
+
"author": "Vitest Team",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"funding": "https://opencollective.com/vitest",
|
|
9
|
+
"homepage": "https://github.com/vitest-dev/vitest-browser-vue#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/vitest-dev/vitest-browser-vue.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/vitest-dev/vitest-browser-vue/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"vue",
|
|
19
|
+
"vitest",
|
|
20
|
+
"browser",
|
|
21
|
+
"testing"
|
|
22
|
+
],
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./pure": {
|
|
29
|
+
"types": "./dist/pure.d.ts",
|
|
30
|
+
"default": "./dist/pure.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"main": "./dist/index.js",
|
|
34
|
+
"module": "./dist/index.js",
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"files": [
|
|
37
|
+
"*.d.ts",
|
|
38
|
+
"*.mjs",
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": "^18.0.0 || >=20.0.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@vitest/browser": "^2.1.0-beta.4",
|
|
46
|
+
"vitest": "^2.1.0-beta.4",
|
|
47
|
+
"vue": "^3.0.0"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@vue/test-utils": "^2.4.6"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@antfu/eslint-config": "^2.24.1",
|
|
54
|
+
"@vitejs/plugin-vue": "^5.1.2",
|
|
55
|
+
"@vitest/browser": "^2.1.0-beta.4",
|
|
56
|
+
"bumpp": "^9.4.2",
|
|
57
|
+
"changelogithub": "^0.13.9",
|
|
58
|
+
"eslint": "^9.8.0",
|
|
59
|
+
"playwright": "^1.46.0",
|
|
60
|
+
"tsup": "^8.2.4",
|
|
61
|
+
"tsx": "^4.16.5",
|
|
62
|
+
"typescript": "^5.5.4",
|
|
63
|
+
"vitest": "^2.1.0-beta.4",
|
|
64
|
+
"vue": "^3.4.35",
|
|
65
|
+
"zx": "^8.1.4"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "tsup",
|
|
69
|
+
"dev": "tsup --watch --sourcemap",
|
|
70
|
+
"test": "vitest",
|
|
71
|
+
"publish-ci": "tsx scripts/publish-ci.ts",
|
|
72
|
+
"release": "tsx scripts/release.ts",
|
|
73
|
+
"lint": "eslint --cache .",
|
|
74
|
+
"lint:fix": "pnpm lint --fix"
|
|
75
|
+
}
|
|
76
|
+
}
|