Skip to content

Assert deeply nested values in Jest

License

Notifications You must be signed in to change notification settings

Thinkmill/jest-expect-contain-deep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Sep 22, 2017
f3112ab · Sep 22, 2017

History

5 Commits
Sep 21, 2017
Sep 21, 2017
Sep 21, 2017
Sep 21, 2017
Sep 21, 2017
Sep 21, 2017
Sep 21, 2017
Sep 22, 2017
Sep 21, 2017
Sep 21, 2017

Repository files navigation

jest-expect-contain-deep

Assert deeply nested values in Jest

Installation

yarn add --dev jest-expect-contain-deep

Usage

Before:

const containDeep = require('jest-expect-contain-deep');

test('values', () => {
  let massiveObject = getMassiveObject();
  expect(massiveObject.foo).toBe(expect.arrayContaining([1, 2]));
  expect(massiveObject.bar.prop).toBe(true);
  expect(massiveObject.baz).toContain('bar');
});

test('spies', () => {
  doSomething();
  expect(mySpy.mock.calls[0][0].foo).toBe(expect.arrayContaining([1, 2]));
  expect(mySpy.mock.calls[0][0].bar.prop).toBe(true);
  expect(mySpy.mock.calls[0][0].baz).toContain('bar');
});

After:

const containDeep = require('jest-expect-contain-deep');

test('values', () => {
  expect(getMassiveObject()).toEqual(containDeep({
    foo: [1, 2],
    bar: { prop: true },
    baz: expect.stringContaining('bar'),
  }));
});

test('spies', () => {
  doSomething();
  expect(mySpy).toHaveBeenCalledWith('arg1', containDeep({
    foo: [1, 2],
    bar: { prop: true },
    baz: expect.stringContaining('bar'),
  }));
});