1.0.0 • Published 3 years ago

@iamsquare/jest-match-object-close-to v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@iamsquare/jest-match-object-close-to-extended

Extend Jest to be able to compare objects/arrays having numeric values to a specified number of decimal places. By default, the values are compared to 10 decimal places. Nested objects/arrays are not supported.

Installation

$ npm i -D @iamsquare/jest-match-object-close-to-extended

$ yarn add --dev @iamsquare/jest-match-object-close-to-extended

Usage

import { toMatchObjectCloseTo } from '@iamsquare/jest-match-object-close-to-extended';

expect.extend({ toMatchObjectCloseTo });

describe('sample tests', () => {
  it('should match object with default 10 decimal places precision', () => {
    expect({ foo: 0 }).toMatchObjectCloseTo({ foo: 0.00000000001 });
  });

  it('should match object with defined precision', () => {
    expect({ foo: 2.0000002 }).toMatchObjectCloseTo({ foo: 2 }, 5);
  });

  it('should match array with defined precision', () => {
    expect([12.00001, 10.001]).toMatchObjectCloseTo([12.000009, 10.001002], 5);
  });

  it('should match array of objects with defined precision', () => {
    expect([{ foo: 2.0000002 }, { foo: 3.0000002 }]).toMatchObjectCloseTo([{ foo: 2 }, { foo: 3 }], 5);
  });
});

TODO

  • Add matcher input validation

License

MIT

1.0.0

3 years ago