afterAll(fn, timeout)

Runs a function after all the tests in this file have completed. If the function returns a promise or is a generator, Jest waits for that promise to resolve before continuing.

Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before aborting. Note: The default timeout is 5 seconds.

This is often useful if you want to clean up some global setup state that is shared across tests.

For example:

const globalDatabase = makeGlobalDatabase(); function cleanUpDatabase(db) { db.cleanUp(); } afterAll(() => { cleanUpDatabase(globalDatabase); }); test('can find things', () => { return globalDatabase.find('thing', {}, results => { expect(results.length).toBeGreaterThan(0); }); }); test('can insert a thing', () => { return globalDatabase.insert('thing', makeThing(), response => { expect(response.success).toBeTruthy(); }); });

Here the afterAll ensures that cleanUpDatabase is called after all tests run.

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

If you want to run some cleanup after every test instead of after all tests, use afterEach instead.