Skip to content

Commit ca2c222

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

17 files changed

Lines changed: 161 additions & 0 deletions

File tree

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+
};

packages/jest-cli/src/cli/args.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ export const options = {
231231
'adequately cleaned up.',
232232
type: 'boolean',
233233
},
234+
globalSetup: {
235+
description: 'The path to a module that runs before All Tests.',
236+
type: 'string',
237+
},
238+
globalTeardown: {
239+
description: 'The path to a module that runs after All Tests.',
240+
type: 'string',
241+
},
234242
globals: {
235243
description:
236244
'A JSON string with map of global variables that need ' +

0 commit comments

Comments
 (0)