Vue + Testing Library
Test like a user: render, screen, user-event and jest-dom. This page shows exactly what pickstack adds to a Vue project for Testing Library: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.
$ npm create pickstack@latest my-app -- --framework vue --modules testing-library --yesCustomize in the configurator
Packages it adds
| Package | Version | Type |
|---|---|---|
@testing-library/dom | ^10.4.2 | dev |
@testing-library/jest-dom | ^8.0.0 | dev |
@testing-library/user-event | ^14.6.13 | dev |
@testing-library/vue | ^8.1.0 | dev |
@types/jsdom | ^30.0.0 | dev |
@vue/test-utils | ^2.6.0 | dev |
jsdom | ^31.0.1 | dev |
vitest | ^5.1.1 | dev |
New files
import userEvent from "@testing-library/user-event";
import { render, screen } from "@testing-library/vue";
import { describe, expect, it } from "vitest";
import CounterCard from "../CounterCard.vue";
describe("CounterCard", () => {
it("increments when +1 is clicked", async () => {
const user = userEvent.setup();
render(CounterCard);
await user.click(screen.getByRole("button", { name: "+1" }));
expect(screen.getByText("1")).toBeInTheDocument();
});
});
import "@testing-library/jest-dom/vitest";
import { cleanup } from "@testing-library/vue";
import { afterEach } from "vitest";
afterEach(() => {
cleanup();
});
{
"extends": "./tsconfig.app.json",
"include": ["src/**/__tests__/*", "src/test-setup.ts", "env.d.ts"],
"exclude": [],
"compilerOptions": {
"lib": [],
"types": ["node", "jsdom"],
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.vitest.tsbuildinfo"
}
}
import { fileURLToPath } from "node:url";
import { defineConfig, mergeConfig } from "vitest/config";
import viteConfig from "./vite.config.ts";
export default mergeConfig(
viteConfig,
defineConfig({
test: {
environment: "jsdom",
setupFiles: ["./src/test-setup.ts"],
root: fileURLToPath(new URL("./", import.meta.url)),
},
}),
);
Changes to existing files
{ "extends": "@vue/tsconfig/tsconfig.dom.json", "include": ["env.d.ts", "src/**/*", "src/**/*.vue"],+ "exclude": ["src/**/__tests__/*", "src/test-setup.ts"],− "exclude": ["src/**/__tests__/*"], "compilerOptions": { "noUncheckedIndexedAccess": true, "paths": {⋯ 6 unchanged lines
⋯ 5 unchanged lines }, { "path": "./tsconfig.app.json"+ },+ {+ "path": "./tsconfig.vitest.json" } ] }⋯ 1 unchanged lines
Project files
.gitignoreREADME.mdenv.d.tsindex.htmlpackage.jsonsrc/App.vuesrc/assets/main.csssrc/components/CounterCard.vuesrc/components/__tests__/CounterCard.spec.tssrc/main.tssrc/test-setup.tstsconfig.app.jsontsconfig.jsontsconfig.node.jsontsconfig.vitest.jsonvite.config.tsvitest.config.ts