This repository uses Vitest with workspace support for testing across all packages.
# Run all tests
yarn test
# Run tests in watch mode (interactive)
yarn test:watch
# Run tests with UI (browser-based test runner)
yarn test:ui
# Run tests in TDD mode (watch with minimal output)
yarn tdd
# Run tests with coverage
yarn coverTests are configured at the workspace level using vitest.workspace.ts:
// vitest.workspace.ts
export default defineWorkspace([
{
extends: "./packages/experience-editor/vitest.config.ts",
test: {
name: "experience-editor",
root: "./packages/experience-editor",
include: ["src/**/*.{test,spec}.{ts,tsx}"],
},
},
// Add more packages here...
]);// packages/your-package/vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "node", // or "jsdom" for React components
globals: true,
},
});// vitest.workspace.ts
export default defineWorkspace([
// ... existing packages
{
extends: "./packages/your-package/vitest.config.ts",
test: {
name: "your-package",
root: "./packages/your-package",
include: ["src/**/*.{test,spec}.{ts,tsx}"],
},
},
]);{
"scripts": {
"test": "vitest run",
"tdd": "vitest"
}
}yarn test- Run all tests onceyarn test:watch- Run tests in watch modeyarn test:ui- Open Vitest UI in browseryarn tdd- Watch mode with minimal outputyarn cover- Run with coverage report
yarn test:nx- Run tests via Nx (runs per-package scripts)yarn tdd:nx- Watch mode via Nxyarn cover:nx- Coverage via Nx
Note: Vitest workspace commands are preferred as they:
- Show all test results in one place
- Support cross-package testing
- Provide better filtering and watch mode
- Work without Nx Cloud setup
When running yarn test, you'll see output like:
✓ |experience-editor| src/utility/fieldLogic.test.ts (27 tests) 3ms
✓ |experience-editor| src/components/form/radioGroup.test.tsx (12 tests) 113ms
Test Files 2 passed (2)
Tests 39 passed (39)
The |package-name| prefix shows which package each test file belongs to.
// src/utils/myFunction.test.ts
import { describe, it, expect } from "vitest";
import { myFunction } from "./myFunction";
describe("myFunction", () => {
it("should do something", () => {
expect(myFunction("input")).toBe("output");
});
});// src/components/MyComponent.test.tsx
import { describe, it, expect } from "vitest";
import { render, screen } from "@testing-library/react";
import { MyComponent } from "./MyComponent";
describe("MyComponent", () => {
it("should render", () => {
render(<MyComponent />);
expect(screen.getByText("Hello")).toBeInTheDocument();
});
});vitest- Test runner@vitest/coverage-v8- Coverage reporting
@testing-library/react- React component testing@testing-library/jest-dom- Extended matchersjsdom- DOM environment for tests
Make sure your test files match the pattern: **/*.{test,spec}.{ts,tsx}
Check that your vitest.config.ts includes the same path resolution as your
vite.config.ts
Ensure you have:
environment: "jsdom"in vitest.config.ts- jsdom installed:
yarn add -D jsdom - @testing-library/react installed
If using yarn test:nx and encountering Nx Cloud errors, either:
- Use
yarn test(Vitest workspace) instead - Configure Nx Cloud token
- Disable Nx Cloud in nx.json
- Colocate tests - Keep test files next to source files
- Descriptive names - Use clear test descriptions
- Test behavior, not implementation - Test what users see/do
- Fast tests - Keep unit tests under 10ms when possible
- Isolated tests - Each test should be independent
- Mock sparingly - Prefer real implementations when feasible
name: Test
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "22"
- run: yarn install
- run: yarn test
- run: yarn coverCoverage reports are generated in coverage/ directory:
yarn cover
# View HTML report
open coverage/index.html