Install
1 | yarn add --dev jest |
Simple Case
1 | // sum.js |
1 | // sum.test.js |
With Babel
1 | yarn add --dev babel-jest |
1 | // .babelrc |
Jest will automatically define
NODE_ENVastest. It will not usedevelopmentsection like Babel does by default when noNODE_ENVis set.
babel-jestis 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 thetransformconfiguration option:
In package.json
1 | { |
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
myBeverageobject that is supposed to be delicious but not sour, you could test it with:
1 | const myBeverage = { |
This isn’t required - you can just write the test block directly at the top level.
- describe.only(name, fn): You can use
describe.onlyif you want to run only one describe block
1 | describe.only('my beverage', () => { |
- describe.skip(name, fn): you can use
describe.skipif you do not want to run a particular describe block:
1 | describe('my beverage', () => { |
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 thetestmethod which runs a test. For example, let’s say there’s a functioninchesOfRain()that should be zero. Your whole test could be:
1 | test('did not rain', () => { |
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 | test('has lemon in it', () => { |
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()

