Skip to content

Recipes (Jest)

Every Vitest recipe has a Jest equivalent in apps/jest-example. The generated output is identical across frameworks — only the test code differs.

You use native describe / it and the story object: call story.init() at the start of each test (no task argument; Jest gets the test name from expect.getState()), then story.given, story.when, story.then (and story.and, story.but). Jest also exports top-level given, when, then, and, but helpers.

VitestJest
Initstory.init(task)story.init()
Importimport { story } from 'executable-stories-vitest'import { story } from 'executable-stories-jest'
SetupnonesetupFilesAfterEnv: ['executable-stories-jest/setup']
Top-level helpersnot exported (no then)given, when, then, and, but exported
### User logs in successfully
- **Given** the user account exists
- **And** the user is on the login page
- **And** the account is active
- **When** the user submits valid credentials
- **Then** the user should see the dashboard
import { expect } from '@jest/globals';
import { story } from 'executable-stories-jest';
describe('Login', () => {
it('User logs in successfully', () => {
story.init();
story.given('the user account exists');
story.given('the user is on the login page');
story.given('the account is active');
story.when('the user submits valid credentials');
story.then('the user should see the dashboard');
expect(true).toBe(true); // or real assertions
});
});

Example: Login blocked for suspended user (with But)

Section titled “Example: Login blocked for suspended user (with But)”
import { story } from 'executable-stories-jest';
describe('Login', () => {
it('Login blocked for suspended user', () => {
story.init();
story.given('the user account exists');
story.given('the account is suspended');
story.when('the user submits valid credentials');
story.then('the user should see an error message');
story.but('the user should not be logged in');
});
});

Example: Bulk user creation (with doc.table)

Section titled “Example: Bulk user creation (with doc.table)”
import { story } from 'executable-stories-jest';
describe('Users', () => {
it('Bulk user creation', () => {
story.init();
story.given('the following users exist');
story.table({
label: 'Users',
columns: ['email', 'role', 'status'],
rows: [
['[email protected]', 'admin', 'active'],
['[email protected]', 'user', 'active'],
['[email protected]', 'user', 'locked'],
],
});
story.when('the admin opens the user list');
story.then('the user list should include');
story.table({
label: 'Expected',
columns: ['email', 'role', 'status'],
rows: [
['[email protected]', 'admin', 'active'],
['[email protected]', 'user', 'active'],
['[email protected]', 'user', 'locked'],
],
});
});
});

The same 32 scenarios as Vitest recipes are in apps/jest-example/src/replicate.story.test.ts. Generated docs: apps/jest-example/src/replicate.story.docs.md.

ScenarioPatternSee Vitest recipe
User logs in successfullyMultiple Given, single When, single ThenLink
User updates profile detailsSingle Given, multiple When, single ThenLink
Checkout calculates totalsSingle Given, single When, multiple ThenLink
Password reset flowMultiple Given/When/ThenLink
Login blocked for suspended userUse of ButLink
Login works (tags)Story tagsLink
Login errors (outline)Scenario outline with loopLink
Many login attempts (outline)Scenario outline, multiple outcomesLink
Bulk user creationdoc.tableLink
Create users from tableScenario outline with doc.tableLink
Calculate shipping optionsDataTable, multiple ThenLink
Shipping eligibilityScenario outline by countryLink
Tax calculation by regionScenario outline with multiple rowsLink
API accepts JSON payloaddoc.json (DocString)Link
Post JSON payload (outline)Scenario outline with doc.jsonLink
Import XML invoicedoc.code (XML)Link
Import users + welcome emaildoc.table + doc.codeLink
Render markdowndoc.code (markdown)Link
Change email addressShared backgroundLink
Change passwordShared background, different When/ThenLink
Eligible customer gets discountRule block, positive pathLink
Ineligible customer no discountRule block, negative pathLink
Two step checkoutMultiple When groupsLink
Payment declinedNegative path with ButLink
Guest checkout alloweddoc.note for ButLink
Logout clears sessionRepeated Then stepsLink
Document status changesExplicit state transitionLink
Update preferencesDataTable as key-value pairsLink
Configure feature flagsComplex DataTableLink
Create orderBackground and tagsLink
Search results show highlightsAnd after ThenLink
Report shows fields in orderAnd in middle of ThenLink

Jest story & doc API — steps and doc usage.