The Node.js and TypeScript realization of the testing principles (see the `testing` skill for the why). Use when creating or reviewing the tests and checks of any Node or TypeScript project, with or without a framework, on the server or in the browser: Vitest for unit tests, test file layout and names, test data (Object Mother), fakes and fake timers, Playwright for browser, e2e or end-to-end tests against a production build, accessibility with axe and the WCAG level that its tags really chec...
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Testing Node?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/jorgecostamacia-testing-node)
---
name: testing-node
description: >-
The Node.js and TypeScript realization of the testing principles (see the
`testing` skill for the why). Use when creating or reviewing the tests and
checks of any Node or TypeScript project, with or without a framework, on
the server or in the browser: Vitest for unit tests, test file layout and
names, test data (Object Mother), fakes and fake timers, Playwright for
browser, e2e or end-to-end tests against a production build,
accessibility with axe and the WCAG level that its tags really check,
keyboard, focus and focus order tests, and the order of the gates in CI.
A framework's skill adds how to mount its components.
---
# Testing in Node and TypeScript
The Node *how* of `testing` (load it for the *why*). Vitest runs the unit tests and Playwright runs the browser tests. A framework's skill adds how to configure Vitest for that framework and how to mount its components.
## The stack
- **Vitest** for unit tests. It is the default test runner of Vite projects; how a framework sets it up comes from the framework's own skill or defaults. A plain Node library uses it too. Vitest 5 needs `vite` as a devDependency: it is a peer dependency of Vitest, not a dependency.
- **Playwright** for browser tests, with **`@axe-core/playwright`** for accessibility.
## Layout
- A unit test sits next to its unit: `price.test.ts` beside `price.ts`. One file per unit.
- The browser tests live in `e2e/`, out of `src/`. They test the application, not one unit.
- Vitest finds only the unit tests and Playwright only the browser tests. Set `include` in each config, so neither runner picks up the other's files.
## Names
`describe` names the unit. Each `test` states one behavior as a sentence: `test('rejects a negative amount')`. A name that needs "and" for two acts is two tests.
## Unit tests
```ts
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
include: ['src/**/*.test.ts'],
},
});
```
- When the framework has its own Vite config, take the Vitest config from it, so the tests resolve aliases and virtual modules as the application does. The framework's skill says how.
- Import `describe`, `test` and `expect` from `vitest`. Do not turn on `globals`: an explicit import shows where each name comes from and needs no global types.
- A double is a hand-written fake first. Use `vi.fn()` or `vi.spyOn()` only for what you cannot control, or to assert an outbound call that is the behavior itself.
- When the code reads the clock or a timer, control it with `vi.useFakeTimers()` and `vi.setSystemTime()`. Restore the real timers in `afterEach`. A test that waits for real time is a flake.
- CI runs `vitest run`, so the script does not depend on how Vitest detects CI.
## Test data
The Object Mother of `testing` is the default here too. A mother is a module of functions next to the tests that use it: `invoices.ts` beside the tests of the invoice code, or in a shared `test/` folder when several units use it.
```ts
export function overdueInvoice(overrides: Partial<Invoice> = {}): Invoice {
return { id: 'inv-1', amount: 100, dueDate: new Date('2026-01-01'), paid: false, ...overrides };
}
```
- A function names a state (`overdueInvoice`, `paidInvoice`), not a field tweak.
- The optional `overrides` argument is the builder: a test names only the fields that it varies (`overdueInvoice({ amount: 0 })`). Do not write a fluent builder type.
- Each function returns a new object, so no test changes the data of another.
## Browser tests
Run Playwright against a production build served locally, not against a dev server. A dev server adds code and skips steps (bundling, minifying, headers) that the real site has.
```ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: 'e2e',
forbidOnly: !!process.env.CI,
use: { baseURL: 'http://localhost:4173' },
webServer: {
command: 'pnpm preview',
url: 'http://localhost:4173',
reuseExistingServer: !process.env.CI,
},
});
```
- `webServer` starts the server that serves the build. Set the port and the command to the project's own.
- `forbidOnly` fails CI when a `test.only` was committed, so the run cannot skip the rest in silence.
- Abort every request to another host. With `const origin = new URL(baseURL).origin`, call `page.route((url) => url.origin !== origin, (route) => route.abort())`. A test then never depends on a third party.
- Find elements by role, label or text (`getByRole`, `getByLabel`), not by CSS class. A user finds them that way, and a style change does not break the test.
- Use Playwright's assertions that wait (`await expect(locator).toBeVisible()`). Never wait for a fixed time.
## Accessibility
axe checks only the part of WCAG that a machine can test. So both halves below are required: axe, and the keyboard and focus tests.
Run axe on every page, or on one page of each template, at a phone width and a desktop width, with the tags of the level that the project claims:
```ts
import AxeBuilder from '@axe-core/playwright';
const WCAG_22_AA = ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa', 'wcag22aa'];
const results = await new AxeBuilder({ page }).withTags(WCAG_22_AA).analyze();
expect(results.violations).toEqual([]);
```
- The tags are the level that you check. For WCAG 2.2 AA, pass all five. With only the 2.1 tags, the 2.2 rules do not run.
- When you disable a rule, write its reason next to it.
- axe does not test hidden content (a closed menu, a closed dialog). Open it, then run axe again.
Write Playwright tests for what axe cannot check:
- **Keyboard:** each control can be reached with Tab and used with the keyboard (Enter, Space, Escape, the arrow keys where the pattern needs them). A menu or a dialog opens and closes from the keyboard.
- **Visible focus:** the focused element shows a visible indicator. Assert that its outline or its box shadow is not `none`.
- **Focus order:** Tab moves through the page in reading order. A dialog moves the focus into itself, keeps it there, and returns it when it closes.
```ts
await page.keyboard.press('Tab');
await expect(page.getByRole('link', { name: 'Skip to content' })).toBeFocused();
```
## The gates in CI
The gates run in this order, the cheap ones first. The format, lint and type checks of `code-style-node` run before any test.
1. Install with the frozen lockfile.
2. Dependency audit (`pnpm audit`, from `low`; see `build-node`).
3. Format check, lint and type check (`code-style-node`).
4. Unit tests.
5. Build.
6. Browser tests against that build.
Each pipeline ships the build that it tested. A framework can build each environment apart: then the release run builds and tests again, and ships that build.