@xstate/test

XState test utilities

Superseded by @xstate/test@2 (model-based and property-based testing for XState v6). Path utilities live in xstate/graph.
Last updated 4 years ago by xstate-release-bot .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @xstate/test 
SYNC missed versions from official npm registry.

@xstate/test

Built self-documenting, easy-to-maintain tests which integrate with any test framework using Statecharts.

Create a visual model, tell it how to interact with your app, and execute the model to test your app automatically.

Read our intro to model-based testing in our beta docs

Talk

Write Fewer Tests! From Automation to Autogeneration at React Rally 2019 (🎥 Video)

Quick Start

  1. Install xstate and @xstate/test:
yarn add xstate @xstate/test
  1. Create your test machine using createTestMachine:
import { createTestMachine } from '@xstate/test';

const machine = createTestMachine({
  initial: 'onHomePage',
  states: {
    onHomePage: {
      on: {
        SEARCH_FOR_MODEL_BASED_TESTING: 'searchResultsVisible'
      }
    },
    searchResultsVisible: {
      on: {
        CLICK_MODEL_BASED_TESTING_RESULT: 'onModelBasedTestingPage',
        PRESS_ESCAPE: 'searchBoxClosed'
      }
    },
    searchBoxClosed: {},
    onModelBasedTestingPage: {}
  }
});
  1. Turn the machine into a test model using createTestModel
import { createTestModel } from '@xstate/test';

const model = createTestModel(machine);
  1. Run the model in a test, passing it the instructions on what to do on different states and events. For example, using Cypress.
describe('Toggle component', () => {
  /**
   * For each path generated by XState,
   * run a new test via `it`
   */
  model.getPaths().forEach((path) => {
    it(path.description, () => {
      // Run any setup before each test here

      /**
       * In environments, like Cypress,
       * that don’t support async, run plan.testSync();
       *
       * Otherwise, you can run await plan.test();
       */
      path.testSync({
        states: {
          onHomePage: () => {
            cy.visit('/');
          },
          searchResultsVisible: () => {
            cy.findByText('Model-based testing').should('be.visible');
          },
          searchBoxClosed: () => {
            cy.findByText('Model-based testing').should('not.be.visible');
          },
          onModelBasedTestingPage: () => {
            cy.url().should('include', '/model-based-testing/intro');
          }
        },
        events: {
          CLICK_MODEL_BASED_TESTING_RESULT: () => {
            cy.findByText('Model-based testing').click();
          },
          SEARCH_FOR_MODEL_BASED_TESTING: () => {
            cy.findByPlaceholderText('Search').type('Model-based testing');
          }
        }
      });

      // Run any cleanup after each test here
    });
  });
});

Current Tags

  • 2.0.0-alpha.4                                ...           alpha (a month ago)
  • 2.0.0-beta.0                                ...           beta (22 days ago)
  • 0.5.1                                ...           latest (5 years ago)

21 Versions

  • 2.0.0-beta.0                                ...           22 days ago
  • 2.0.0-alpha.4                                ...           a month ago
  • 2.0.0-alpha.3                                ...           a month ago
  • 2.0.0-alpha.2                                ...           2 months ago
  • 2.0.0-alpha.1                                ...           2 months ago
  • 1.0.0-beta.5 [deprecated]           ...           3 years ago
  • 1.0.0-beta.4 [deprecated]           ...           3 years ago
  • 1.0.0-beta.3 [deprecated]           ...           3 years ago
  • 1.0.0-beta.2 [deprecated]           ...           3 years ago
  • 1.0.0-alpha.1 [deprecated]           ...           4 years ago
  • 1.0.0-alpha.0 [deprecated]           ...           4 years ago
  • 0.5.1 [deprecated]           ...           5 years ago
  • 0.5.0 [deprecated]           ...           5 years ago
  • 0.4.2 [deprecated]           ...           6 years ago
  • 0.4.1 [deprecated]           ...           6 years ago
  • 0.4.0 [deprecated]           ...           7 years ago
  • 0.3.0 [deprecated]           ...           7 years ago
  • 0.2.1 [deprecated]           ...           7 years ago
  • 0.2.0 [deprecated]           ...           7 years ago
  • 0.1.0 [deprecated]           ...           7 years ago
  • 0.0.1 [deprecated]           ...           7 years ago
Downloads
Total 58
Today 0
This Week 15
This Month 57
Last Day 5
Last Week 19
Last Month 1
Dependencies (1)
Dev Dependencies (6)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE