Install

1
yarn add --dev jest

Simple Case

1
2
3
4
5
6
// sum.js
function sum (a, b) {
return a + b
}

module.exports = sum
1
2
3
4
5
6
7
8
// sum.test.js
const test = require('jest')
const expect = require('expect')

const sum = require('./sum')
test('adds 1 + 2 to equals 3', () => {
expect(sum(1, 2).toBe(3))
})

With Babel

1
yarn add --dev babel-jest
1
2
3
4
5
6
7
8
9
10
// .babelrc
{
"presets": [
[ "env", {
"targets": {
"node": "current"
}
}]
]
}

Jest will automatically define NODE_ENV as test. It will not use development section like Babel does by default when no NODE_ENV is set.

babel-jest is automatically installed when installing Jest and will automatically transform files if a babel configuration exists in your project. To avoid this, you can explicitly reset the transform configuration option:

In package.json

1
2
3
4
5
{
"jest": {
"transform": {}
}
}

Globals

In your test files, Jest puts each of its methods and objects into the global environment. You don’t need to require or import anything to use them.

Methods

  • afterAll(fn): Runs a function after all the tests in this file have completed. If the function returns a promise, Jest waits for that promise to resolve before continuing. This is often useful if you want to clean up some global setup state that is shared across tests.

If afterAll is inside a describe block, it runs at the end of the describe block.

  • afterEach(fn): Runs a function after each test in this file. If the function returns a promise, Jest waits for the promise to resolve before continuing. This is often useful if you want to clean up some temprary state that is created by each test.

If afterEach is inside a describe block, it only runs after the tests that are inside this describe block.

  • beforeAll(fn): similar to afterAll()

  • beforeEach(fn): similar to afterEach()

  • describe(name, fn): create a block that groups together several related tests in one ‘test suite’. For example, if you have a myBeverage object that is supposed to be delicious but not sour, you could test it with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const myBeverage = {
delicious: true,
sour: false,
}

describe('my beverage', () => {
test('is delicious', () => {
expect(myBeverage.delicious).toBeTruthy()
})

test('is not sour', () => {
expect(myBeverage.sour).toBeFalsy()
})
})

This isn’t required - you can just write the test block directly at the top level.

  • describe.only(name, fn): You can use describe.only if you want to run only one describe block
1
2
3
4
5
6
7
8
9
describe.only('my beverage', () => {
test('is delicious', () => {
expect(myBeverage.delicious).toBeTruthy()
})
})

describe('my other beverage', () => {
// ...will be skipped
})
  • describe.skip(name, fn): you can use describe.skip if you do not want to run a particular describe block:
1
2
3
4
5
6
7
8
9
describe('my beverage', () => {
test('is delicious', () => {
expect(myBeverage.delicious).toBeTruthy()
})
})

describe.skip('my other beverage', () => {
// ... will be skipped
})
  • require.requireActual(moduleName): returns the actual module instead of a mock, bypassing all checks on whether the module should receive a mock implementation or not.

  • require.requireMock(moduleName): returns a mock module instead of the actual module, bypassing all checks on whether the module should be required normally or not.

  • test(name, fn): Also under the alias it(name, fn), all you need in a test file is that the test method which runs a test. For example, let’s say there’s a function inchesOfRain() that should be zero. Your whole test could be:

1
2
3
test('did not rain', () => {
expect(inchesOfRain()).toBe(0)
})

The first argument is the test name; the second argument is a function that contains the expectations to test

If a promise is returned from test, Jest will wait for the promise to resolve before letting the test complete.

1
2
3
4
5
test('has lemon in it', () => {
return fetchBeverageList().then(list => {
expect(list).toContain('lemon')
})
})

Even though the call to test will return right away, the test doesn’t complete until the promise resolves as well.

  • test.only(name, fn): similar to describe.only()

  • test.skip(name, fn): similar to describe.skip()