# @xstate/test

> XState test utilities

Latest version **0.5.1** (published 2022-01-27) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @xstate/test
pnpm add @xstate/test
yarn add @xstate/test
bun add @xstate/test
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2022-01-27 |
| First published | 2019-08-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 70.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 30103 |
| Author | David Khourshid |
| Maintainers | xstate-release-bot, andarist, davidkpiano |
| Keywords | state, machine, statechart, scxml, state machine, model based testing, mbt, model, testing |

## Links

- npm: https://www.npmjs.com/package/@xstate/test
- Repository: https://github.com/statelyai/xstate
- Homepage: https://github.com/statelyai/xstate/tree/main/packages/xstate-test#readme
- Issues: https://github.com/statelyai/xstate/issues
- npm.io page: https://npm.io/package/@xstate/test

## Dependencies (2)

- [chalk](https://npm.io/package/chalk.md) ^2.4.2
- [@xstate/graph](https://npm.io/package/@xstate/graph.md) ^1.4.1

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 0.5.1 (latest) — 2022-01-27
- 1.0.0-beta.5 (beta) — 2024-01-11
- 1.0.0-alpha.1 (alpha) — 2023-03-07
- 1.0.0-beta.4 — 2023-09-24
- 1.0.0-beta.3 — 2023-09-06
- 1.0.0-beta.2 — 2023-09-05
- 1.0.0-alpha.0 — 2022-05-30
- 0.5.0 — 2021-10-25
- 0.4.2 — 2021-02-13
- 0.4.1 — 2020-08-17
- 0.4.0 — 2020-04-15
- 0.3.0 — 2020-01-07
- 0.2.1 — 2019-12-28
- 0.2.0 — 2019-12-16
- 0.1.0 — 2019-08-30
- … 1 more at https://npm.io/package/@xstate/test/versions

## README

# @xstate/test

This package contains utilities for facilitating [model-based testing](https://en.wikipedia.org/wiki/Model-based_testing) for any software.

- [Read the full documentation in the XState docs](https://xstate.js.org/docs/packages/xstate-test/).
- [Read our contribution guidelines](https://github.com/statelyai/xstate/blob/main/CONTRIBUTING.md).

## Talk

[Write Fewer Tests! From Automation to Autogeneration](https://slides.com/davidkhourshid/mbt) at React Rally 2019 ([🎥 Video](https://www.youtube.com/watch?v=tpNmPKjPSFQ))

## Quick Start

1. Install `xstate` and `@xstate/test`:

```bash
npm install xstate @xstate/test
```

2. Create the machine that will be used to model the system under test (SUT):

```js
import { createMachine } from 'xstate';

const toggleMachine = createMachine({
  id: 'toggle',
  initial: 'inactive',
  states: {
    inactive: {
      on: {
        TOGGLE: 'active'
      }
    },
    active: {
      on: {
        TOGGLE: 'inactive'
      }
    }
  }
});
```

3. Add assertions for each state in the machine (in this example, using [Puppeteer](https://github.com/GoogleChrome/puppeteer)):

```js
// ...

const toggleMachine = createMachine({
  id: 'toggle',
  initial: 'inactive',
  states: {
    inactive: {
      on: {
        /* ... */
      },
      meta: {
        test: async (page) => {
          await page.waitFor('input:checked');
        }
      }
    },
    active: {
      on: {
        /* ... */
      },
      meta: {
        test: async (page) => {
          await page.waitFor('input:not(:checked)');
        }
      }
    }
  }
});
```

4. Create the model:

```js
import { createMachine } from 'xstate';
import { createModel } from '@xstate/test';

const toggleMachine = createMachine(/* ... */);

const toggleModel = createModel(toggleMachine).withEvents({
  TOGGLE: {
    exec: async (page) => {
      await page.click('input');
    }
  }
});
```

5. Create test plans and run the tests with coverage:

```js
// ...

describe('toggle', () => {
  const testPlans = toggleModel.getShortestPathPlans();

  testPlans.forEach((plan) => {
    describe(plan.description, () => {
      plan.paths.forEach((path) => {
        it(path.description, async () => {
          // do any setup, then...

          await path.test(page);
        });
      });
    });
  });

  it('should have full coverage', () => {
    return toggleModel.testCoverage();
  });
});
```

---
_Source: https://npm.io/package/@xstate/test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
