Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-mock-random

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-mock-random

Math.random() as a deterministic jest mock function

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

jest-mock-random

Math.random() as deterministic Jest mock function.

Install

~ npm install --save-dev jest-mock-random
// or
~ yarn add --dev jest-mock-random

Usage

jest-mock-random is used as an additional beforeEach for your sequence of test.

import mockRandomWith from 'jest-mock-random';

describe('Test with random usage', () => {
  mockRandomWith([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.

WARNING: if a no decimal value is passed it will print some warnings during the test.

  mockRandomWith([0.1, 0.2]); // OK
  mockRandomWith([0.1, '0.2']); // OK
  mockRandomWith(['0.2', '0.2']); // OK
  mockRandomWith(0.2); // OK
  mockRandomWith('0.2'); // OK

  mockRandomWith('a'); // WARNING
  mockRandomWith(13); // WARNING
  mockRandomWith({}); // WARNING
  mockRandomWith([]); // WARNING
  mockRandomWith(['a', 1]); // WARNING

Keywords

FAQs

Package last updated on 16 Jan 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc