From ca2c222810aa478ac174d67a3cd28f00902f0cb0 Mon Sep 17 00:00:00 2001 From: Fumihiro Xue Date: Wed, 18 Oct 2017 03:11:14 +0800 Subject: [PATCH] Add Global Setup/Teardown APIs --- .../__snapshots__/show_config.test.js.snap | 2 ++ .../__tests__/global_setup.test.js | 29 +++++++++++++++++ .../__tests__/global_teardown.test.js | 32 +++++++++++++++++++ .../global_setup/__tests__/processor.test.js | 9 ++++++ integration_tests/global_setup/package.json | 5 +++ integration_tests/global_setup/processor.js | 19 +++++++++++ .../__tests__/processor.test.js | 9 ++++++ .../global_teardown/package.json | 5 +++ .../global_teardown/processor.js | 19 +++++++++++ packages/jest-cli/src/cli/args.js | 8 +++++ packages/jest-cli/src/run_jest.js | 8 +++++ packages/jest-config/src/defaults.js | 2 ++ packages/jest-config/src/index.js | 2 ++ packages/jest-config/src/normalize.js | 2 ++ test_utils.js | 2 ++ types/Argv.js | 2 ++ types/Config.js | 6 ++++ 17 files changed, 161 insertions(+) create mode 100644 integration_tests/__tests__/global_setup.test.js create mode 100644 integration_tests/__tests__/global_teardown.test.js create mode 100644 integration_tests/global_setup/__tests__/processor.test.js create mode 100644 integration_tests/global_setup/package.json create mode 100644 integration_tests/global_setup/processor.js create mode 100644 integration_tests/global_teardown/__tests__/processor.test.js create mode 100644 integration_tests/global_teardown/package.json create mode 100644 integration_tests/global_teardown/processor.js diff --git a/integration_tests/__tests__/__snapshots__/show_config.test.js.snap b/integration_tests/__tests__/__snapshots__/show_config.test.js.snap index 57d4aaf6dd63..9e80443bcc1f 100644 --- a/integration_tests/__tests__/__snapshots__/show_config.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/show_config.test.js.snap @@ -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]\\", diff --git a/integration_tests/__tests__/global_setup.test.js b/integration_tests/__tests__/global_setup.test.js new file mode 100644 index 000000000000..cc5006ebba34 --- /dev/null +++ b/integration_tests/__tests__/global_setup.test.js @@ -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'); +}); diff --git a/integration_tests/__tests__/global_teardown.test.js b/integration_tests/__tests__/global_teardown.test.js new file mode 100644 index 000000000000..39b6d60c17fd --- /dev/null +++ b/integration_tests/__tests__/global_teardown.test.js @@ -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'); +}); diff --git a/integration_tests/global_setup/__tests__/processor.test.js b/integration_tests/global_setup/__tests__/processor.test.js new file mode 100644 index 000000000000..d5e9fd705b16 --- /dev/null +++ b/integration_tests/global_setup/__tests__/processor.test.js @@ -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)); diff --git a/integration_tests/global_setup/package.json b/integration_tests/global_setup/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/integration_tests/global_setup/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/integration_tests/global_setup/processor.js b/integration_tests/global_setup/processor.js new file mode 100644 index 000000000000..7cbf31c66c5f --- /dev/null +++ b/integration_tests/global_setup/processor.js @@ -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(); + }); +}; diff --git a/integration_tests/global_teardown/__tests__/processor.test.js b/integration_tests/global_teardown/__tests__/processor.test.js new file mode 100644 index 000000000000..d5e9fd705b16 --- /dev/null +++ b/integration_tests/global_teardown/__tests__/processor.test.js @@ -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)); diff --git a/integration_tests/global_teardown/package.json b/integration_tests/global_teardown/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/integration_tests/global_teardown/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/integration_tests/global_teardown/processor.js b/integration_tests/global_teardown/processor.js new file mode 100644 index 000000000000..721550e1bf7a --- /dev/null +++ b/integration_tests/global_teardown/processor.js @@ -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(); + }); +}; diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 018b7cbd619d..735e1530d572 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -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 ' + diff --git a/packages/jest-cli/src/run_jest.js b/packages/jest-cli/src/run_jest.js index 72d705873e96..56707b04018c 100644 --- a/packages/jest-cli/src/run_jest.js +++ b/packages/jest-cli/src/run_jest.js @@ -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( @@ -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, diff --git a/packages/jest-config/src/defaults.js b/packages/jest-config/src/defaults.js index ddf03c198094..483c8072a6fa 100644 --- a/packages/jest-config/src/defaults.js +++ b/packages/jest-config/src/defaults.js @@ -37,6 +37,8 @@ export default ({ coveragePathIgnorePatterns: [NODE_MODULES_REGEXP], coverageReporters: ['json', 'text', 'lcov', 'clover'], expand: false, + globalSetup: null, + globalTeardown: null, globals: {}, haste: { providesModuleNodeModules: [], diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 5f75a03a233d..0c2f7ad73860 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -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, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index fc09f87e5cee..9ae171e7f527 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -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': diff --git a/test_utils.js b/test_utils.js index 53ad9e0acb99..9de6380a6685 100644 --- a/test_utils.js +++ b/test_utils.js @@ -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, diff --git a/types/Argv.js b/types/Argv.js index 7125a0f75ce9..9387617fc862 100644 --- a/types/Argv.js +++ b/types/Argv.js @@ -33,6 +33,8 @@ export type Argv = {| expand: boolean, findRelatedTests: boolean, forceExit: boolean, + globalSetup: ?string, + globalTeardown: ?string, globals: string, h: boolean, haste: string, diff --git a/types/Config.js b/types/Config.js index c62d9449dea8..9d232e465cce 100644 --- a/types/Config.js +++ b/types/Config.js @@ -32,6 +32,8 @@ export type DefaultOptions = {| coverageReporters: Array, expand: boolean, globals: ConfigGlobals, + globalSetup: ?string, + globalTeardown: ?string, haste: HasteConfig, mapCoverage: boolean, moduleDirectories: Array, @@ -83,6 +85,8 @@ export type InitialOptions = { forceExit?: boolean, json?: boolean, globals?: ConfigGlobals, + globalSetup?: ?string, + globalTeardown?: ?string, haste?: HasteConfig, reporters?: Array, logHeapUsage?: boolean, @@ -155,6 +159,8 @@ export type GlobalConfig = {| findRelatedTests: boolean, forceExit: boolean, json: boolean, + globalSetup: ?string, + globalTeardown: ?string, lastCommit: boolean, logHeapUsage: boolean, listTests: boolean,