Skip to content

epodgaetskiy/jest-cheat-sheet

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 

Repository files navigation

Jest cheat sheet

Basic test

describe('makePoniesPink', () => {
  test('make each pony pink', () => {
    const actual = fn(['Alice', 'Bob', 'Eve']);
    expect(actual).toEqual(['Pink Alice', 'Pink Bob', 'Pink Eve']);
  });
});

Matchers

expect.assertions(28) // Exactly 28 assertions are called during a test
expect.hasAssertions() // At least one assertion is called during a test

expect(42).toBe(42) // ===
expect(42).not.toBe(3) // !==
expect([1, 2]).toEqual([1, 2]) // Deep equality
expect('').toBeFalsy() // false, 0, '', null, undefined, NaN
expect('foo').toBeTruthy() // Not false, 0, '', null, undefined, NaN
expect(null).toBeNull() // === null
expect(undefined).toBeUndefined() // === undefined
expect(7).toBeDefined() // !== undefined

expect('long string').toMatch('str')
expect('coffee').toMatch(/ff/)
expect('pizza').not.toMatch('coffee')
expect(['pizza', 'coffee']).toEqual([expect.stringContaining('zz'), expect.stringMatching(/ff/)])

expect(new A()).toBeInstanceOf(A)
expect(() => {}).toEqual(expect.any(Function))
expect('pizza').toEqual(expect.anything())

expect({a: 1}).toHaveProperty('a')
expect({a: 1}).toHaveProperty('a', 1)
expect({a: {b: 1}}).toHaveProperty('a.b')
expect({a: 1, b: 2}).toMatchObject({a: 1})
expect({a: 1, b: 2}).toMatchObject({a: expect.any(Number), b: expect.any(Number)})
expect([{a: 1}, {b: 2}]).toEqual([expect.objectContaining({a: expect.any(Number)}), expect.anything()])

expect(2).toBeGreaterThan(1)
expect(1).toBeGreaterThanOrEqual(1)
expect(1).toBeLessThan(2)
expect(1).toBeLessThanOrEqual(1)
expect(0.2 + 0.1).toBeCloseTo(0.3, 5)

expect(['Alice', 'Bob', 'Eve']).toHaveLength(3)
expect(['Alice', 'Bob', 'Eve']).toContain('Alice')
expect([{a: 1}, {a: 2}]).toContainEqual({a: 1})
expect(['Alice', 'Bob', 'Eve']).toEqual(expect.arrayContaining(['Alice', 'Bob']))

// const fn = () => { throw new Error('Out of cheese!') }
expect(fn).toThrow()
expect(fn).toThrow('Out of cheese')
expect(fn).toThrowErrorMatchingSnapshot()

expect(node).toMatchSnapshot()

// const fn = jest.fn()
fn.mockClear() // Clear number of calls
expect(fn).toBeCalled() // Function was called
expect(fn).not.toBeCalled() // Function was *not* called
expect(fn).toHaveBeenCalledTimes(1) // Function was called only once
expect(fn).toBeCalledWith('first arg', 'second arg') // Any of calls was with these arguments
expect(fn).toHaveBeenLastCalledWith('first arg', 'secon arg') // Last call was with these arguments
expect(fn.mock.calls).toEqual([['first', 'call', 'args'], ['second', 'call', 'args']]) // Multiple calls
expect(fn.mock.calls[0][0](1)).toBe(2) // fn.mock.calls[0][0] — the first argument of the first call

Matchers docs

Aliases

  • toBeCalled → toHaveBeenCalled
  • toBeCalledWith → toHaveBeenCalledWith
  • lastCalledWithtoHaveBeenLastCalledWith
  • toThrowError → toThrow

Promise matchers (Jest 20+)

test('resolve to lemon', () => {
  expect.assertions(1);
  // Make sure to add a return statement
  return expect(Promise.resolve('lemon')).resolves.toBe('lemon');
  // return expect(Promise.reject('octopus')).rejects.toBeDefined();
});

Or with async/await:

test('resolve to lemon', async () => {
  expect.assertions(2);
  await expect(Promise.resolve('lemon')).resolves.toBe('lemon');
  await expect(Promise.resolve('lemon')).resolves.not.toBe('octopus');
});

resolves docs

Mocks

Mock functions

test('call the callback', () => {
  const callback = jest.fn();
  fn(callback);
  expect(callback).toBeCalled();
  expect(callback.mock.calls[0][1].baz).toBe('pizza'); // Second argument of the first call
});

You can also pass an implementation to jest.fn function:

const callback = jest.fn(() => true);

Mock functions docs

Mock modules using jest.mock method

jest.mock('lodash/memoize', () => a => a); // The original lodash/memoize should exist
jest.mock('lodash/memoize', () => a => a, { virtual: true });  // The original lodash/memoize isn’t required

jest.mock docs

Note: When using babel-jest, calls to jest.mock will automatically be hoisted to the top of the code block. Use jest.doMock if you want to explicitly avoid this behavior.

Mock modules using a mock file

  1. Create a file like __mocks__/lodash/memoize.js:

    module.exports = a => a;
  2. Add to your test:

    jest.mock('lodash/memoize');

Note: When using babel-jest, calls to jest.mock will automatically be hoisted to the top of the code block. Use jest.doMock if you want to explicitly avoid this behavior.

Manual mocks docs

Accessing the original module when using mocks

jest.mock('fs')
const fs = require('fs') // Mocked module
const fs = require.requireActual('fs') // Original module

Resources


You may also like

Contributing

Improvements are welcome! Open an issue or send a pull request.

Author and license

Artem Sapegin, a frontend developer at Here and the creator of React Styleguidist. I also write about frontend at my blog.

CC0 1.0 Universal license, see the included License.md file.

About

Jest cheat sheet

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published