{"id":13049,"library":"cypress-test-data-generator","title":"Cypress Test Data Generator","description":"Cypress Test Data Generator is a Cypress plugin (currently at version 2.0.2) designed to simplify the creation of realistic, reproducible test data for end-to-end tests. It leverages the popular Faker.js library to provide over 40 distinct data generators for common entities like users, products, orders, and social profiles, along with extensive support for internationalization across 50+ locales. The plugin integrates seamlessly with Cypress via `cy.task`, allowing data generation directly within test specs or before hooks, executed in the Node.js environment. Its key differentiators include seed support for consistent data across test runs, fully typed APIs for better developer experience, and a zero-configuration approach with sensible defaults, significantly reducing the boilerplate associated with manual test data creation. The project appears actively maintained with a stable release cadence. It aims to eliminate the tedious and error-prone process of manually crafting test data, providing a robust and flexible alternative for modern Cypress workflows.","status":"active","version":"2.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/khawjaahmad/cypress-test-data-generator","tags":["javascript","cypress","testing","data-generator","faker"],"install":[{"cmd":"npm install cypress-test-data-generator","lang":"bash","label":"npm"},{"cmd":"yarn add cypress-test-data-generator","lang":"bash","label":"yarn"},{"cmd":"pnpm add cypress-test-data-generator","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a peer dependency for Cypress `cy.task` integration and plugin setup within `cypress.config.js`.","package":"cypress","optional":false},{"reason":"Underpins all data generation logic, providing the core functionality for creating realistic data.","package":"faker","optional":false}],"imports":[{"note":"This is the primary CommonJS import for setting up the plugin within `cypress.config.js`. While Cypress supports ESM configs, the plugin's documented setup uses `require`. If using an ESM config, ensure proper CJS interoperability or use an ESM-compatible import if explicitly supported by the plugin.","wrong":"import dataGenerator from 'cypress-test-data-generator';","symbol":"dataGenerator","correct":"const dataGenerator = require('cypress-test-data-generator');"},{"note":"Data generators (like `generateUser`, `generateProduct`, etc.) are exposed as Cypress tasks, meaning they run in the Node.js environment. They are not directly importable or callable within client-side test files.","wrong":"import { generateUser } from 'cypress-test-data-generator';","symbol":"generateUser","correct":"cy.task('generateUser')"},{"note":"All data generation functions are called as Cypress tasks using `cy.task()`, not as custom Cypress commands (e.g., `cy.generateProduct()`). Options for generation are passed as the second argument to `cy.task()`.","wrong":"cy.generateProduct({ options })","symbol":"generateProduct","correct":"cy.task('generateProduct', { options })"}],"quickstart":{"code":"/* cypress.config.js */\nconst { defineConfig } = require('cypress');\nconst dataGenerator = require('cypress-test-data-generator');\n\nmodule.exports = defineConfig({\n  e2e: {\n    setupNodeEvents(on, config) {\n      // Register the data generator tasks\n      on('task', dataGenerator(on, config));\n      // Important: always return the config object from setupNodeEvents\n      return config;\n    },\n    baseUrl: 'http://localhost:3000', // Example base URL for your application\n  },\n});\n\n/* cypress/e2e/registration.cy.js */\ndescribe('User Registration Flow', () => {\n  it('should allow a new user to register with generated data', () => {\n    // Generate a user with a specific locale and seed for reproducibility\n    cy.task('generateUser', { locale: 'en', seed: 12345 }).then((user) => {\n      cy.visit('/register');\n      cy.get('#firstName').type(user.firstName);\n      cy.get('#lastName').type(user.lastName);\n      cy.get('#email').type(user.email);\n      cy.get('#password').type('SecureP@ssw0rd!');\n      cy.get('#confirmPassword').type('SecureP@ssw0rd!');\n      cy.get('button[type=\"submit\"]').click();\n\n      // Assertions after registration\n      cy.url().should('include', '/dashboard');\n      cy.contains(`Welcome, ${user.firstName}`).should('be.visible');\n      cy.log(`Registered user: ${user.email}`);\n    });\n  });\n\n  it('should generate a product and a related review for testing', () => {\n    cy.task('generateProduct', { category: 'Electronics' }).then((product) => {\n      cy.log(`Generated Product: ${JSON.stringify(product)}`);\n      cy.task('generateReview', { productId: product.id, rating: 5, comment: 'Excellent product!', locale: 'es' }).then((review) => {\n        cy.log(`Generated Review: ${JSON.stringify(review)}`);\n        // Example scenario: Navigate to product page and verify review presence\n        cy.visit(`/products/${product.id}`);\n        cy.contains(product.name).should('be.visible');\n        cy.contains(review.comment).should('be.visible');\n        cy.get('span.rating').should('have.attr', 'data-rating', '5');\n      });\n    });\n  });\n});","lang":"javascript","description":"This quickstart demonstrates how to integrate `cypress-test-data-generator` into `cypress.config.js` and use `cy.task` in a test to generate diverse, reproducible data like users, products, and reviews, including locale and seeding options."},"warnings":[{"fix":"Ensure `return config;` is the last statement within your `setupNodeEvents` function: `setupNodeEvents(on, config) { on('task', dataGenerator(on, config)); return config; }`","message":"The `setupNodeEvents` function in `cypress.config.js` must return the `config` object. Failing to do so can prevent Cypress from loading plugins correctly and lead to `cy.task` not being registered.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always invoke data generators through `cy.task('generatorName', options)` within your Cypress tests.","message":"Data generation via `cy.task` occurs in the Node.js environment, not in the browser context where your Cypress tests run. You cannot directly call Faker.js methods or the plugin's generator functions (e.g., `generateUser()`) from within your test spec files without `cy.task`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For reproducible test scenarios where data consistency is crucial, always specify a `seed` value in your generator options: `cy.task('generateUser', { seed: 12345 })`.","message":"Omitting the `seed` option when generating data will result in different data being produced on each test run. While useful for diversity, it can make debugging flaky tests challenging.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Stick to `const dataGenerator = require('cypress-test-data-generator');` for `cypress.config.js` unless explicitly configured for ESM and the plugin is verified to support it seamlessly.","message":"While `cypress.config.js` supports ESM imports (`import ... from ...`) in modern Cypress versions when using `.mjs` or `type: \"module\"` in `package.json`, this plugin's documentation primarily shows CommonJS `require()`. Attempting a direct ESM `import` without ensuring compatibility might lead to errors.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Verify that `on('task', dataGenerator(on, config));` is present and that `return config;` is the final statement within the `e2e.setupNodeEvents` function in your `cypress.config.js`.","cause":"The `cypress-test-data-generator` plugin was not correctly initialized or the `setupNodeEvents` function did not return the `config` object in your `cypress.config.js`.","error":"CypressError: `cy.task('generateUser')` failed with the following error: The task 'generateUser' was not registered."},{"fix":"Ensure the import is `const dataGenerator = require('cypress-test-data-generator');` and it's called as `dataGenerator(on, config)` when registering tasks: `on('task', dataGenerator(on, config));`.","cause":"This usually occurs in `cypress.config.js` if `dataGenerator` is imported incorrectly or called without the `on` and `config` arguments.","error":"TypeError: dataGenerator is not a function"},{"fix":"The `faker` library is an internal dependency used by the plugin in the Node.js environment. Access generated data exclusively through the `cy.task()` interface, for example, `cy.task('generateUser')`, rather than attempting to call `faker` directly.","cause":"Developers might encounter this if they try to directly access `faker` or its methods within a test spec file, assuming it's globally available or directly exposed by the plugin client-side.","error":"ReferenceError: faker is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}