jest-mock-random
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,7 +0,12 @@ | ||
const mockRandomWith = require('../index'); | ||
const { | ||
mockRandomForEach, | ||
mockRandom, | ||
resetMockRandom, | ||
} = require('../index'); | ||
describe('mockRandomWith', () => { | ||
mockRandomWith([0.1, 0.2, 0.3]); | ||
afterEach(() => jest.restoreAllMocks()); // avoid mock by spy too leak between test | ||
describe('mockRandomForEach', () => { | ||
mockRandomForEach([0.1, 0.2, 0.3]); | ||
describe('Array of values', () => { | ||
mockRandomWith([0.1, 0.2, 0.3]); | ||
mockRandomForEach([0.1, 0.2, 0.3]); | ||
it('mocks the Math random calls with the given array', () => { | ||
@@ -26,3 +31,3 @@ const actual = [...new Array(3)].map(() => Math.random()); | ||
describe('Corner case', () => { | ||
mockRandomWith([]); | ||
mockRandomForEach([]); | ||
it('throw Error in case we pass an empty array', () => { | ||
@@ -35,3 +40,3 @@ const actual = () => Math.random(); | ||
describe('Singular value', () => { | ||
mockRandomWith(0.1); | ||
mockRandomForEach(0.1); | ||
it('returns always the same value in case of passing a singular integer', () => { | ||
@@ -51,3 +56,3 @@ const actual = [...new Array(3)].map(() => Math.random()); | ||
describe('Invalid types warning with single value', () => { | ||
mockRandomWith(45); | ||
mockRandomForEach(45); | ||
it('logs a warning if the singular value passed is not a decimal', () => { | ||
@@ -61,4 +66,14 @@ const spy = jest.spyOn(global.console, 'warn'); | ||
}); | ||
describe('Value 0', () => { | ||
mockRandomForEach(0); | ||
it('does not logs a warning if the value is 0', () => { | ||
const spy = jest.spyOn(global.console, 'warn'); | ||
Math.random(); | ||
expect(spy).not.toBeCalled(); | ||
}); | ||
}); | ||
describe('Invalid types warning with array of values', () => { | ||
mockRandomWith([1, 0.5, 'a']); | ||
mockRandomForEach([1, 0.5, 'a']); | ||
it('logs a warning if any value of the passed array is not a decimal', () => { | ||
@@ -74,3 +89,3 @@ const spy = jest.spyOn(global.console, 'warn'); | ||
let b; | ||
mockRandomWith(0.2); | ||
mockRandomForEach(0.2); | ||
beforeAll(() => { b = [1]; }); | ||
@@ -83,2 +98,27 @@ it('allows other calls to beforeAll', () => { | ||
}); | ||
describe('Individual test mock random', () => { | ||
afterEach(() => resetMockRandom()); | ||
it('mock random for a particular test', () => { | ||
mockRandom([0, 0.1, 0.2]); | ||
const actual = [...new Array(3)].map(() => Math.random()); | ||
expect(actual).toEqual([0, 0.1, 0.2]); | ||
}); | ||
it('mocks individually each test where is declared', () => { | ||
mockRandom(0.2); | ||
const actual = [...new Array(3)].map(() => Math.random()); | ||
expect(actual).toEqual([0.2, 0.2, 0.2]); | ||
}); | ||
it('resets the mock on resetMockRandom', () => { | ||
mockRandom(0.2); | ||
resetMockRandom(); | ||
const actual = [...new Array(3)].map(() => Math.random()); | ||
expect(actual).not.toEqual([0.2, 0.2, 0.2]); | ||
}); | ||
}); | ||
}); |
18
index.js
// Warning in case the passed value is not a decimal | ||
// eslint-disable-next-line no-console | ||
const showWarning = () => console.warn('WARNING: The value that you are using to mock random is not a decimal and it is breaking the real contract'); | ||
const isDecimal = number => !Number.isNaN(number) && number % 1 !== 0; | ||
const isDecimal = number => number === 0 || (!Number.isNaN(number) && number % 1 !== 0); | ||
const warnBrokenContract = (values) => { | ||
@@ -13,3 +13,3 @@ if (!values.map(parseFloat).every(isDecimal)) { | ||
const mathCopy = Object.create(global.Math); | ||
const resetMathRandom = () => { | ||
const resetMockRandom = () => { | ||
global.Math = mathCopy; | ||
@@ -39,3 +39,3 @@ }; | ||
// Through a copy of the global Math object we mock the random method | ||
const createMathRandomMock = (values) => { | ||
const mockRandom = (values) => { | ||
const mockMath = Object.create(global.Math); | ||
@@ -47,10 +47,10 @@ mockMath.random = randomMock(values); | ||
// When mockRandomWith is called it create the mock beforeEach and reset it after | ||
const mockRandomWith = (valuesArray) => { | ||
const mockRandomForEach = (valuesArray) => { | ||
// eslint-disable-next-line no-undef | ||
beforeEach(() => { | ||
createMathRandomMock(valuesArray); | ||
mockRandom(valuesArray); | ||
}); | ||
// eslint-disable-next-line no-undef | ||
afterEach(() => { | ||
resetMathRandom(); | ||
resetMockRandom(); | ||
}); | ||
@@ -60,2 +60,6 @@ }; | ||
module.exports = mockRandomWith; | ||
module.exports = { | ||
mockRandomForEach, | ||
mockRandom, | ||
resetMockRandom, | ||
}; |
{ | ||
"name": "jest-mock-random", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Math.random() as a deterministic jest mock function", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
136739
175