Skip to content

Commit ea82f18

Browse files
committed
Add Global Setup/Teardown APIs
1 parent 1aef0f6 commit ea82f18

18 files changed

Lines changed: 171 additions & 0 deletions

File tree

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.
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.
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', () => {
21+
const path = require('path');
22+
const processorPath = path.resolve(__dirname, '../global_setup/processor.js');
23+
const result = runJest.json('global_setup', [
24+
`--globalSetup=${processorPath}`,
25+
]);
26+
expect(result.status).toBe(0);
27+
const setup = fs.readFileSync(DIR + '/setup', 'utf8');
28+
expect(setup).toBe('setup');
29+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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', () => {
21+
const path = require('path');
22+
const processorPath = path.resolve(
23+
__dirname,
24+
'../global_teardown/processor.js',
25+
);
26+
const result = runJest.json('global_teardown', [
27+
`--globalTeardown=${processorPath}`,
28+
]);
29+
expect(result.status).toBe(0);
30+
const teardown = fs.readFileSync(DIR + '/teardown', 'utf8');
31+
expect(teardown).toBe('teardown');
32+
});
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 os = require('os');
9+
const mkdirp = require('mkdirp');
10+
11+
const DIR = os.tmpdir() + '/jest';
12+
13+
module.exports = function() {
14+
return new Promise((resolve, reject) => {
15+
mkdirp.sync(DIR);
16+
fs.writeFileSync(DIR + '/setup', 'setup');
17+
resolve();
18+
});
19+
};
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 os = require('os');
9+
const mkdirp = require('mkdirp');
10+
11+
const DIR = os.tmpdir() + '/jest';
12+
13+
module.exports = function() {
14+
return new Promise((resolve, reject) => {
15+
mkdirp.sync(DIR);
16+
fs.writeFileSync(DIR + '/teardown', 'teardown');
17+
resolve();
18+
});
19+
};

0 commit comments

Comments
 (0)