August 2017
Beginner
374 pages
10h 41m
English
Sometimes, you want to execute some code before each test runs, to setup the environment for the test. For this case, Jest provides a beforeEach function. After the test completes, you might want to clean up the environment, with the afterEach function.
For example, we might want to create some posts before each test runs and remove them again after the tests have finished:
let posts = []beforeEach(() => { posts.push({ title: 'test' }) posts.push({ title: 'hello world' })})afterEach(() => { posts = []})test('test post exists', () => expect(posts).toContain({ title: 'test' }))test('hello world post exists', () => expect(posts).toContain({ title: 'hello world' }))
Read now
Unlock full access