Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
test-data-bot
Advanced tools
An easy way to generate test data for your JavaScript unit tests. Completely agnostic of test runner, framework or environment.
npm install --save-dev test-data-bot
const { build, fake, sequence } = require('test-data-bot')
const userBuilder = build('User').fields({
name: fake(f => f.name.findName()),
email: sequence(x => `jack${x}@test.com`),
age: 26,
})
const user = userBuilder()
console.log(user)
// => { name: 'Bob Fleming', email: 'jack1@test.com', age: 26 }
Firstly, use build
to create a new builder. The name passed is just for debugging/documentation purposes and can be whatever you'd like.
Once you've created a builder, use fields
to pass in the names and values of fields.
Field values can be one of:
sequence
, which takes a function which is passed a number. This is an easy way to ensure a value is unique everytime, but still know what it will be. A sequence is per field, and the number starts at 1.fake
. This takes a function that will be called with an instance of faker.js, and you can use any of the faker API methods to return data.perBuild
. This takes a function that will be called each time an instance is created. This is useful if you want each instance to have the same actual value (say, an object), but one that isn't referentially the same.incrementingId
. This will produce a number that starts at 1
and increments each time it is used. Good to model IDs from a database.oneOf
. This takes any number of primitive values, and picks one at random.arrayOf
. This takes any value (including another builder) and generates an array of them. It also takes the array length
as the second argument: arrayOf('foo', 2)
will generate ['foo', 'foo']
. arrayOf(fake(f => f.name.findName()), 5)
will generate an array of 5 random names.bool
. This is a shortcut for oneOf(true, false)
and will pick one of them at random.numberBetween
. This takes two arguments as min/max, and generates a random integer between them. numberBetween(0, 10)
is a shortcut for fake(f => f.random.number({ min: 0, max: 10 })
.The test-data-bot always creates plain JavaScript objects, but you may need to generate instances of a class, for example. In this instance, you can pass a map
to the builder. This will be called with the generated object, and you can then do with that data whatever you'd like:
const userBuilder = build('User')
.fields({
name: fake(f => f.name.findName()),
email: sequence(x => `jack${x}@test.com`),
})
.map(user => ({
name: user.name.toUpperCase(),
email: user.email.toUpperCase(),
}))
const user = userBuilder()
console.log(user)
// => { name: 'BOB FLEMING', email: 'JACK1@TEST.COM' }
Sometimes you might want to ensure a certain value, rather than use the generator. In this case you can pass it in when you call the builder:
const userBuilder = build('User').fields({
name: fake(f => f.name.findName()),
email: sequence(x => `jack${x}@test.com`),
})
const user = userBuilder({ name: 'JACK' })
console.log(user)
// => { name: 'JACK', email: 'jack1@test.com' }
In this case, name
will always be 'JACK'
, and the generator given will not be used.
FAQs
Generate test data for your tests easily.
The npm package test-data-bot receives a total of 3,758 weekly downloads. As such, test-data-bot popularity was classified as popular.
We found that test-data-bot demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.