Back to Storybook

Use Storybook Expect

code/lib/eslint-plugin/docs/rules/use-storybook-expect.md

10.3.61.5 KB
Original Source

Use expect from '@storybook/jest' (use-storybook-expect)

<!-- RULE-CATEGORIES:START -->

Included in these configurations: <ul><li>addon-interactions</li><li>flat/addon-interactions</li><li>recommended</li><li>flat/recommended</li></ul>

<!-- RULE-CATEGORIES:END -->

Rule Details

Storybook provides a browser-compatible version of expect via the storybook/test core package (formerly available in the legacy @storybook/jest library). When writing interactions and asserting values, you should always use expect from the storybook/test library.

Examples of incorrect code for this rule:

js
Default.play = async () => {
  // Using global expect from Jest. Will break on the browser
  await expect(123).toEqual(123);
};

Examples of correct code for this rule:

js
// Correct import.
import { expect } from "storybook/test";
// or this, which is now considered legacy
import { expect } from "@storybook/jest";

Default.play = async () => {
  // Using imported expect from storybook package
  await expect(123).toEqual(123);
};

When Not To Use It

This rule should not be applied in test files. Please ensure that you define the Storybook rules only for story files. You can see more details here.