Skip to content

Commit c12bad7

Browse files
committed
Add Global Setup/Teardown APIs
1 parent f162044 commit c12bad7

23 files changed

Lines changed: 214 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* `[jest-message-util]` Always remove node internals from stacktraces ([#4695](https://github.com/facebook/jest/pull/4695))
1616

1717
### Features
18+
* `[jest-config]` Add Global Setup/Teardown options ([#4716](https://github.com/facebook/jest/pull/4716))
1819
* `[jest-environment-*]` [**BREAKING**] Add Async Test Environment APIs, dispose is now teardown ([#4506](https://github.com/facebook/jest/pull/4506))
1920
* `[jest-cli]` Add an option to clear the cache ([#4430](https://github.com/facebook/jest/pull/4430))
2021
* `[babel-plugin-jest-hoist]` Improve error message, that the second argument of `jest.mock` must be an inline function ([#4593](https://github.com/facebook/jest/pull/4593))

docs/Configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ For example, the following would create a global `__DEV__` variable set to `true
170170

171171
Note that, if you specify a global reference value (like an object or array) here, and some code mutates that value in the midst of running a test, that mutation will *not* be persisted across test runs for other test files.
172172

173+
### `globalSetup` [string]
174+
Default: `undefined`
175+
176+
This option allows the use of a custom global setup module which exports an async function that is triggered once before all test suites.
177+
178+
### `globalTeardown` [string]
179+
Default: `undefined`
180+
181+
This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites.
182+
173183
### `mapCoverage` [boolean]
174184

175185
##### available in Jest **20.0.0+**

integration_tests/__tests__/__snapshots__/show_config.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ exports[`--showConfig outputs config info and exits 1`] = `
6666
\\"clover\\"
6767
],
6868
\\"expand\\": false,
69+
\\"globalSetup\\": null,
70+
\\"globalTeardown\\": null,
6971
\\"listTests\\": false,
7072
\\"mapCoverage\\": false,
7173
\\"maxWorkers\\": \\"[maxWorkers]\\",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* @flow
7+
*/
8+
'use strict';
9+
10+
const fs = require('fs');
11+
const os = require('os');
12+
const runJest = require('../runJest');
13+
const {cleanup} = require('../utils');
14+
15+
const DIR = os.tmpdir() + '/jest';
16+
17+
beforeEach(() => cleanup(DIR));
18+
afterAll(() => cleanup(DIR));
19+
20+
test('globalSetup is triggered once before all test suites', () => {
21+
const path = require('path');
22+
const setupPath = path.resolve(__dirname, '../global_setup/setup.js');
23+
const result = runJest.json('global_setup', [`--globalSetup=${setupPath}`]);
24+
expect(result.status).toBe(0);
25+
const files = fs.readdirSync(DIR);
26+
expect(files.length).toBe(1);
27+
const setup = fs.readFileSync(DIR + '/' + files[0], 'utf8');
28+
expect(setup).toBe('setup');
29+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* @flow
7+
*/
8+
'use strict';
9+
10+
const fs = require('fs');
11+
const os = require('os');
12+
const runJest = require('../runJest');
13+
const {cleanup} = require('../utils');
14+
15+
const DIR = os.tmpdir() + '/jest';
16+
17+
beforeEach(() => cleanup(DIR));
18+
afterAll(() => cleanup(DIR));
19+
20+
test('globalTeardown is triggered once after all test suites', () => {
21+
const path = require('path');
22+
const teardownPath = path.resolve(
23+
__dirname,
24+
'../global_teardown/teardown.js',
25+
);
26+
const result = runJest.json('global_teardown', [
27+
`--globalTeardown=${teardownPath}`,
28+
]);
29+
expect(result.status).toBe(0);
30+
const files = fs.readdirSync(DIR);
31+
expect(files.length).toBe(1);
32+
const teardown = fs.readFileSync(DIR + '/' + files[0], 'utf8');
33+
expect(teardown).toBe('teardown');
34+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
test('should match 1', () => expect(1).toBe(1));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
test('should match 1', () => expect(1).toBe(1));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
'use strict';
8+
9+
test('should match 1', () => expect(1).toBe(1));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
}
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
const fs = require('fs');
8+
const crypto = require('crypto');
9+
const os = require('os');
10+
const mkdirp = require('mkdirp');
11+
12+
const DIR = os.tmpdir() + '/jest';
13+
14+
module.exports = function() {
15+
return new Promise((resolve, reject) => {
16+
mkdirp.sync(DIR);
17+
const fileId = crypto.randomBytes(20).toString('hex');
18+
fs.writeFileSync(DIR + '/' + fileId, 'setup');
19+
resolve();
20+
});
21+
};

0 commit comments

Comments
 (0)