Skip to content

Recipes (Cypress)

Example scenarios are implemented in Cypress in apps/cypress-example. You use native describe / it and the story object: call story.init() at the start of each test (no task argument; same as Jest), then story.given, story.when, story.then. Story meta is sent from the browser to Node via cy.task for the reporter.

Vitest/JestCypress
Initstory.init(task) / story.init()story.init()
Reporter configIn test config fileVia CLI flag or Module API
Meta transportDirect (in-process)Browser → Node via cy.task
Setupconfig reporterPlugin + support file
import { story } from 'executable-stories-cypress';
describe('Calculator', () => {
it('adds two numbers', () => {
story.init();
story.given('two numbers 5 and 3');
const a = 5, b = 3;
story.when('I add them together');
const result = a + b;
story.then('the result is 8');
expect(result).toBe(8);
});
});
### Adds two numbers
- **Given** two numbers 5 and 3
- **When** I add them together
- **Then** the result is 8
import { story } from 'executable-stories-cypress';
describe('Login', () => {
it('User logs in successfully', () => {
story.init({ tags: ['smoke', 'auth'] });
story.given('the user account exists');
story.given('the user is on the login page');
story.note('Account must be active for at least 24 hours.');
story.when('the user submits valid credentials');
story.json({
label: 'Credentials',
value: { email: '[email protected]', password: '***' },
});
story.then('the user should see the dashboard');
});
});

Example: Login blocked for suspended user (with But)

Section titled “Example: Login blocked for suspended user (with But)”
import { story } from 'executable-stories-cypress';
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');
});
});

Cypress reports are generated via the Mocha reporter flag or the Module API:

Terminal window
# Via CLI flag
cypress run --reporter executable-stories-cypress/reporter --reporter-options outputDir=docs,outputName=user-stories
# Via Module API
import { buildRawRunFromCypressResult, generateReportsFromRawRun } from 'executable-stories-cypress/reporter';
const result = await cypress.run();
const rawRun = buildRawRunFromCypressResult(result, { projectRoot: process.cwd() });
await generateReportsFromRawRun(rawRun, { formats: ['markdown', 'html'], outputDir: 'docs' });

The cypress-example app includes specs for calculator, step aliases, story options, wrapped steps, and Gherkin-style patterns. Generated docs: apps/cypress-example/docs/user-stories.md.

For a full list of patterns, see the Vitest recipes — the same scenarios can be adapted to Cypress by replacing story.init(task) with story.init() and using describe/it from Mocha.

Cypress story & doc API — steps and doc usage.