Registry / testing / cypress-test-data-generator

cypress-test-data-generator

JSON →
library2.0.2jsnpmunverified

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.

npm install cypress-test-data-generator
INSTALL
IMPORT
SIG · CYPRESS-TEST-DATA-
C
cypress-test-data-generator
testingjavascriptv2.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

dataGenerator
const dataGenerator = require('cypress-test-data-generator');
import dataGenerator from 'cypress-test-data-generator';
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.
generateUser
cy.task('generateUser')
import { generateUser } from 'cypress-test-data-generator';
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.
generateProduct
cy.task('generateProduct', { options })
cy.generateProduct({ options })
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()`.

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.

/* cypress.config.js */ const { defineConfig } = require('cypress'); const dataGenerator = require('cypress-test-data-generator'); module.exports = defineConfig({ e2e: { setupNodeEvents(on, config) { // Register the data generator tasks on('task', dataGenerator(on, config)); // Important: always return the config object from setupNodeEvents return config; }, baseUrl: 'http://localhost:3000', // Example base URL for your application }, }); /* cypress/e2e/registration.cy.js */ describe('User Registration Flow', () => { it('should allow a new user to register with generated data', () => { // Generate a user with a specific locale and seed for reproducibility cy.task('generateUser', { locale: 'en', seed: 12345 }).then((user) => { cy.visit('/register'); cy.get('#firstName').type(user.firstName); cy.get('#lastName').type(user.lastName); cy.get('#email').type(user.email); cy.get('#password').type('SecureP@ssw0rd!'); cy.get('#confirmPassword').type('SecureP@ssw0rd!'); cy.get('button[type="submit"]').click(); // Assertions after registration cy.url().should('include', '/dashboard'); cy.contains(`Welcome, ${user.firstName}`).should('be.visible'); cy.log(`Registered user: ${user.email}`); }); }); it('should generate a product and a related review for testing', () => { cy.task('generateProduct', { category: 'Electronics' }).then((product) => { cy.log(`Generated Product: ${JSON.stringify(product)}`); cy.task('generateReview', { productId: product.id, rating: 5, comment: 'Excellent product!', locale: 'es' }).then((review) => { cy.log(`Generated Review: ${JSON.stringify(review)}`); // Example scenario: Navigate to product page and verify review presence cy.visit(`/products/${product.id}`); cy.contains(product.name).should('be.visible'); cy.contains(review.comment).should('be.visible'); cy.get('span.rating').should('have.attr', 'data-rating', '5'); }); }); }); });
Debug
Known issues
gotchaThe `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.
fix
Ensure `return config;` is the last statement within your `setupNodeEvents` function: `setupNodeEvents(on, config) { on('task', dataGenerator(on, config)); return config; }`
affects: >=1.0.0
gotchaData 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`.
fix
Always invoke data generators through `cy.task('generatorName', options)` within your Cypress tests.
affects: >=1.0.0
gotchaOmitting 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.
fix
For reproducible test scenarios where data consistency is crucial, always specify a `seed` value in your generator options: `cy.task('generateUser', { seed: 12345 })`.
affects: >=1.0.0
gotchaWhile `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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
CypressError: `cy.task('generateUser')` failed with the following error: The task 'generateUser' was not registered.
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`.
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`.
TypeError: dataGenerator is not a function
This usually occurs in `cypress.config.js` if `dataGenerator` is imported incorrectly or called without the `on` and `config` arguments.
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));`.
ReferenceError: faker is not defined
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.
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.
Upgrade
Version history
2.0.2latest on npm
Audit
Dependencies
cypressrequiredRequired as a peer dependency for Cypress `cy.task` integration and plugin setup within `cypress.config.js`.
fakerrequiredUnderpins all data generation logic, providing the core functionality for creating realistic data.
Agent activity
12 hits · last 30 days
node
12
Resources
cypress-test-data-generator — npm install cypress-test-data-generator · libregistry