Use Case

Seed Cypress E2E Tests with MockHero

cypress.config.ts

import { defineConfig } from "cypress";

export default defineConfig({
  e2e: {
    async setupNodeEvents(on, config) {
      on("task", {
        async seed() {
          const data = await fetch("https://api.mockhero.dev/api/v1/generate", {
            method: "POST",
            headers: {
              "x-api-key": process.env.MOCKHERO_API_KEY!,
              "Content-Type": "application/json"
            },
            body: JSON.stringify({
              seed: 1,
              tables: [{
                name: "users", count: 10,
                fields: [
                  { name: "id", type: "uuid" },
                  { name: "email", type: "email" }
                ]
              }]
            })
          }).then(r => r.json());
          // insert into your DB here
          return null;
        }
      });
    }
  }
});
// cypress/e2e/home.cy.ts
before(() => { cy.task("seed"); });

it("renders 10 users", () => {
  cy.visit("/users");
  cy.get("[data-testid=user-row]").should("have.length", 10);
});

Get Started

Free API key.

M

MockHero Team

Guides and tutorials for generating realistic test data with the MockHero API.

Start generating test data for free

1,000 rows/month on the free tier. No credit card required.

Get Your API Key

Related Articles