Test Data Bot
test-data-bot is a JavaScript library designed for easily generating dynamic test data for unit and integration tests. It operates agnostic of specific test runners or frameworks, providing a fluent API for defining data builders. As of version 0.8.0, it offers features like static values, unique sequences, data generation using `faker.js` (internally), per-build instance generation, incrementing IDs, random selection from a list, and array generation. The package emphasizes simplicity and flexibility, allowing for easy overrides and custom mapping functions to transform generated plain JavaScript objects into class instances or specific formats. Its release cadence appears to follow semantic versioning, with `0.x` indicating pre-1.0 development. Key differentiators include its simple, builder-pattern API and deep integration with `faker.js` without exposing it as a direct peer dependency.
Common errors
-
TypeError: build is not a function
cause Attempting to use `build` without correct destructuring for CommonJS `require` or using `require` in an ESM context.fixEnsure correct import syntax: `const { build, fake } = require('test-data-bot');` for CommonJS or `import { build, fake } from 'test-data-bot';` for ESM. -
ReferenceError: faker is not defined
cause Trying to directly access `faker` methods outside of the `fake()` helper function, or without passing the `f` argument.fixAll `faker.js` methods must be accessed via the `f` argument provided to the callback inside `fake()`: `fake(f => f.name.firstName())`. -
TypeError: Cannot read properties of undefined (reading 'someMethod')
cause This error often occurs within the `fake()` callback if the `f` (faker) object is not passed or if an invalid `faker.js` method is called.fixVerify that your `fake()` callback receives the `faker` instance as an argument (`fake(f => ...)`) and that the `faker.js` method you are calling (e.g., `f.name.findName()`) exists in the `faker.js` API.
Warnings
- gotcha The `sequence` helper generates numbers unique to each field definition, meaning `sequence(x => x)` in two different fields will both start their count from 1. Conversely, `incrementingId` provides a globally unique, auto-incrementing number across all uses.
- gotcha While `test-data-bot` uses `faker.js` internally, the specific version of `faker.js` is managed by `test-data-bot` and is not exposed or directly controllable by the end-user. Updates to the internal `faker.js` dependency could lead to subtle changes in generated data patterns between `test-data-bot` patch versions, even without a major `test-data-bot` version bump.
- gotcha When using `arrayOf(builder, count)` where `builder` is another test-data-bot builder, this will create an array containing `count` references to the *builder function itself*, not an array of generated objects. To get an array of generated objects, you must call the builder.
- gotcha Overriding values by passing an object to the builder function (`userBuilder({ name: 'New Name' })`) only affects top-level fields. For deeply nested objects that are defined within `fields`, a simple override will replace the entire nested object rather than merging properties.
Install
-
npm install test-data-bot -
yarn add test-data-bot -
pnpm add test-data-bot
Imports
- build
const { build } = require('test-data-bot')import { build } from 'test-data-bot' - fake
const { fake } = require('test-data-bot')import { fake } from 'test-data-bot' - sequence
const { sequence } = require('test-data-bot')import { sequence } from 'test-data-bot' - incrementingId
const { incrementingId } = require('test-data-bot')import { incrementingId } from 'test-data-bot'
Quickstart
import { build, fake, sequence } from 'test-data-bot';
const userBuilder = build('User').fields({
id: sequence(x => x),
name: fake(f => f.name.findName()),
email: sequence(x => `user${x}@example.com`),
age: 26,
isActive: true,
});
const newUser1 = userBuilder();
const newUser2 = userBuilder({ name: 'Jane Doe', age: 30 });
console.log(newUser1);
// Example output: { id: 1, name: 'John Smith', email: 'user1@example.com', age: 26, isActive: true }
console.log(newUser2);
// Example output: { id: 2, name: 'Jane Doe', email: 'user2@example.com', age: 30, isActive: true }
const adminBuilder = build('Admin').fields({
...userBuilder.fields,
role: 'admin'
});
const newAdmin = adminBuilder();
console.log(newAdmin);
// Example output: { id: 3, name: 'Another Name', email: 'user3@example.com', age: 26, isActive: true, role: 'admin' }