Skip to content

Commit d42f293

Browse files
committed
Add Global Setup/Teardown APIs
1 parent 2890498 commit d42f293

23 files changed

Lines changed: 280 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* `[jest-resolve]` Preserve module identity for symlinks ([#4761](https://github.com/facebook/jest/pull/4761))
1919

2020
### Features
21+
* `[jest-config]` Add Global Setup/Teardown options ([#4716](https://github.com/facebook/jest/pull/4716))
2122
* `[jest-environment-*]` [**BREAKING**] Add Async Test Environment APIs, dispose is now teardown ([#4506](https://github.com/facebook/jest/pull/4506))
2223
* `[jest-cli]` Add an option to clear the cache ([#4430](https://github.com/facebook/jest/pull/4430))
2324
* `[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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 path = require('path');
13+
const runJest = require('../runJest');
14+
const {cleanup} = require('../utils');
15+
16+
const DIR = path.join(os.tmpdir(), '/jest');
17+
18+
beforeEach(() => cleanup(DIR));
19+
afterAll(() => cleanup(DIR));
20+
21+
test('globalSetup is triggered once before all test suites', () => {
22+
const path = require('path');
23+
const setupPath = path.resolve(__dirname, '../global_setup/setup.js');
24+
const result = runJest.json('global_setup', [`--globalSetup=${setupPath}`]);
25+
expect(result.status).toBe(0);
26+
const files = fs.readdirSync(DIR);
27+
expect(files).toHaveLength(1);
28+
const setup = fs.readFileSync(path.join(DIR, '/', files[0]), 'utf8');
29+
expect(setup).toBe('setup');
30+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 mkdirp = require('mkdirp');
12+
const os = require('os');
13+
const path = require('path');
14+
const runJest = require('../runJest');
15+
const {cleanup} = require('../utils');
16+
17+
const DIR = path.join(os.tmpdir(), '/jest');
18+
19+
beforeEach(() => cleanup(DIR));
20+
afterAll(() => cleanup(DIR));
21+
22+
test('globalTeardown is triggered once after all test suites', () => {
23+
mkdirp.sync(DIR);
24+
const path = require('path');
25+
const teardownPath = path.resolve(
26+
__dirname,
27+
'../global_teardown/teardown.js',
28+
);
29+
const result = runJest.json('global_teardown', [
30+
`--globalTeardown=${teardownPath}`,
31+
]);
32+
expect(result.status).toBe(0);
33+
const files = fs.readdirSync(DIR);
34+
expect(files).toHaveLength(1);
35+
const teardown = fs.readFileSync(path.join(DIR, '/', files[0]), 'utf8');
36+
expect(teardown).toBe('teardown');
37+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
const fs = require('fs');
10+
const path = require('path');
11+
const os = require('os');
12+
13+
const DIR = path.join(os.tmpdir(), '/jest');
14+
15+
test('should exist setup file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(1);
18+
const setup = fs.readFileSync(path.join(DIR, '/', files[0]), 'utf8');
19+
expect(setup).toBe('setup');
20+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
const fs = require('fs');
10+
const path = require('path');
11+
const os = require('os');
12+
13+
const DIR = path.join(os.tmpdir(), '/jest');
14+
15+
test('should exist setup file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(1);
18+
const setup = fs.readFileSync(path.join(DIR, '/', files[0]), 'utf8');
19+
expect(setup).toBe('setup');
20+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
const fs = require('fs');
10+
const path = require('path');
11+
const os = require('os');
12+
13+
const DIR = path.join(os.tmpdir(), '/jest');
14+
15+
test('should exist setup file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(1);
18+
const setup = fs.readFileSync(path.join(DIR, '/', files[0]), 'utf8');
19+
expect(setup).toBe('setup');
20+
});
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 crypto = require('crypto');
8+
const fs = require('fs');
9+
const mkdirp = require('mkdirp');
10+
const os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), '/jest');
14+
15+
module.exports = function() {
16+
return new Promise((resolve, reject) => {
17+
mkdirp.sync(DIR);
18+
const fileId = crypto.randomBytes(20).toString('hex');
19+
fs.writeFileSync(path.join(DIR, '/', fileId), 'setup');
20+
resolve();
21+
});
22+
};

0 commit comments

Comments
 (0)