This project demonstrates how to integrate Wirespec, Storybook, MSW (Mock Service Worker), and Pact to create a robust API mocking and contract testing solution for frontend applications.
This example showcases a simple Todo application that:
- Uses Wirespec to define API contracts
- Generates TypeScript types and API clients from Wirespec definitions
- Uses MSW to mock API responses in Storybook from Wirespec definitions
- Captures API interactions with Pact for contract testing
The integration provides a seamless workflow for developing, testing, and documenting frontend components that interact with APIs.
- API Definition with Wirespec: Define your API contracts in a simple, readable format
- Type-Safe API Clients: Generate TypeScript types and API clients from Wirespec definitions
- Interactive Component Testing: Test components with mocked API responses in Storybook
- Contract Testing: Capture API interactions for contract testing with Pact
- Comprehensive Testing: Test happy paths and error scenarios with MSW
# Clone the repository
git clone https://github.com/yourusername/wirespec-example-storybook-msw-pact.git
cd wirespec-example-storybook-msw-pact
# Install dependencies
npm installnpm run generateThis command compiles the Wirespec definitions in the wirespec directory and generates TypeScript types and API clients in the src/gen directory.
npm run storybookThis command starts Storybook, where you can interact with the Todo application components and see how they behave with different API responses.
npm testThis command runs the tests using Vitest.
- API Definition: The API is defined in
wirespec/todo.wsusing Wirespec syntax - Code Generation: TypeScript types and API clients are generated from the Wirespec definition
- Component Implementation: The Todo application is implemented as a web component using Lit Element
- Storybook Integration: Storybook stories use MSW to mock API responses
- Pact Integration: API interactions are captured and recorded as Pact contracts
// In a Storybook story
msw.use(
wirespecMsw(GetTodos.api, async () => GetTodos.response200({body: todos, total: 10}))
)This code mocks the GET /todos API to return a successful response with a list of todos.
The wirespecPact function in wirespec.ts automatically captures API interactions and records them as Pact contracts:
export const wirespecPact = (req: RawRequest, res: RawResponse) => {
const interaction: Interaction = {
// ... interaction details
};
// Add the interaction to the Pact contract
pact.interactions.push(interaction);
}This project includes a custom Vitest reporter that automatically generates Pact contract files from API interactions captured during tests:
// Configured in vite.config.ts
test: {
// other configuration...
reporters: ['default', 'junit', new PactReporter()]
}The Pact reporter:
- Collects MSW request/response pairs from test files
- Transforms them into Pact interactions
- Creates a Pact contract file with consumer and provider information
- Writes the Pact contract to the "pacts" directory
When you run tests with npm test, the reporter automatically generates Pact files that can be used for contract testing with your API providers.
- Wirespec: API specification and code generation
- Storybook: Component development and testing
- MSW (Mock Service Worker): API mocking
- Pact: Contract testing
- Lit Element: Web component framework
- TypeScript: Type-safe JavaScript
- Vitest: Testing framework