Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"./packages/design-system/**/*.figma.batch.ts",
"./packages/design-system/types/**/figma.d.ts",
"./packages/design-system/src/components/ds-file-upload/stories/adapters/simple-file-upload-adapter.ts",
"./packages/eslint-plugin-internal/src/rules/__tests__/fixtures/*.ts"
"./packages/eslint-plugin-internal/src/rules/__tests__/fixtures/*.ts",
"./packages/design-system/vitest/jsdom-26.mjs"
],
"ignoreDependencies": ["jsdom-26"],
"ignoreExportsUsedInFile": {
"interface": true,
"type": true
Expand Down
4 changes: 4 additions & 0 deletions packages/design-system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export const App = () => {
);
};
```

## Testing with JSDOM

Components work in JSDOM-based test runners (Vitest or Jest with `jsdom`) once your test setup stubs a few browser APIs. See [Guidelines/Testing](https://drivenets.github.io/design-system/?path=/docs/guidelines-testing--docs) for the setup and known limitations.
8 changes: 7 additions & 1 deletion packages/design-system/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
"generate-scss-dts": "typed-scss-modules ./src -o ./.scss-dts --nameFormat all --exportType default > /dev/null 2>&1",
"figma:lint": "figma connect parse --exit-on-unreadable-files",
"figma:publish": "figma connect publish --exit-on-unreadable-files",
"test": "vitest --project=unit --project=browser --project=storybook",
"test": "vitest --project=unit --project=jsdom --project=jsdom-26 --project=browser --project=storybook",
"test:coverage": "pnpm test --coverage",
"test:unit": "vitest --project=unit",
"test:jsdom": "vitest --project=jsdom --project=jsdom-26",
"test:browser": "vitest --project=browser",
"test:requires-build": "vitest --project=requires-build",
"test:storybook-docs": "vitest --project=storybook-docs",
Expand Down Expand Up @@ -104,6 +105,9 @@
"@storybook/react-vite": "^10.6.7",
"@tanstack/react-query": "^5.108.0",
"@tanstack/react-router": "^1.170.45",
"@testing-library/dom": "^10.4.2",
"@testing-library/react": "^16.3.3",
"@testing-library/user-event": "^14.6.13",
"@tsdown/css": "0.23.4",
"@types/eslint-plugin-jsx-a11y": "^6.10.1",
"@types/react": "^19.2.18",
Expand All @@ -119,6 +123,8 @@
"eslint-plugin-react": "^7.37.5",
"eslint-plugin-react-hooks": "^7.1.1",
"eslint-plugin-storybook": "^10.6.7",
"jsdom": "^31.0.1",
Comment thread
mmurawski-dn marked this conversation as resolved.
Resolved
"jsdom-26": "npm:jsdom@26.1.0",
"material-symbols": "0.49.0",
"mockdate": "^3.0.5",
"playwright": "^1.65.1",
Expand Down
74 changes: 74 additions & 0 deletions packages/design-system/src/stories/testing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Meta } from '@storybook/addon-docs/blocks';

<Meta title="Guidelines/Testing" />

# Testing with JSDOM

Components render and respond to user events in JSDOM-based test runners (Vitest or Jest with `jsdom`). JSDOM lacks a
few browser APIs the components use, so add these stubs to your test setup file:

```typescript
// vitest.setup.ts or jest.setup.ts
window.matchMedia = (query: string) => ({
matches: false,
media: query,
onchange: null,
addEventListener: () => {},
removeEventListener: () => {},
addListener: () => {},
removeListener: () => {},
dispatchEvent: () => false,
});

globalThis.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};

Element.prototype.scrollTo = () => {};
Element.prototype.scrollIntoView = () => {};
```

Without them:

- Components with responsive props (`DsButton`, `DsStack`, `DsGrid`, `DsDrawer`, `DsPanel` and anything built on
them, such as `DsDatePicker` and `DsTable`) throw `window.matchMedia is not a function` on render.
- Opening `DsSelect`, `DsAutocomplete` or another positioned overlay throws `ResizeObserver is not defined`, and so do
components that measure their content, such as `DsTable`.
- `DsSelect` never commits the chosen option, and `DsTimePicker` throws when it opens (`scrollTo`).
- `DsCommentThread` throws on render (`scrollIntoView`).

## Older JSDOM versions

Jest 29 and 30 ship jsdom 20 and 26.

- Before jsdom 30 there is no `CSS.escape`. Keyboard navigation in `DsAutocomplete` and `DsRadioGroup`, and
`DsTable` column sizing, call it and throw `CSS is not defined`. `@testing-library/jest-dom` installs a polyfill;
without it, install `css.escape` and import it in your setup file.
- Before jsdom 27, highlighted option text is split, so `DsAutocomplete` options are named "Ban ana" instead of
"Banana". Match on text content instead
(`getByRole('option', { name: (_name, element) => element.textContent.includes('Banana') })`), or use jsdom 27 or
later.

## CSS imports in Jest

If a test imports `@drivenets/design-system/index.min.css`, map CSS files to a stub in Jest
(`moduleNameMapper: { '\\.css$': '<rootDir>/style-stub.js' }`). Vitest handles CSS imports on its own.

## Known limitations

- JSDOM does no layout. Elements have no size, so a `DsTable` with `virtualized` set renders no data rows. Leave
`virtualized` off in tests that check rows.
- Truncation (tooltips on truncated text), overlay positioning and scroll behavior can't be asserted. Cover them in a
real browser (for example Vitest browser mode or Playwright).
- The `matchMedia` stub never matches, so responsive props resolve to the `md` breakpoint. Return `matches: true` to
test `lg`.
- The `DsSlider` thumb appears one tick after render, so find it with `findByRole('slider')`.
- `DsCodeInput` logs JSDOM's "Not implemented: HTMLCanvasElement's getContext()" message. It is harmless.
Comment thread
mmurawski-dn marked this conversation as resolved.
Resolved

## Writing tests

Query components by role and accessible name (`getByRole('combobox')`, `getByRole('dialog', { name: 'Title' })`) and
drive them with `@testing-library/user-event`. The design system runs its own scenarios with exactly this setup, on
jsdom 26 and the latest jsdom, in [`tests/jsdom`](https://github.com/drivenets/design-system/tree/main/packages/design-system/tests/jsdom).
Loading
Loading