Skip to content

Commit fc15a72

Browse files
committed
Add Global Setup/Teardown APIs
1 parent f20344b commit fc15a72

23 files changed

Lines changed: 279 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969

7070
### Features
7171

72+
* `[jest-config]` Add Global Setup/Teardown options
73+
([#4716](https://github.com/facebook/jest/pull/4716))
7274
* `[jest-config]` Add `testEnvironmentOptions` to apply to jsdom options or node
7375
context. ([#5003](https://github.com/facebook/jest/pull/5003))
7476
* `[jest-jasmine2]` Update Timeout error message to `jest.timeout` and display

docs/Configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ Note that, if you specify a global reference value (like an object or array)
261261
here, and some code mutates that value in the midst of running a test, that
262262
mutation will _not_ be persisted across test runs for other test files.
263263

264+
### `globalSetup` [string]
265+
Default: `undefined`
266+
267+
This option allows the use of a custom global setup module which exports an async function that is triggered once before all test suites.
268+
269+
### `globalTeardown` [string]
270+
Default: `undefined`
271+
272+
This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites.
273+
264274
### `mapCoverage` [boolean]
265275

266276
##### 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
@@ -70,6 +70,8 @@ exports[`--showConfig outputs config info and exits 1`] = `
7070
],
7171
\\"detectLeaks\\": false,
7272
\\"expand\\": false,
73+
\\"globalSetup\\": null,
74+
\\"globalTeardown\\": null,
7375
\\"listTests\\": false,
7476
\\"mapCoverage\\": false,
7577
\\"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 path = require('path');
13+
const runJest = require('../runJest');
14+
const {cleanup} = require('../utils');
15+
16+
const DIR = path.join(os.tmpdir(), 'jest_global_setup');
17+
18+
beforeEach(() => cleanup(DIR));
19+
afterAll(() => cleanup(DIR));
20+
21+
test('globalSetup is triggered once before all test suites', () => {
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).toHaveLength(1);
27+
const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
28+
expect(setup).toBe('setup');
29+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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_global_teardown');
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 teardownPath = path.resolve(
25+
__dirname,
26+
'../global_teardown/teardown.js',
27+
);
28+
const result = runJest.json('global_teardown', [
29+
`--globalTeardown=${teardownPath}`,
30+
]);
31+
expect(result.status).toBe(0);
32+
const files = fs.readdirSync(DIR);
33+
expect(files).toHaveLength(1);
34+
const teardown = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
35+
expect(teardown).toBe('teardown');
36+
});
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 os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), 'jest_global_setup');
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 os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), 'jest_global_setup');
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 os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), 'jest_global_setup');
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_global_setup');
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)