
Research
/Security News
Contagious Interview Campaign Escalates With 67 Malicious npm Packages and New Malware Loader
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
jest-mock-random
Advanced tools
Math.random()
as deterministic Jest mock function.
~ npm install --save-dev jest-mock-random
// or
~ yarn add --dev jest-mock-random
jest-mock-random can be used as an additional beforeEach for your sequence of test.
import { mockRandomForEach } from 'jest-mock-random';
describe('Test with random usage', () => {
mockRandomForEach([0.1, 0.2, 0.3, 0.6]);
it('assigns random the values that we want to mock in order', () => {
const actual = [Math.random(), Math.random(), Math.random(), Math.random()]; // [0.1, 0.2, 0.3, 0.6]
expect(actual).toEqual([0.1, 0.2, 0.3, 0.6]);
});
});
mockRandomWith accept a single value that it will give back every time Math.random is called or an array of values that the mock will use as a circular array.
The values could be or a decimal number or a string that represents a decimal.
In the same way the mock can be use for individual test:
import { mockRandom, resetMockRandom } from 'jest-mock-random';
describe('Test with random usage', () => {
it('assigns random the values that we want to mock in order', () => {
mockRandom([0.1, 0.2]);
const actual = [Math.random(), Math.random(), Math.random(), Math.random()]; // [0.1, 0.2, 0.1, 0.2]
expect(actual).toEqual([0.1, 0.2, 0.1, 0.2]);
resetMockRandom();
});
});
WARNING: if a no decimal value is passed it will print some warnings during the test.
mockRandomForEach([0.1, 0.2]); // OK
mockRandomForEach([0.1, '0.2']); // OK
mockRandomForEach(['0.2', '0.2']); // OK
mockRandomForEach(0.2); // OK
mockRandomForEach('0.2'); // OK
mockRandomForEach('a'); // WARNING
mockRandomForEach(13); // WARNING
mockRandomForEach({}); // WARNING
mockRandomForEach([]); // WARNING
mockRandomForEach(['a', 1]); // WARNING
FAQs
Math.random() as a deterministic jest mock function
The npm package jest-mock-random receives a total of 13,321 weekly downloads. As such, jest-mock-random popularity was classified as popular.
We found that jest-mock-random 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.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.