Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
fixtures-generator
Advanced tools
Creates random fixtures and validate them. Useful when writing tests.
This project was created to solve the most common problems with fixtures that we faced while building tests at VoxFeed.
Use npm or yarn:
npm install -D fixtures-generator
yarn add -D fixtures-generator
Example of schemas object (./schemas.js):
const authorSchema = {
type: 'object',
properties: {
id: { type: 'integer' },
name: { type: 'string' }
}
};
const bookSchema = {
type: 'object',
properties: {
id: { type: 'integer' },
authorId: { type: 'integer' },
title: { type: 'string' }
}
};
const schemas = {
author: authorSchema,
book: bookSchema
};
module.exports = schemas;
After schemas are declared you should build a generator instance. This instance is the one you can use across your tests to generate fixtures.
Example (./generate-fixtures.js)
const FixtureGenerator = require('fixtures-generator');
const schemas = require('./schemas');
const options = {
optionalsProbability: 0.5
};
const generateFixtures = new FixtureGenerator(schemas, options);
module.exports = generateFixtures;
Available options are:
Option | Type | Description |
---|---|---|
optionalsProbability | Float | Number between 0 and 1 that specifies the percentage of non-required propoerties that will be included in randomly generated fixtures. |
Finally, import the fixtures generator instance and create some fixtures.
Example:
const generateFixtures = require('./generate-fixtures');
const params = {
type: 'author',
quantity: 3
};
const authors = generateFixtures(params); // Will generate 3 author fixtures.
Required parameters for generateFixtures
method are:
Parameter | Type | Description |
---|---|---|
type | String | The name of the fixture schema to be used to generate and validate fixture. |
recipe | [Object] | The seed of the fixtures to be generated on the same order. Resulting fixtures will keep all properties sent here. |
quantity | Number | Quantity of fixtures to be generated. If quantity is higher than the size of recipe the exceeding fixtures will be randomly generated; otherwise parameter is omitted. |
It is possible to create random fixtures by just specifying the required quantity:
const fixtures = generateFixtures('invite', 3);
// Result:
[
{
_id: ObjectId('592f0dabfcf681e3d5ceeb6c'),
otherProperties...
},
{
_id: ObjectId('592f0dabfcf681e3d7defb9a'),
otherProperties...
},
{
_id: ObjectId('592f0dabfcf681e3d14cab00'),
otherProperties...
}
];
Also, it is possible to specify an objects array that will be part of generated fixtures.
const recipe = [
{
campaign: ObjectId('ffffffffffffffffffffffff'),
username: 'manuel'
},
{
_id: ObjectId('a1a1a1a1a1a1a1a1a1a1a1a1')
}
];
const fixtures = generateFixtures('invite', recipe);
// Result:
[
{
_id: ObjectId('592f0dabfcf681e3d5ceeb69'),
campaign: ObjectId('ffffffffffffffffffffffff'),
username: 'manuel',
otherProperties...
},
{
_id: ObjectId('a1a1a1a1a1a1a1a1a1a1a1a1'),
campaign: ObjectId('39583dabfcf681e3d5ceeb61'),
username: 'whatever',
otherProperties...
}
];
When a recipe is used to generate fixtures, all passed properties must be valid; otherwise an error is thrown.
const recipe = [
{
oldProperty: 'lalala, I am a property that should not exist',
}
];
const fixtures = generateFixtures('invite', recipe);
// It will throw an error since "oldProperty" is not a valida property for "invite" type.
⚠️ In order make this module flexible, validation is only at keys level. That means, data types are NOT validated (for example: if property username
is declared as string, it won't throw error if an integer is sent in recipe
). It is developer's responsibility to send the correct data type.
FAQs
Creates random fixtures and validate them. Useful when writing tests.
The npm package fixtures-generator receives a total of 0 weekly downloads. As such, fixtures-generator popularity was classified as not popular.
We found that fixtures-generator 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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.