When unit testing in a JavaScript/TypeScript project I have found great value in using chancejs (https://chancejs.com). But in each different spec file I have to instantiate a new instance of chance like so:
Example with instances:
//Spec 1
const chance = new Chance();
const personMock = {
firstName: chance.name(),
age: chance.integer(),
};
//Spec 2
const chance = new Chance();
const addressMock = {
address: chance.address(),
isApartment: chance.bool(),
};
I have not discovered a need for a chance instance, except when a known seed is declared (https://chancejs.com/usage/seed.html). In all cases where it is not desireable to use a seed, it seems like instantiating chance is only extra overhead.
Question 1: Has anybody desired using chance in the same way we use Math.random()?
Example without instances:
//Spec 1
const personMock = {
firstName: Chance.name(),
age: Chance.integer(),
};
//Spec 2
const addressMock = {
address: Chance.address(),
isApartment: Chance.bool(),
};
Question 2: Can you foresee any drawbacks to doing so?
Aucun commentaire:
Enregistrer un commentaire