New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

test-data-bot

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

test-data-bot - npm Package Compare versions

Comparing version

to
0.5.0

6

CHANGELOG.md

@@ -0,1 +1,7 @@

### 0.5.0 [03 August]
- add `arrayOf`
- add `bool`
- Enable `sequence` to take other builders.
### 0.4.0 [19 July]

@@ -2,0 +8,0 @@

2

package.json
{
"name": "test-data-bot",
"version": "0.4.0",
"version": "0.5.0",
"description": "Generate test data for your tests easily.",

@@ -5,0 +5,0 @@ "engines": {

@@ -37,2 +37,4 @@ # test-data-bot

- A call to `oneOf`. This takes any number of primitive values, and picks one at random.
- A call to `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.
- A call to `bool`. This is a shortcut for `oneOf(true, false)` and will pick one of them at random.

@@ -39,0 +41,0 @@ ## Mapping

@@ -8,2 +8,9 @@ class Field {

recurseOntoBuilder(builderType, fieldName, nextBuilder) {
return new Field(
`${builderType}(${fieldName})`,
nextBuilder
).generateValue()
}
generateValue() {

@@ -14,3 +21,12 @@ if (this.value && this.value._testDataBotType) {

} else if (this.value._testDataBotType === 'sequenceData') {
return this.value.sequenceFn(this.sequenceStart++)
const sequenceResponse = this.value.sequenceFn(this.sequenceStart++)
if (sequenceResponse.hasOwnProperty('_testDataBotType')) {
return this.recurseOntoBuilder(
'sequence',
this.name,
sequenceResponse
)
} else {
return sequenceResponse
}
} else if (this.value._testDataBotType === 'perBuild') {

@@ -23,2 +39,10 @@ return this.value.buildFn()

return this.value.oneOfOptions[randomIndex]
} else if (this.value._testDataBotType === 'arrayOf') {
return Array.from({ length: this.value.count }).map(_ => {
return this.recurseOntoBuilder(
'arrayOf',
this.name,
this.value.builder
)
})
} else {

@@ -93,2 +117,19 @@ throw new Error(

module.exports = { build, fake, sequence, perBuild, incrementingId, oneOf }
const arrayOf = (builder, count = 1) => ({
_testDataBotType: 'arrayOf',
builder,
count,
})
const bool = () => oneOf(true, false)
module.exports = {
build,
arrayOf,
fake,
sequence,
perBuild,
incrementingId,
oneOf,
bool,
}

@@ -8,4 +8,24 @@ const {

oneOf,
bool,
arrayOf,
} = require('./index')
expect.extend({
toBeTrueOrFalse(received) {
const pass = received === true || received === false
if (pass) {
return {
pass: true,
message: () => `expected ${received} not to be true or false`,
}
} else {
return {
pass: false,
message: () => `expected ${received} to be true or false`,
}
}
},
})
describe('generating fake items', () => {

@@ -107,2 +127,49 @@ it('generates an object that can build items', () => {

})
it('allows a sequence to take a builder', () => {
const userBuilder = build('User').fields({
name: fake(f => 'Jack'),
email: sequence(x => fake(f => f.name.findName() + x)),
})
const user = userBuilder()
expect(user.email).toMatch(/(\w+)1/)
})
it('supports arrayOf with another builder', () => {
const commentBuilder = build('Comment').fields({
text: fake(f => f.lorem.sentence()),
})
const userBuilder = build('User').fields({
friends: arrayOf(fake(f => f.name.findName()), 2),
comments: arrayOf(commentBuilder(), 3),
})
const user = userBuilder()
expect(user.friends).toEqual(
expect.arrayContaining([expect.any(String), expect.any(String)])
)
expect(user.comments).toEqual(
expect.arrayContaining(Array(3).fill({ text: expect.any(String) }))
)
})
it('lets arrayOf take primitives', () => {
const userBuilder = build('User').fields({
comments: arrayOf(1, 3),
})
const user = userBuilder()
expect(user.comments).toEqual([1, 1, 1])
})
it('defines boolean to return either true or false', () => {
const userBuilder = build('User').fields({
isAdmin: bool(),
})
const user = userBuilder()
expect(user.isAdmin).toBeTrueOrFalse()
})
})