Skip to content

Commit

Permalink
Add Global Setup/Teardown APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
xfumihiro committed Oct 17, 2017
1 parent 1aef0f6 commit ca2c222
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ exports[`--showConfig outputs config info and exits 1`] = `
\\"clover\\"
],
\\"expand\\": false,
\\"globalSetup\\": null,
\\"globalTeardown\\": null,
\\"listTests\\": false,
\\"mapCoverage\\": false,
\\"maxWorkers\\": \\"[maxWorkers]\\",
Expand Down
29 changes: 29 additions & 0 deletions integration_tests/__tests__/global_setup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @flow
*/
'use strict';

const fs = require('fs');
const os = require('os');
const runJest = require('../runJest');
const {cleanup} = require('../utils');

const DIR = os.tmpdir() + '/jest';

beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

test('globalSetup', () => {
const path = require('path');
const processorPath = path.resolve(__dirname, '../global_setup/processor.js');
const result = runJest.json('global_setup', [
`--globalSetup=${processorPath}`,
]);
expect(result.status).toBe(0);
const setup = fs.readFileSync(DIR + '/setup', 'utf8');
expect(setup).toBe('setup');
});
32 changes: 32 additions & 0 deletions integration_tests/__tests__/global_teardown.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @flow
*/
'use strict';

const fs = require('fs');
const os = require('os');
const runJest = require('../runJest');
const {cleanup} = require('../utils');

const DIR = os.tmpdir() + '/jest';

beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

test('globalTeardown', () => {
const path = require('path');
const processorPath = path.resolve(
__dirname,
'../global_teardown/processor.js',
);
const result = runJest.json('global_teardown', [
`--globalTeardown=${processorPath}`,
]);
expect(result.status).toBe(0);
const teardown = fs.readFileSync(DIR + '/teardown', 'utf8');
expect(teardown).toBe('teardown');
});
9 changes: 9 additions & 0 deletions integration_tests/global_setup/__tests__/processor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('should match 1', () => expect(1).toBe(1));
5 changes: 5 additions & 0 deletions integration_tests/global_setup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
19 changes: 19 additions & 0 deletions integration_tests/global_setup/processor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const fs = require('fs');
const os = require('os');
const mkdirp = require('mkdirp');

const DIR = os.tmpdir() + '/jest';

module.exports = function() {
return new Promise((resolve, reject) => {
mkdirp.sync(DIR);
fs.writeFileSync(DIR + '/setup', 'setup');
resolve();
});
};
9 changes: 9 additions & 0 deletions integration_tests/global_teardown/__tests__/processor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('should match 1', () => expect(1).toBe(1));
5 changes: 5 additions & 0 deletions integration_tests/global_teardown/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
19 changes: 19 additions & 0 deletions integration_tests/global_teardown/processor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const fs = require('fs');
const os = require('os');
const mkdirp = require('mkdirp');

const DIR = os.tmpdir() + '/jest';

module.exports = function() {
return new Promise((resolve, reject) => {
mkdirp.sync(DIR);
fs.writeFileSync(DIR + '/teardown', 'teardown');
resolve();
});
};
8 changes: 8 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ export const options = {
'adequately cleaned up.',
type: 'boolean',
},
globalSetup: {
description: 'The path to a module that runs before All Tests.',
type: 'string',
},
globalTeardown: {
description: 'The path to a module that runs after All Tests.',
type: 'string',
},
globals: {
description:
'A JSON string with map of global variables that need ' +
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ export default async function runJest({
changedFilesPromise: ?ChangedFilesPromise,
onComplete: (testResults: AggregatedResult) => any,
}) {
if (globalConfig.globalSetup) {
// $FlowFixMe
await require(globalConfig.globalSetup)();
}
const sequencer = new TestSequencer();
let allTests = [];
const testRunData = await Promise.all(
Expand Down Expand Up @@ -161,6 +165,10 @@ export default async function runJest({

sequencer.cacheResults(allTests, results);

if (globalConfig.globalTeardown) {
// $FlowFixMe
await require(globalConfig.globalTeardown)();
}
return processResults(results, {
isJSON: globalConfig.json,
onComplete,
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default ({
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
coverageReporters: ['json', 'text', 'lcov', 'clover'],
expand: false,
globalSetup: null,
globalTeardown: null,
globals: {},
haste: {
providesModuleNodeModules: [],
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ const getConfigs = (
expand: options.expand,
findRelatedTests: options.findRelatedTests,
forceExit: options.forceExit,
globalSetup: options.globalSetup,
globalTeardown: options.globalTeardown,
json: options.json,
lastCommit: options.lastCommit,
listTests: options.listTests,
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ export default function normalize(options: InitialOptions, argv: Argv) {
case 'coverageThreshold':
case 'displayName':
case 'expand':
case 'globalSetup':
case 'globalTeardown':
case 'globals':
case 'findRelatedTests':
case 'forceExit':
Expand Down
2 changes: 2 additions & 0 deletions test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
expand: false,
findRelatedTests: false,
forceExit: false,
globalSetup: null,
globalTeardown: null,
json: false,
lastCommit: false,
listTests: false,
Expand Down
2 changes: 2 additions & 0 deletions types/Argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type Argv = {|
expand: boolean,
findRelatedTests: boolean,
forceExit: boolean,
globalSetup: ?string,
globalTeardown: ?string,
globals: string,
h: boolean,
haste: string,
Expand Down
6 changes: 6 additions & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export type DefaultOptions = {|
coverageReporters: Array<string>,
expand: boolean,
globals: ConfigGlobals,
globalSetup: ?string,
globalTeardown: ?string,
haste: HasteConfig,
mapCoverage: boolean,
moduleDirectories: Array<string>,
Expand Down Expand Up @@ -83,6 +85,8 @@ export type InitialOptions = {
forceExit?: boolean,
json?: boolean,
globals?: ConfigGlobals,
globalSetup?: ?string,
globalTeardown?: ?string,
haste?: HasteConfig,
reporters?: Array<ReporterConfig | string>,
logHeapUsage?: boolean,
Expand Down Expand Up @@ -155,6 +159,8 @@ export type GlobalConfig = {|
findRelatedTests: boolean,
forceExit: boolean,
json: boolean,
globalSetup: ?string,
globalTeardown: ?string,
lastCommit: boolean,
logHeapUsage: boolean,
listTests: boolean,
Expand Down

0 comments on commit ca2c222

Please sign in to comment.