From 786f5bcb4895edaacebd6f1ac2cf7671aa93d89c Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Sat, 18 Aug 2018 18:22:00 +0300 Subject: [PATCH 01/11] add global setup and teardown for projects as well --- TestUtils.js | 2 + .../__snapshots__/show_config.test.js.snap | 2 + e2e/__tests__/global_setup.test.js | 54 +++++++++++++++++-- e2e/__tests__/global_teardown.test.js | 50 ++++++++++++++++- e2e/global-setup/project-1/setup.js | 22 ++++++++ e2e/global-setup/project-1/setup.test.js | 20 +++++++ e2e/global-setup/project-2/setup.js | 22 ++++++++ e2e/global-setup/project-2/setup.test.js | 20 +++++++ e2e/global-setup/projects.jest.config.js | 19 +++++++ e2e/global-teardown/project-1/teardown.js | 22 ++++++++ .../project-1/teardown.test.js | 17 ++++++ e2e/global-teardown/project-2/teardown.js | 22 ++++++++ .../project-2/teardown.test.js | 17 ++++++ e2e/global-teardown/projects.jest.config.js | 19 +++++++ jest.config.js | 2 + packages/jest-cli/src/runGlobalModules.js | 49 +++++++++++++++++ packages/jest-cli/src/runJest.js | 31 +++-------- packages/jest-config/src/index.js | 2 + types/Config.js | 2 + 19 files changed, 365 insertions(+), 29 deletions(-) create mode 100644 e2e/global-setup/project-1/setup.js create mode 100644 e2e/global-setup/project-1/setup.test.js create mode 100644 e2e/global-setup/project-2/setup.js create mode 100644 e2e/global-setup/project-2/setup.test.js create mode 100644 e2e/global-setup/projects.jest.config.js create mode 100644 e2e/global-teardown/project-1/teardown.js create mode 100644 e2e/global-teardown/project-1/teardown.test.js create mode 100644 e2e/global-teardown/project-2/teardown.js create mode 100644 e2e/global-teardown/project-2/teardown.test.js create mode 100644 e2e/global-teardown/projects.jest.config.js create mode 100644 packages/jest-cli/src/runGlobalModules.js diff --git a/TestUtils.js b/TestUtils.js index 3cd6e43ecb3e..faf99a081821 100644 --- a/TestUtils.js +++ b/TestUtils.js @@ -79,6 +79,8 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = { errorOnDeprecated: false, filter: null, forceCoverageMatch: [], + globalSetup: null, + globalTeardown: null, globals: {}, haste: { providesModuleNodeModules: [], diff --git a/e2e/__tests__/__snapshots__/show_config.test.js.snap b/e2e/__tests__/__snapshots__/show_config.test.js.snap index 802d244a8076..31869aacee18 100644 --- a/e2e/__tests__/__snapshots__/show_config.test.js.snap +++ b/e2e/__tests__/__snapshots__/show_config.test.js.snap @@ -17,6 +17,8 @@ exports[`--showConfig outputs config info and exits 1`] = ` \\"errorOnDeprecated\\": false, \\"filter\\": null, \\"forceCoverageMatch\\": [], + \\"globalSetup\\": null, + \\"globalTeardown\\": null, \\"globals\\": {}, \\"haste\\": { \\"providesModuleNodeModules\\": [] diff --git a/e2e/__tests__/global_setup.test.js b/e2e/__tests__/global_setup.test.js index be0d626cc350..9842d842569a 100644 --- a/e2e/__tests__/global_setup.test.js +++ b/e2e/__tests__/global_setup.test.js @@ -14,13 +14,27 @@ import runJest, {json as runWithJson} from '../runJest'; import {cleanup} from '../Utils'; const DIR = path.join(os.tmpdir(), 'jest-global-setup'); +const project1DIR = path.join(os.tmpdir(), 'jest-global-setup-project-1'); +const project2DIR = path.join(os.tmpdir(), 'jest-global-setup-project-2'); -beforeEach(() => cleanup(DIR)); -afterAll(() => cleanup(DIR)); +beforeEach(() => { + cleanup(DIR); + cleanup(project1DIR); + cleanup(project2DIR); +}); +afterAll(() => { + cleanup(DIR); + cleanup(project1DIR); + cleanup(project2DIR); +}); test('globalSetup is triggered once before all test suites', () => { const setupPath = path.resolve(__dirname, '../global-setup/setup.js'); - const result = runWithJson('global-setup', [`--globalSetup=${setupPath}`]); + const result = runWithJson('global-setup', [ + `--globalSetup=${setupPath}`, + `--testPathPattern=__tests__`, + ]); + expect(result.status).toBe(0); const files = fs.readdirSync(DIR); expect(files).toHaveLength(1); @@ -32,6 +46,7 @@ test('jest throws an error when globalSetup does not export a function', () => { const setupPath = path.resolve(__dirname, '../global-setup/invalid_setup.js'); const {status, stderr} = runJest('global-setup', [ `--globalSetup=${setupPath}`, + `--testPathPattern=__tests__`, ]); expect(status).toBe(1); @@ -55,3 +70,36 @@ test('globalSetup function gets jest config object as a parameter', () => { expect(result.stdout).toBe(testPathPattern); }); + +test('should call globalSetup function of multiple projects', () => { + const configPath = path.resolve( + __dirname, + '../global-setup/projects.jest.config.js', + ); + + const result = runWithJson('global-setup', [`--config=${configPath}`]); + + expect(result.status).toBe(0); + + expect(fs.existsSync(DIR)).toBe(true); + expect(fs.existsSync(project1DIR)).toBe(true); + expect(fs.existsSync(project2DIR)).toBe(true); +}); + +test('should not call a globalSetup of a project if there are no tests to run from this project', () => { + const configPath = path.resolve( + __dirname, + '../global-setup/projects.jest.config.js', + ); + + const result = runWithJson('global-setup', [ + `--config=${configPath}`, + '--testPathPattern=project-1', + ]); + + expect(result.status).toBe(0); + + expect(fs.existsSync(DIR)).toBe(true); + expect(fs.existsSync(project1DIR)).toBe(true); + expect(fs.existsSync(project2DIR)).toBe(false); +}); diff --git a/e2e/__tests__/global_teardown.test.js b/e2e/__tests__/global_teardown.test.js index 7408a1293155..540d1f999905 100644 --- a/e2e/__tests__/global_teardown.test.js +++ b/e2e/__tests__/global_teardown.test.js @@ -15,9 +15,19 @@ import runJest, {json as runWithJson} from '../runJest'; import {cleanup} from '../Utils'; const DIR = path.join(os.tmpdir(), 'jest-global-teardown'); +const project1DIR = path.join(os.tmpdir(), 'jest-global-teardown-project-1'); +const project2DIR = path.join(os.tmpdir(), 'jest-global-teardown-project-2'); -beforeEach(() => cleanup(DIR)); -afterAll(() => cleanup(DIR)); +beforeEach(() => { + cleanup(DIR); + cleanup(project1DIR); + cleanup(project2DIR); +}); +afterAll(() => { + cleanup(DIR); + cleanup(project1DIR); + cleanup(project2DIR); +}); test('globalTeardown is triggered once after all test suites', () => { mkdirp.sync(DIR); @@ -27,7 +37,9 @@ test('globalTeardown is triggered once after all test suites', () => { ); const result = runWithJson('global-teardown', [ `--globalTeardown=${teardownPath}`, + `--testPathPattern=__tests__`, ]); + expect(result.status).toBe(0); const files = fs.readdirSync(DIR); expect(files).toHaveLength(1); @@ -42,6 +54,7 @@ test('jest throws an error when globalTeardown does not export a function', () = ); const {status, stderr} = runJest('global-teardown', [ `--globalTeardown=${teardownPath}`, + `--testPathPattern=__tests__`, ]); expect(status).toBe(1); @@ -65,3 +78,36 @@ test('globalTeardown function gets jest config object as a parameter', () => { expect(result.stdout).toBe(testPathPattern); }); + +test('should call globalTeardown function of multiple projects', () => { + const configPath = path.resolve( + __dirname, + '../global-teardown/projects.jest.config.js', + ); + + const result = runJest.json('global-teardown', [`--config=${configPath}`]); + + expect(result.status).toBe(0); + + expect(fs.existsSync(DIR)).toBe(true); + expect(fs.existsSync(project1DIR)).toBe(true); + expect(fs.existsSync(project2DIR)).toBe(true); +}); + +test('should not call a globalTeardown of a project if there are no tests to run from this project', () => { + const configPath = path.resolve( + __dirname, + '../global-teardown/projects.jest.config.js', + ); + + const result = runJest.json('global-teardown', [ + `--config=${configPath}`, + '--testPathPattern=project-1', + ]); + + expect(result.status).toBe(0); + + expect(fs.existsSync(DIR)).toBe(true); + expect(fs.existsSync(project1DIR)).toBe(true); + expect(fs.existsSync(project2DIR)).toBe(false); +}); diff --git a/e2e/global-setup/project-1/setup.js b/e2e/global-setup/project-1/setup.js new file mode 100644 index 000000000000..aa6897cf9645 --- /dev/null +++ b/e2e/global-setup/project-1/setup.js @@ -0,0 +1,22 @@ +/** + * 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 crypto = require('crypto'); +const fs = require('fs'); +const mkdirp = require('mkdirp'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-setup-project-1'); + +module.exports = function() { + return new Promise((resolve, reject) => { + mkdirp.sync(DIR); + const fileId = crypto.randomBytes(20).toString('hex'); + fs.writeFileSync(path.join(DIR, fileId), 'setup'); + resolve(); + }); +}; diff --git a/e2e/global-setup/project-1/setup.test.js b/e2e/global-setup/project-1/setup.test.js new file mode 100644 index 000000000000..aa8e94403a3f --- /dev/null +++ b/e2e/global-setup/project-1/setup.test.js @@ -0,0 +1,20 @@ +/** + * 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'; + +const fs = require('fs'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-setup-project-1'); + +test('should exist setup file', () => { + const files = fs.readdirSync(DIR); + expect(files).toHaveLength(1); + const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8'); + expect(setup).toBe('setup'); +}); diff --git a/e2e/global-setup/project-2/setup.js b/e2e/global-setup/project-2/setup.js new file mode 100644 index 000000000000..e47105b98f13 --- /dev/null +++ b/e2e/global-setup/project-2/setup.js @@ -0,0 +1,22 @@ +/** + * 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 crypto = require('crypto'); +const fs = require('fs'); +const mkdirp = require('mkdirp'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-setup-project-2'); + +module.exports = function() { + return new Promise((resolve, reject) => { + mkdirp.sync(DIR); + const fileId = crypto.randomBytes(20).toString('hex'); + fs.writeFileSync(path.join(DIR, fileId), 'setup'); + resolve(); + }); +}; diff --git a/e2e/global-setup/project-2/setup.test.js b/e2e/global-setup/project-2/setup.test.js new file mode 100644 index 000000000000..082cba8cfa98 --- /dev/null +++ b/e2e/global-setup/project-2/setup.test.js @@ -0,0 +1,20 @@ +/** + * 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'; + +const fs = require('fs'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-setup-project-2'); + +test('should exist setup file', () => { + const files = fs.readdirSync(DIR); + expect(files).toHaveLength(1); + const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8'); + expect(setup).toBe('setup'); +}); diff --git a/e2e/global-setup/projects.jest.config.js b/e2e/global-setup/projects.jest.config.js new file mode 100644 index 000000000000..23d5f5b71db5 --- /dev/null +++ b/e2e/global-setup/projects.jest.config.js @@ -0,0 +1,19 @@ +const path = require('path'); + +module.exports = { + globalSetup: '/setup.js', + projects: [ + { + displayName: 'project-1', + globalSetup: '/setup.js', + rootDir: path.resolve(__dirname, './project-1'), + testMatch: ['/**/*.test.js'], + }, + { + displayName: 'project-2', + globalSetup: '/setup.js', + rootDir: path.resolve(__dirname, './project-2'), + testMatch: ['/**/*.test.js'], + }, + ], +}; diff --git a/e2e/global-teardown/project-1/teardown.js b/e2e/global-teardown/project-1/teardown.js new file mode 100644 index 000000000000..7486fde66812 --- /dev/null +++ b/e2e/global-teardown/project-1/teardown.js @@ -0,0 +1,22 @@ +/** + * 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 crypto = require('crypto'); +const fs = require('fs'); +const mkdirp = require('mkdirp'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-teardown-project-1'); + +module.exports = function() { + return new Promise((resolve, reject) => { + mkdirp.sync(DIR); + const fileId = crypto.randomBytes(20).toString('hex'); + fs.writeFileSync(path.join(DIR, fileId), 'teardown'); + resolve(); + }); +}; diff --git a/e2e/global-teardown/project-1/teardown.test.js b/e2e/global-teardown/project-1/teardown.test.js new file mode 100644 index 000000000000..ac42013e1b05 --- /dev/null +++ b/e2e/global-teardown/project-1/teardown.test.js @@ -0,0 +1,17 @@ +/** + * 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'; + +const fs = require('fs'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-teardown-project-1'); + +test('should not exist teardown file', () => { + expect(fs.existsSync(DIR)).toBe(false); +}); diff --git a/e2e/global-teardown/project-2/teardown.js b/e2e/global-teardown/project-2/teardown.js new file mode 100644 index 000000000000..10c44b943571 --- /dev/null +++ b/e2e/global-teardown/project-2/teardown.js @@ -0,0 +1,22 @@ +/** + * 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 crypto = require('crypto'); +const fs = require('fs'); +const mkdirp = require('mkdirp'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-teardown-project-2'); + +module.exports = function() { + return new Promise((resolve, reject) => { + mkdirp.sync(DIR); + const fileId = crypto.randomBytes(20).toString('hex'); + fs.writeFileSync(path.join(DIR, fileId), 'teardown'); + resolve(); + }); +}; diff --git a/e2e/global-teardown/project-2/teardown.test.js b/e2e/global-teardown/project-2/teardown.test.js new file mode 100644 index 000000000000..12a26bd97149 --- /dev/null +++ b/e2e/global-teardown/project-2/teardown.test.js @@ -0,0 +1,17 @@ +/** + * 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'; + +const fs = require('fs'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-teardown-project-2'); + +test('should not exist teardown file', () => { + expect(fs.existsSync(DIR)).toBe(false); +}); diff --git a/e2e/global-teardown/projects.jest.config.js b/e2e/global-teardown/projects.jest.config.js new file mode 100644 index 000000000000..ef827dfb2e7a --- /dev/null +++ b/e2e/global-teardown/projects.jest.config.js @@ -0,0 +1,19 @@ +const path = require('path'); + +module.exports = { + globalTeardown: '/teardown.js', + projects: [ + { + displayName: 'project-1', + globalTeardown: '/teardown.js', + rootDir: path.resolve(__dirname, './project-1'), + testMatch: ['/**/*.test.js'], + }, + { + displayName: 'project-2', + globalTeardown: '/teardown.js', + rootDir: path.resolve(__dirname, './project-2'), + testMatch: ['/**/*.test.js'], + }, + ], +}; diff --git a/jest.config.js b/jest.config.js index 2f73f03df927..fe3cbb1d6872 100644 --- a/jest.config.js +++ b/jest.config.js @@ -29,6 +29,8 @@ module.exports = { '/node_modules/', '/examples/', '/e2e/.*/__tests__', + '/e2e/global-setup', + '/e2e/global-teardown', '\\.snap$', '/packages/.*/build', '/packages/.*/build-es5', diff --git a/packages/jest-cli/src/runGlobalModules.js b/packages/jest-cli/src/runGlobalModules.js new file mode 100644 index 000000000000..b3318d715346 --- /dev/null +++ b/packages/jest-cli/src/runGlobalModules.js @@ -0,0 +1,49 @@ +/** + * 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 + */ +import type {GlobalConfig} from 'types/Config'; +import type {Test} from 'types/TestRunner'; + +export default async ({ + allTests, + globalConfig, + moduleName, +}: { + allTests: Array, + globalConfig: GlobalConfig, + moduleName: string, +}) => { + const globalModulePaths = new Set( + allTests.map(test => test.context.config[moduleName]), + ); + + if (globalConfig[moduleName]) { + globalModulePaths.add(globalConfig[moduleName]); + } + + if (globalModulePaths.size > 0) { + await Promise.all( + Array.from(globalModulePaths).map(async modulePath => { + if (!modulePath) { + return null; + } + + // $FlowFixMe + const globalModule = require(modulePath); + + if (typeof globalModule !== 'function') { + throw new TypeError( + `${moduleName} file must export a function at ${modulePath}`, + ); + } + + return globalModule(globalConfig); + }), + ); + } +}; diff --git a/packages/jest-cli/src/runJest.js b/packages/jest-cli/src/runJest.js index ae7257900460..ad7e88221972 100644 --- a/packages/jest-cli/src/runJest.js +++ b/packages/jest-cli/src/runJest.js @@ -21,6 +21,7 @@ import {Console, formatTestResults} from 'jest-util'; import exit from 'exit'; import fs from 'graceful-fs'; import getNoTestsFoundMessage from './getNoTestsFoundMessage'; +import runGlobalModules from './runGlobalModules'; import SearchSource from './SearchSource'; import TestScheduler from './TestScheduler'; import TestSequencer from './TestSequencer'; @@ -271,19 +272,8 @@ export default (async function runJest({ collectHandles = collectNodeHandles(); } - if (globalConfig.globalSetup) { - // $FlowFixMe - const globalSetup = require(globalConfig.globalSetup); - if (typeof globalSetup !== 'function') { - throw new TypeError( - `globalSetup file must export a function at ${ - globalConfig.globalSetup - }`, - ); - } + await runGlobalModules({allTests, globalConfig, moduleName: 'globalSetup'}); - await globalSetup(globalConfig); - } const results = await new TestScheduler( globalConfig, { @@ -294,19 +284,12 @@ export default (async function runJest({ sequencer.cacheResults(allTests, results); - if (globalConfig.globalTeardown) { - // $FlowFixMe - const globalTeardown = require(globalConfig.globalTeardown); - if (typeof globalTeardown !== 'function') { - throw new TypeError( - `globalTeardown file must export a function at ${ - globalConfig.globalTeardown - }`, - ); - } + await runGlobalModules({ + allTests, + globalConfig, + moduleName: 'globalTeardown', + }); - await globalTeardown(globalConfig); - } return processResults(results, { collectHandles, isJSON: globalConfig.json, diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 5138379d9ccd..6084ab90ef6a 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -164,6 +164,8 @@ const getConfigs = ( errorOnDeprecated: options.errorOnDeprecated, filter: options.filter, forceCoverageMatch: options.forceCoverageMatch, + globalSetup: options.globalSetup, + globalTeardown: options.globalTeardown, globals: options.globals, haste: options.haste, moduleDirectories: options.moduleDirectories, diff --git a/types/Config.js b/types/Config.js index d13f38827d4a..89d229a94a73 100644 --- a/types/Config.js +++ b/types/Config.js @@ -253,6 +253,8 @@ export type ProjectConfig = {| errorOnDeprecated: boolean, filter: ?Path, forceCoverageMatch: Array, + globalSetup: ?string, + globalTeardown: ?string, globals: ConfigGlobals, haste: HasteConfig, moduleDirectories: Array, From 9413052c16bc1602d73e4c6a7fbb9e8453649b71 Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Mon, 20 Aug 2018 00:26:30 +0300 Subject: [PATCH 02/11] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28fc48be6596..d6629f0077b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ - `[jest-cli]` Add `changedSince` to allowed watch mode configs ([#6955](https://github.com/facebook/jest/pull/6955)) - `[babel-jest]` Add support for `babel.config.js` added in Babel 7.0.0 ([#6911](https://github.com/facebook/jest/pull/6911)) +- `[jest-cli]` Add Support for `globalSetup` and `globalTeardown` in projects ([#6865](https://github.com/facebook/jest/pull/6865)) ### Fixes From f86bc94f1efcbe2ba4ab9b5129981ff551ebffb7 Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Mon, 20 Aug 2018 01:00:21 +0300 Subject: [PATCH 03/11] change runGlobalModules function name to runGlobalHook --- .../jest-cli/src/{runGlobalModules.js => runGlobalHook.js} | 0 packages/jest-cli/src/runJest.js | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename packages/jest-cli/src/{runGlobalModules.js => runGlobalHook.js} (100%) diff --git a/packages/jest-cli/src/runGlobalModules.js b/packages/jest-cli/src/runGlobalHook.js similarity index 100% rename from packages/jest-cli/src/runGlobalModules.js rename to packages/jest-cli/src/runGlobalHook.js diff --git a/packages/jest-cli/src/runJest.js b/packages/jest-cli/src/runJest.js index ad7e88221972..a6915dd2d906 100644 --- a/packages/jest-cli/src/runJest.js +++ b/packages/jest-cli/src/runJest.js @@ -21,7 +21,7 @@ import {Console, formatTestResults} from 'jest-util'; import exit from 'exit'; import fs from 'graceful-fs'; import getNoTestsFoundMessage from './getNoTestsFoundMessage'; -import runGlobalModules from './runGlobalModules'; +import runGlobalHook from './runGlobalHook'; import SearchSource from './SearchSource'; import TestScheduler from './TestScheduler'; import TestSequencer from './TestSequencer'; @@ -272,7 +272,7 @@ export default (async function runJest({ collectHandles = collectNodeHandles(); } - await runGlobalModules({allTests, globalConfig, moduleName: 'globalSetup'}); + await runGlobalHook({allTests, globalConfig, moduleName: 'globalSetup'}); const results = await new TestScheduler( globalConfig, @@ -284,7 +284,7 @@ export default (async function runJest({ sequencer.cacheResults(allTests, results); - await runGlobalModules({ + await runGlobalHook({ allTests, globalConfig, moduleName: 'globalTeardown', From 0eed21ebcd13a8330faf5fa532048b4406c2e01e Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Sun, 16 Sep 2018 00:36:02 +0300 Subject: [PATCH 04/11] Add documentation in configuration section --- docs/Configuration.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/Configuration.md b/docs/Configuration.md index 5e68d10c74a0..76f497a55f28 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -323,12 +323,16 @@ Default: `undefined` This option allows the use of a custom global setup module which exports an async function that is triggered once before all test suites. This function gets Jest's `globalConfig` object as a parameter. +Note: A global setup module configured in a project (using multi-project runner) will be triggered only when you run at list one test from this project. + ### `globalTeardown` [string] Default: `undefined` This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites. This function gets Jest's `globalConfig` object as a parameter. +Note: A global teardown module configured in a project (using multi-project runner) will be triggered only when you run at list one test from this project. + ### `moduleDirectories` [array] Default: `["node_modules"]` From e61b3315e3d25da875f1572303808119f1626afe Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Wed, 19 Sep 2018 02:47:04 +0300 Subject: [PATCH 05/11] fix typo --- docs/Configuration.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 76f497a55f28..bc42ec6f8d6c 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -323,7 +323,7 @@ Default: `undefined` This option allows the use of a custom global setup module which exports an async function that is triggered once before all test suites. This function gets Jest's `globalConfig` object as a parameter. -Note: A global setup module configured in a project (using multi-project runner) will be triggered only when you run at list one test from this project. +Note: A global setup module configured in a project (using multi-project runner) will be triggered only when you run at least one test from this project. ### `globalTeardown` [string] @@ -331,7 +331,7 @@ Default: `undefined` This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites. This function gets Jest's `globalConfig` object as a parameter. -Note: A global teardown module configured in a project (using multi-project runner) will be triggered only when you run at list one test from this project. +Note: A global teardown module configured in a project (using multi-project runner) will be triggered only when you run at least one test from this project. ### `moduleDirectories` [array] From bcbc6021d6b564b2c2c39e931c5a30c6743761f5 Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Mon, 8 Oct 2018 02:10:10 +0300 Subject: [PATCH 06/11] refactor to use ESM in integration tests --- e2e/__tests__/global_teardown.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/__tests__/global_teardown.test.js b/e2e/__tests__/global_teardown.test.js index 540d1f999905..ca138dee7075 100644 --- a/e2e/__tests__/global_teardown.test.js +++ b/e2e/__tests__/global_teardown.test.js @@ -85,7 +85,7 @@ test('should call globalTeardown function of multiple projects', () => { '../global-teardown/projects.jest.config.js', ); - const result = runJest.json('global-teardown', [`--config=${configPath}`]); + const result = runWithJson('global-teardown', [`--config=${configPath}`]); expect(result.status).toBe(0); @@ -100,7 +100,7 @@ test('should not call a globalTeardown of a project if there are no tests to run '../global-teardown/projects.jest.config.js', ); - const result = runJest.json('global-teardown', [ + const result = runWithJson('global-teardown', [ `--config=${configPath}`, '--testPathPattern=project-1', ]); From 382aac6d7f32b6d042cdd9ddb35dd2963e8153cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 18 Dec 2018 17:51:33 +0100 Subject: [PATCH 07/11] Update Configuration.md --- docs/Configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 7776eec45336..0635b8e1fe84 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -361,7 +361,7 @@ Default: `undefined` This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites. This function gets Jest's `globalConfig` object as a parameter. -Note: A global teardown module configured in a project (using multi-project runner) will be triggered only when you run at least one test from this project. +_Note: A global teardown module configured in a project (using multi-project runner) will be triggered only when you run at least one test from this project._ ### `moduleDirectories` [array] From c2c2c2291844e421a4946ba84602dac2e85981cc Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Tue, 18 Dec 2018 19:04:27 +0200 Subject: [PATCH 08/11] add copyright header --- e2e/global-setup/projects.jest.config.js | 7 +++++++ e2e/global-teardown/projects.jest.config.js | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/e2e/global-setup/projects.jest.config.js b/e2e/global-setup/projects.jest.config.js index 23d5f5b71db5..5d05d3df0a7e 100644 --- a/e2e/global-setup/projects.jest.config.js +++ b/e2e/global-setup/projects.jest.config.js @@ -1,3 +1,10 @@ +/** + * 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 path = require('path'); module.exports = { diff --git a/e2e/global-teardown/projects.jest.config.js b/e2e/global-teardown/projects.jest.config.js index ef827dfb2e7a..9d14d87059fc 100644 --- a/e2e/global-teardown/projects.jest.config.js +++ b/e2e/global-teardown/projects.jest.config.js @@ -1,3 +1,10 @@ +/** + * 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 path = require('path'); module.exports = { From b53f31745ebf465524dbbe168671863d52a730e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 19 Dec 2018 00:33:43 +0200 Subject: [PATCH 09/11] Fix grammer in test description Co-Authored-By: ranyitz --- e2e/global-teardown/project-2/teardown.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/global-teardown/project-2/teardown.test.js b/e2e/global-teardown/project-2/teardown.test.js index 12a26bd97149..55577b686d78 100644 --- a/e2e/global-teardown/project-2/teardown.test.js +++ b/e2e/global-teardown/project-2/teardown.test.js @@ -12,6 +12,6 @@ const path = require('path'); const DIR = path.join(os.tmpdir(), 'jest-global-teardown-project-2'); -test('should not exist teardown file', () => { +test('teardown file should not exist', () => { expect(fs.existsSync(DIR)).toBe(false); }); From c4b794b1d22b54e7302345f188e47860f29f75ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Wed, 19 Dec 2018 00:34:48 +0200 Subject: [PATCH 10/11] Change moduleName type from a string to an enum Co-Authored-By: ranyitz --- packages/jest-cli/src/runGlobalHook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-cli/src/runGlobalHook.js b/packages/jest-cli/src/runGlobalHook.js index b3318d715346..9731a294ac2a 100644 --- a/packages/jest-cli/src/runGlobalHook.js +++ b/packages/jest-cli/src/runGlobalHook.js @@ -16,7 +16,7 @@ export default async ({ }: { allTests: Array, globalConfig: GlobalConfig, - moduleName: string, + moduleName: 'globalSetup' | 'globalTeardown', }) => { const globalModulePaths = new Set( allTests.map(test => test.context.config[moduleName]), From ca16490482e8061060ba40ced0fbda4be7c742c2 Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Wed, 19 Dec 2018 00:40:54 +0200 Subject: [PATCH 11/11] refactor: remove async await and added return type --- packages/jest-cli/src/runGlobalHook.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/jest-cli/src/runGlobalHook.js b/packages/jest-cli/src/runGlobalHook.js index 9731a294ac2a..4fec38ce2a43 100644 --- a/packages/jest-cli/src/runGlobalHook.js +++ b/packages/jest-cli/src/runGlobalHook.js @@ -9,7 +9,7 @@ import type {GlobalConfig} from 'types/Config'; import type {Test} from 'types/TestRunner'; -export default async ({ +export default ({ allTests, globalConfig, moduleName, @@ -17,7 +17,7 @@ export default async ({ allTests: Array, globalConfig: GlobalConfig, moduleName: 'globalSetup' | 'globalTeardown', -}) => { +}): Promise => { const globalModulePaths = new Set( allTests.map(test => test.context.config[moduleName]), ); @@ -27,7 +27,7 @@ export default async ({ } if (globalModulePaths.size > 0) { - await Promise.all( + return Promise.all( Array.from(globalModulePaths).map(async modulePath => { if (!modulePath) { return null; @@ -46,4 +46,6 @@ export default async ({ }), ); } + + return Promise.resolve(); };