From b0ce1f16d790bb511c357fa5bd006f4013927f2f Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Mon, 18 Jun 2018 00:04:52 +0300 Subject: [PATCH 1/3] feat: global setup and global teardown now gets globalConfig as a parameter --- docs/Configuration.md | 4 ++-- e2e/__tests__/global_setup.test.js | 16 ++++++++++++++++ e2e/__tests__/global_teardown.test.js | 16 ++++++++++++++++ e2e/global-setup/custom_tests_dir/pass.test.js | 1 + e2e/global-setup/setup-with-config.js | 10 ++++++++++ .../custom_tests_dir/pass.test.js | 1 + e2e/global-teardown/teardown-with-config.js | 10 ++++++++++ packages/jest-cli/src/run_jest.js | 4 ++-- 8 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 e2e/global-setup/custom_tests_dir/pass.test.js create mode 100644 e2e/global-setup/setup-with-config.js create mode 100644 e2e/global-teardown/custom_tests_dir/pass.test.js create mode 100644 e2e/global-teardown/teardown-with-config.js diff --git a/docs/Configuration.md b/docs/Configuration.md index bce49748ad85..7be922d73773 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -303,13 +303,13 @@ Note that, if you specify a global reference value (like an object or array) her 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 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. ### `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 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. ### `moduleDirectories` [array] diff --git a/e2e/__tests__/global_setup.test.js b/e2e/__tests__/global_setup.test.js index 5c14ad22164d..ea412495343c 100644 --- a/e2e/__tests__/global_setup.test.js +++ b/e2e/__tests__/global_setup.test.js @@ -39,3 +39,19 @@ test('jest throws an error when globalSetup does not export a function', () => { `TypeError: globalSetup file must export a function at ${setupPath}`, ); }); + +test('globalSetup function gets jest config object as a parameter', () => { + const setupPath = path.resolve( + __dirname, + '../global-setup/setup-with-config.js', + ); + + const testPathPattern = 'custom_tests_dir/pass'; + + const result = runJest('global-setup', [ + `--globalSetup=${setupPath}`, + `--testPathPattern=${testPathPattern}`, + ]); + + expect(result.stdout).toBe(testPathPattern); +}); diff --git a/e2e/__tests__/global_teardown.test.js b/e2e/__tests__/global_teardown.test.js index 92e2e28486a3..af8ea535df48 100644 --- a/e2e/__tests__/global_teardown.test.js +++ b/e2e/__tests__/global_teardown.test.js @@ -49,3 +49,19 @@ test('jest throws an error when globalTeardown does not export a function', () = `TypeError: globalTeardown file must export a function at ${teardownPath}`, ); }); + +test('globalTeardown function gets jest config object as a parameter', () => { + const teardownPath = path.resolve( + __dirname, + '../global-teardown/teardown-with-config.js', + ); + + const testPathPattern = 'custom_tests_dir/pass'; + + const result = runJest('global-teardown', [ + `--globalTeardown=${teardownPath}`, + `--testPathPattern=${testPathPattern}`, + ]); + + expect(result.stdout).toBe(testPathPattern); +}); diff --git a/e2e/global-setup/custom_tests_dir/pass.test.js b/e2e/global-setup/custom_tests_dir/pass.test.js new file mode 100644 index 000000000000..d223d197adec --- /dev/null +++ b/e2e/global-setup/custom_tests_dir/pass.test.js @@ -0,0 +1 @@ +test('should pass', () => {}); diff --git a/e2e/global-setup/setup-with-config.js b/e2e/global-setup/setup-with-config.js new file mode 100644 index 000000000000..bb4047693f2b --- /dev/null +++ b/e2e/global-setup/setup-with-config.js @@ -0,0 +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. + */ + +module.exports = function(jestConfig) { + console.log(jestConfig.testPathPattern); +}; diff --git a/e2e/global-teardown/custom_tests_dir/pass.test.js b/e2e/global-teardown/custom_tests_dir/pass.test.js new file mode 100644 index 000000000000..d223d197adec --- /dev/null +++ b/e2e/global-teardown/custom_tests_dir/pass.test.js @@ -0,0 +1 @@ +test('should pass', () => {}); diff --git a/e2e/global-teardown/teardown-with-config.js b/e2e/global-teardown/teardown-with-config.js new file mode 100644 index 000000000000..bb4047693f2b --- /dev/null +++ b/e2e/global-teardown/teardown-with-config.js @@ -0,0 +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. + */ + +module.exports = function(jestConfig) { + console.log(jestConfig.testPathPattern); +}; diff --git a/packages/jest-cli/src/run_jest.js b/packages/jest-cli/src/run_jest.js index 5b7468a6cb7a..8b27c967a196 100644 --- a/packages/jest-cli/src/run_jest.js +++ b/packages/jest-cli/src/run_jest.js @@ -278,7 +278,7 @@ export default (async function runJest({ ); } - await globalSetup(); + await globalSetup(globalConfig); } const results = await new TestScheduler( globalConfig, @@ -301,7 +301,7 @@ export default (async function runJest({ ); } - await globalTeardown(); + await globalTeardown(globalConfig); } return processResults(results, { collectHandles, From 1fa533e7585b7f4f62d6f3b2eefbfa2a5100d7ab Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Mon, 18 Jun 2018 00:16:45 +0300 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72b2a46da94f..4d129be3b492 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Fixes +- `[jest-cli]` Pass `globalConfig` as a parameter to `globalSetup` and `globalTeardown` functions ([#6486](https://github.com/facebook/jest/pull/6486)) - `[jest-config]` Add missing options to the `defaults` object ([#6428](https://github.com/facebook/jest/pull/6428)) - `[expect]` Using symbolic property names in arrays no longer causes the `toEqual` matcher to fail ([#6391](https://github.com/facebook/jest/pull/6391)) - `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([#6398](https://github.com/facebook/jest/pull/6398)) From d66f59b51676028a852942ec0aecee9991a09910 Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Mon, 18 Jun 2018 20:48:35 +0300 Subject: [PATCH 3/3] remove full path from testPathPattern --- e2e/__tests__/global_setup.test.js | 2 +- e2e/__tests__/global_teardown.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/__tests__/global_setup.test.js b/e2e/__tests__/global_setup.test.js index ea412495343c..8772a8e3e07c 100644 --- a/e2e/__tests__/global_setup.test.js +++ b/e2e/__tests__/global_setup.test.js @@ -46,7 +46,7 @@ test('globalSetup function gets jest config object as a parameter', () => { '../global-setup/setup-with-config.js', ); - const testPathPattern = 'custom_tests_dir/pass'; + const testPathPattern = 'pass'; const result = runJest('global-setup', [ `--globalSetup=${setupPath}`, diff --git a/e2e/__tests__/global_teardown.test.js b/e2e/__tests__/global_teardown.test.js index af8ea535df48..82bb9ccd425b 100644 --- a/e2e/__tests__/global_teardown.test.js +++ b/e2e/__tests__/global_teardown.test.js @@ -56,7 +56,7 @@ test('globalTeardown function gets jest config object as a parameter', () => { '../global-teardown/teardown-with-config.js', ); - const testPathPattern = 'custom_tests_dir/pass'; + const testPathPattern = 'pass'; const result = runJest('global-teardown', [ `--globalTeardown=${teardownPath}`,