> To build a Wix site agentically, or add Wix features like bookings or a store to a site you already have, follow https://www.wix.com/headless/skill.md
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: Writing Tests

## Article: Writing Tests

## Article Link: https://dev.wix.com/docs/wix-design-system/contributing/writing-tests.md

## Article Content:

<!-- heading -->

# Writing Tests

<!-- end heading -->

<br />

> For a component library to be stable, it must be testable and tested.

## Test Types

### Component (Unit) Tests

- Tests run using [`jest`](https://facebook.github.io/jest/).
- These tests focus on component behavior and don't require visual rendering.
- Each component has an associated test file with a `.spec.tsx` extension (for example, `ComponentName.spec.tsx`).
- Each component has a driver, named `ComponentName.uni.driver.ts`.

#### Writing a unit test (example)

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```ts
import React from 'react';
import { createRendererWithUniDriver } from '../../utils/test-utils/react/index';
import { checkboxUniDriverFactory } from './Checkbox.private.uni.driver';
import Checkbox from '../Checkbox';

describe('Checkbox', () => {
  const render = createRendererWithUniDriver(checkboxUniDriverFactory);

  it('should be unchecked by default', async () => {
    const { driver } = render(<Checkbox />);
    expect(await driver.isChecked()).toBe(false);
  });
});

```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

### Visual Testing

#### Terminology

- `Storybook`: A playground for displaying React components with a navigation sidebar.
- `Sled`: A tool for visual testing in `Storybook`.

#### FAQ

**Q: How do I create visual test files?**

**A:** Create a new file `MyComponent/test/MyComponent.visual.tsx`:

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```tsx
import React from 'react';
import { storiesOf } from '@wix/wix-storybook-utils/dynamic-stories';
import MyComponent from '../';

storiesOf('MyComponent/size', module).add('large', () => (
  <MyComponent size="large" />
));
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

---

**Q: What do I test in visual tests?**

**A:** Styling variations in different states (for example, props like: `size`, `skin`, `error message`...).

---

**Q: How do I test interactions in visual tests?**

**A:** For asynchronous actions (for example, waiting for a tooltip to appear), use `Sled e2e` tests.

_Warning: Interactions that aren't immediate (like animations) can cause flaky tests._

---

**Q: How to run visual tests locally?**

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
pnpm test:visuals:[TARGET_THEME]
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

or

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
npm run test:visuals:[TARGET_THEME]
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

---

**Q: How can I update a baseline if I see a diff?**

**A:** There are two ways you can accept a baseline.

1. Run locally:
<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
pnpm test:visuals:[TARGET_THEME] -- -u
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

or

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
npm run test:visuals:[TARGET_THEME] -- -u
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

2. In the pull request when the build fails, open the build log (Falcon), find the link to **Sheshesh**, and accept the baseline there.

---

**Q: How can I debug a component that has been rendered into a snapshot?**

**A:** Start `Storybook` testing environment to see your snapshots live:

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
pnpm start:[TARGET_THEME]
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

or

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
npm run start:[TARGET_THEME]
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

Once the local server is running, open it in your browser and navigate to the component you want to debug. Then, in code, find the snapshot-generating test and work with it to find a solution to your problem.

### Sled E2E tests

1. These tests use real browser APIs (for example, position calculations, hovering, styling).
2. Tests run with sled using `Puppeteer` in Chrome.
3. Tests are defined with a `.sled.spec.ts` extension (for example, `ComponentName.sled.spec.ts`), located in `packages/wix-design-system/sled/tests/__storybook-e2e-tests__/`.

---

## Running Tests

### Unit Tests

1. Single run:
<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
pnpm test:unit
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

or

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
npm run test:unit
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

2. Watch mode:
<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
pnpm test:unit --watch
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

or

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
npm run test:unit -- --watch
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

#### Debugging

In watch mode, use `jest`'s interactive mode by pressing `p` and typing the name of the test.

### Visual Tests

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
pnpm test:visuals:[TARGET_THEME]
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

or

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
npm run test:visuals:[TARGET_THEME]
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

### Sled E2E tests

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
pnpm test:e2e
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

or

<!-- grid layout -->

<!-- grid layout item -->

<!-- grid layout content -->

```bash
npm run test:e2e
```

<!-- end grid layout -->

<!-- end grid layout item -->

<!-- end grid layout content -->

<br>
<br>
<br>
<br>
<br>