From 0691d9473f8f056b8f4781ee222b1a6f9d188d47 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 30 Sep 2018 16:24:57 -0400 Subject: [PATCH 01/15] add ability to require multiple modules after framework loaded --- TestUtils.js | 1 + packages/jest-cli/src/cli/args.js | 6 ++++++ packages/jest-config/src/Defaults.js | 1 + packages/jest-config/src/Descriptions.js | 2 ++ packages/jest-config/src/ValidConfig.js | 1 + packages/jest-config/src/index.js | 1 + packages/jest-config/src/normalize.js | 6 ++++++ packages/jest-jasmine2/src/index.js | 6 ++++++ .../jest-validate/src/__tests__/fixtures/jest_config.js | 1 + types/Argv.js | 1 + types/Config.js | 3 +++ 11 files changed, 29 insertions(+) diff --git a/TestUtils.js b/TestUtils.js index f7d376858913..a10908682524 100644 --- a/TestUtils.js +++ b/TestUtils.js @@ -100,6 +100,7 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = { runner: 'jest-runner', setupFiles: [], setupTestFrameworkScriptFile: null, + setupTestsAfterJest: [], skipFilter: false, skipNodeResolution: false, snapshotResolver: null, diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 25716500f378..a3771736ce97 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -511,6 +511,12 @@ export const options = { 'set up the testing framework before each test.', type: 'string', }, + setupTestsAfterJest: { + description: + 'The paths to modules that run some code to configure or ' + + 'set up the testing framework after each test. ', + type: 'array', + }, showConfig: { default: undefined, description: 'Print your jest config and then exits.', diff --git a/packages/jest-config/src/Defaults.js b/packages/jest-config/src/Defaults.js index fd2c040015d0..18b2630242c4 100644 --- a/packages/jest-config/src/Defaults.js +++ b/packages/jest-config/src/Defaults.js @@ -61,6 +61,7 @@ export default ({ runner: 'jest-runner', setupFiles: [], setupTestFrameworkScriptFile: null, + setupTestsAfterJest: [], skipFilter: false, snapshotSerializers: [], testEnvironment: 'jest-environment-jsdom', diff --git a/packages/jest-config/src/Descriptions.js b/packages/jest-config/src/Descriptions.js index fc8cafb8e785..1d8b7e249952 100644 --- a/packages/jest-config/src/Descriptions.js +++ b/packages/jest-config/src/Descriptions.js @@ -63,6 +63,8 @@ export default ({ 'The paths to modules that run some code to configure or set up the testing environment before each test', setupTestFrameworkScriptFile: 'The path to a module that runs some code to configure or set up the testing framework before each test', + setupTestsAfterJest: + 'The paths to modules that run some code to configure or set up the testing environment after each test', snapshotSerializers: 'A list of paths to snapshot serializer modules Jest should use for snapshot testing', testEnvironment: 'The test environment that will be used for testing', diff --git a/packages/jest-config/src/ValidConfig.js b/packages/jest-config/src/ValidConfig.js index edd84404cff3..c9f6b5b63828 100644 --- a/packages/jest-config/src/ValidConfig.js +++ b/packages/jest-config/src/ValidConfig.js @@ -88,6 +88,7 @@ export default ({ runner: 'jest-runner', setupFiles: ['/setup.js'], setupTestFrameworkScriptFile: '/testSetupFile.js', + setupTestsAfterJest: ['/testSetupFile.js'], silent: true, skipFilter: false, skipNodeResolution: false, diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 5138379d9ccd..c8a14a2db6d7 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -183,6 +183,7 @@ const getConfigs = ( runner: options.runner, setupFiles: options.setupFiles, setupTestFrameworkScriptFile: options.setupTestFrameworkScriptFile, + setupTestsAfterJest: options.setupTestsAfterJest, skipFilter: options.skipFilter, skipNodeResolution: options.skipNodeResolution, snapshotResolver: options.snapshotResolver, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 8f41c357190f..78ab58feb0de 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -109,6 +109,11 @@ const setupPreset = ( if (options.setupFiles) { options.setupFiles = (preset.setupFiles || []).concat(options.setupFiles); } + + if (!options.setupTestsAfterJest) { + options.setupTestsAfterJest = []; + } + if (options.modulePathIgnorePatterns && preset.modulePathIgnorePatterns) { options.modulePathIgnorePatterns = preset.modulePathIgnorePatterns.concat( options.modulePathIgnorePatterns, @@ -414,6 +419,7 @@ export default function normalize(options: InitialOptions, argv: Argv) { value = normalizeCollectCoverageOnlyFrom(options, key); break; case 'setupFiles': + case 'setupTestsAfterJest': case 'snapshotSerializers': value = options[key] && diff --git a/packages/jest-jasmine2/src/index.js b/packages/jest-jasmine2/src/index.js index b93eab79d10c..55ecfd14e856 100644 --- a/packages/jest-jasmine2/src/index.js +++ b/packages/jest-jasmine2/src/index.js @@ -131,6 +131,12 @@ async function jasmine2( runtime.requireModule(config.setupTestFrameworkScriptFile); } + if (config.setupTestsAfterJest.length) { + for (let i = 0; i < config.setupTestsAfterJest.length; i++) { + runtime.requireModule(config.setupTestsAfterJest[i]); + } + } + if (globalConfig.enabledTestsMap) { env.specFilter = spec => { const suiteMap = diff --git a/packages/jest-validate/src/__tests__/fixtures/jest_config.js b/packages/jest-validate/src/__tests__/fixtures/jest_config.js index f7ac1e2a1bc8..9e5c30305386 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jest_config.js +++ b/packages/jest-validate/src/__tests__/fixtures/jest_config.js @@ -108,6 +108,7 @@ const validConfig = { roots: [''], setupFiles: ['/setup.js'], setupTestFrameworkScriptFile: '/testSetupFile.js', + setupTestsAfterJest: ['/testSetupFile.js'], silent: true, snapshotSerializers: ['my-serializer-module'], testEnvironment: 'jest-environment-jsdom', diff --git a/types/Argv.js b/types/Argv.js index ef157f155dfd..30376724ce6c 100644 --- a/types/Argv.js +++ b/types/Argv.js @@ -72,6 +72,7 @@ export type Argv = {| runInBand: boolean, setupFiles: Array, setupTestFrameworkScriptFile: string, + setupTestsAfterJest: Array, showConfig: boolean, silent: boolean, snapshotSerializers: Array, diff --git a/types/Config.js b/types/Config.js index d13f38827d4a..8d4b86f9a83b 100644 --- a/types/Config.js +++ b/types/Config.js @@ -65,6 +65,7 @@ export type DefaultOptions = {| runTestsByPath: boolean, setupFiles: Array, setupTestFrameworkScriptFile: ?Path, + setupTestsAfterJest: Array, skipFilter: boolean, snapshotSerializers: Array, testEnvironment: string, @@ -150,6 +151,7 @@ export type InitialOptions = { scriptPreprocessor?: string, setupFiles?: Array, setupTestFrameworkScriptFile?: Path, + setupTestsAfterJest: Array, silent?: boolean, skipFilter?: boolean, skipNodeResolution?: boolean, @@ -272,6 +274,7 @@ export type ProjectConfig = {| runner: string, setupFiles: Array, setupTestFrameworkScriptFile: ?Path, + setupTestsAfterJest: Array, skipFilter: boolean, skipNodeResolution: boolean, snapshotResolver: ?Path, From 4e5b9023477081ba02cc511b13310e106c87c09e Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 7 Oct 2018 19:24:23 -0400 Subject: [PATCH 02/15] apply logic to jest-circus --- e2e/__tests__/__snapshots__/show_config.test.js.snap | 1 + .../src/legacy_code_todo_rewrite/jest_adapter.js | 6 ++++++ .../src/lib/__tests__/__snapshots__/init.test.js.snap | 3 +++ types/Config.js | 2 +- 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/e2e/__tests__/__snapshots__/show_config.test.js.snap b/e2e/__tests__/__snapshots__/show_config.test.js.snap index 802d244a8076..00ca68a22ae6 100644 --- a/e2e/__tests__/__snapshots__/show_config.test.js.snap +++ b/e2e/__tests__/__snapshots__/show_config.test.js.snap @@ -45,6 +45,7 @@ exports[`--showConfig outputs config info and exits 1`] = ` \\"runner\\": \\"jest-runner\\", \\"setupFiles\\": [], \\"setupTestFrameworkScriptFile\\": null, + \\"setupTestsAfterJest\\": [], \\"skipFilter\\": false, \\"snapshotSerializers\\": [], \\"testEnvironment\\": \\"jest-environment-jsdom\\", diff --git a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js index f40fa28c7ef2..b4e96babbaba 100644 --- a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js +++ b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js @@ -77,6 +77,12 @@ const jestAdapter = async ( runtime.requireModule(config.setupTestFrameworkScriptFile); } + if (config.setupTestsAfterJest.length) { + for (let i = 0; i < config.setupTestsAfterJest.length; i++) { + runtime.requireModule(config.setupTestsAfterJest[i]); + } + } + runtime.requireModule(testPath); const results = await runAndTransformResultsToJestFormat({ config, diff --git a/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap b/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap index adf63d20d15c..eca0aa4e1a38 100644 --- a/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap +++ b/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap @@ -141,6 +141,9 @@ module.exports = { // The path to a module that runs some code to configure or set up the testing framework before each test // setupTestFrameworkScriptFile: null, + // The paths to modules that run some code to configure or set up the testing environment after each test + // setupTestsAfterJest: [], + // A list of paths to snapshot serializer modules Jest should use for snapshot testing // snapshotSerializers: [], diff --git a/types/Config.js b/types/Config.js index 8d4b86f9a83b..78303532637c 100644 --- a/types/Config.js +++ b/types/Config.js @@ -151,7 +151,7 @@ export type InitialOptions = { scriptPreprocessor?: string, setupFiles?: Array, setupTestFrameworkScriptFile?: Path, - setupTestsAfterJest: Array, + setupTestsAfterJest?: Array, silent?: boolean, skipFilter?: boolean, skipNodeResolution?: boolean, From 4910e562ad9c9efe5cd29325c0542976a3fefb45 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 7 Oct 2018 19:30:07 -0400 Subject: [PATCH 03/15] add tests to determine if normalize works --- .../src/__tests__/normalize.test.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 9fea83c1b07b..6b0559976ad1 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -330,6 +330,53 @@ describe('haste', () => { }); }); +describe('setupTestsAfterJest', () => { + let Resolver; + beforeEach(() => { + Resolver = require('jest-resolve'); + Resolver.findNodeModule = jest.fn( + name => + name.startsWith('/') ? name : '/root/path/foo' + path.sep + name, + ); + }); + + it('normalizes the path according to rootDir', () => { + const {options} = normalize( + { + rootDir: '/root/path/foo', + setupTestsAfterJest: ['bar/baz'], + }, + {}, + ); + + expect(options.setupTestsAfterJest).toEqual([expectedPathFooBar]); + }); + + it('does not change absolute paths', () => { + const {options} = normalize( + { + rootDir: '/root/path/foo', + setupTestsAfterJest: ['/an/abs/path'], + }, + {}, + ); + + expect(options.setupTestsAfterJest).toEqual([expectedPathAbs]); + }); + + it('substitutes tokens', () => { + const {options} = normalize( + { + rootDir: '/root/path/foo', + setupTestsAfterJest: ['/bar/baz'], + }, + {}, + ); + + expect(options.setupTestsAfterJest).toEqual([expectedPathFooBar]); + }); +}); + describe('setupTestFrameworkScriptFile', () => { let Resolver; beforeEach(() => { From 07b6cba3ac13b10106590b3d47b94fff3c768e51 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 7 Oct 2018 22:06:19 -0400 Subject: [PATCH 04/15] add integration tests --- .../setup_tests_after_jest_config.test.js | 60 +++++++++++++++++++ .../__tests__/runner_patch.test.js | 13 ++++ .../__tests__/test1.test.js | 13 ++++ .../__tests__/test2.test.js | 13 ++++ .../package.json | 7 +++ e2e/setup-tests-after-jest-config/setup1.js | 8 +++ e2e/setup-tests-after-jest-config/setup2.js | 8 +++ .../setup_hooks_into_runner.js | 8 +++ 8 files changed, 130 insertions(+) create mode 100644 e2e/__tests__/setup_tests_after_jest_config.test.js create mode 100644 e2e/setup-tests-after-jest-config/__tests__/runner_patch.test.js create mode 100644 e2e/setup-tests-after-jest-config/__tests__/test1.test.js create mode 100644 e2e/setup-tests-after-jest-config/__tests__/test2.test.js create mode 100644 e2e/setup-tests-after-jest-config/package.json create mode 100644 e2e/setup-tests-after-jest-config/setup1.js create mode 100644 e2e/setup-tests-after-jest-config/setup2.js create mode 100644 e2e/setup-tests-after-jest-config/setup_hooks_into_runner.js diff --git a/e2e/__tests__/setup_tests_after_jest_config.test.js b/e2e/__tests__/setup_tests_after_jest_config.test.js new file mode 100644 index 000000000000..92dc2788651b --- /dev/null +++ b/e2e/__tests__/setup_tests_after_jest_config.test.js @@ -0,0 +1,60 @@ +/** + * 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'; + +import path from 'path'; +import {json as runWithJson} from '../runJest'; +import {writeFiles} from '../Utils'; + +const DIR = path.resolve(__dirname, '../setup-tests-after-jest-config'); + +describe('setupTestsAfterJest', () => { + it('requires multiple setup files before each file in the suite', () => { + const pkgJson = { + jest: { + setupTestsAfterJest: ['./setup1.js', './setup2.js'], + }, + }; + + writeFiles(DIR, { + 'package.json': JSON.stringify(pkgJson, null, 2), + }); + + const result = runWithJson('setup-tests-after-jest-config', [ + 'test1.test.js', + 'test2.test.js', + ]); + + expect(result.json.numTotalTests).toBe(2); + expect(result.json.numPassedTests).toBe(2); + expect(result.json.testResults.length).toBe(2); + expect(result.status).toBe(0); + }); + + it('requires setup files *after* the test runners are required', () => { + const pkgJson = { + jest: { + setupTestsAfterJest: ['./setup_hooks_into_runner.js'], + }, + }; + + writeFiles(DIR, { + 'package.json': JSON.stringify(pkgJson, null, 2), + }); + + const result = runWithJson('setup-tests-after-jest-config', [ + 'runner_patch.test.js', + ]); + + expect(result.json.numTotalTests).toBe(1); + expect(result.json.numPassedTests).toBe(1); + expect(result.json.testResults.length).toBe(1); + expect(result.status).toBe(0); + }); +}); diff --git a/e2e/setup-tests-after-jest-config/__tests__/runner_patch.test.js b/e2e/setup-tests-after-jest-config/__tests__/runner_patch.test.js new file mode 100644 index 000000000000..d823155baa9a --- /dev/null +++ b/e2e/setup-tests-after-jest-config/__tests__/runner_patch.test.js @@ -0,0 +1,13 @@ +/** + * 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'; + +describe('setupFile', () => { + it('patches jasmine in setup file', () => { + expect(global.describeDefined).toBe(true); + }); +}); diff --git a/e2e/setup-tests-after-jest-config/__tests__/test1.test.js b/e2e/setup-tests-after-jest-config/__tests__/test1.test.js new file mode 100644 index 000000000000..5a2eb6e580e0 --- /dev/null +++ b/e2e/setup-tests-after-jest-config/__tests__/test1.test.js @@ -0,0 +1,13 @@ +/** + * 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'; + +describe('test', () => { + it('has predefined global variable', () => { + expect(global.definedInSetupFile).toEqual(true); + }); +}); diff --git a/e2e/setup-tests-after-jest-config/__tests__/test2.test.js b/e2e/setup-tests-after-jest-config/__tests__/test2.test.js new file mode 100644 index 000000000000..5a2eb6e580e0 --- /dev/null +++ b/e2e/setup-tests-after-jest-config/__tests__/test2.test.js @@ -0,0 +1,13 @@ +/** + * 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'; + +describe('test', () => { + it('has predefined global variable', () => { + expect(global.definedInSetupFile).toEqual(true); + }); +}); diff --git a/e2e/setup-tests-after-jest-config/package.json b/e2e/setup-tests-after-jest-config/package.json new file mode 100644 index 000000000000..dc052bf1c7bb --- /dev/null +++ b/e2e/setup-tests-after-jest-config/package.json @@ -0,0 +1,7 @@ +{ + "jest": { + "setupTestsAfterJest": [ + "./setup_hooks_into_runner.js" + ] + } +} \ No newline at end of file diff --git a/e2e/setup-tests-after-jest-config/setup1.js b/e2e/setup-tests-after-jest-config/setup1.js new file mode 100644 index 000000000000..c03a0bc8ad12 --- /dev/null +++ b/e2e/setup-tests-after-jest-config/setup1.js @@ -0,0 +1,8 @@ +/** + * 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. + */ + +global.definedInSetupFile = true; diff --git a/e2e/setup-tests-after-jest-config/setup2.js b/e2e/setup-tests-after-jest-config/setup2.js new file mode 100644 index 000000000000..c03a0bc8ad12 --- /dev/null +++ b/e2e/setup-tests-after-jest-config/setup2.js @@ -0,0 +1,8 @@ +/** + * 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. + */ + +global.definedInSetupFile = true; diff --git a/e2e/setup-tests-after-jest-config/setup_hooks_into_runner.js b/e2e/setup-tests-after-jest-config/setup_hooks_into_runner.js new file mode 100644 index 000000000000..9c08b2dfa05a --- /dev/null +++ b/e2e/setup-tests-after-jest-config/setup_hooks_into_runner.js @@ -0,0 +1,8 @@ +/** + * 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. + */ + +global.describeDefined = !!global.describe; From 51fe135e086f48cadd42a19c31dc593343381131 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 7 Oct 2018 22:13:17 -0400 Subject: [PATCH 05/15] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28fc48be6596..a1dd586b4032 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- `[jest-jasmine2/jest-circus/jest-cli]` Setup before tests but after framework loads ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-jasmine2/jest-circus/jest-cli]` Add test.todo ([#6996](https://github.com/facebook/jest/pull/6996)) - `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661)) - `[jest-haste-map]` Add `getFileIterator` to `HasteFS` for faster file iteration ([#7010](https://github.com/facebook/jest/pull/7010)). From c9367f81fb5630691121eed49e78b7f3f8d7d982 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 7 Oct 2018 22:56:10 -0400 Subject: [PATCH 06/15] update descriptions --- packages/jest-cli/src/cli/args.js | 4 ++-- packages/jest-config/src/Descriptions.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index a3771736ce97..0d8024dfe34d 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -513,8 +513,8 @@ export const options = { }, setupTestsAfterJest: { description: - 'The paths to modules that run some code to configure or ' + - 'set up the testing framework after each test. ', + 'A list of paths to modules that run some code to configure or ' + + 'set up the testing framework before each test ', type: 'array', }, showConfig: { diff --git a/packages/jest-config/src/Descriptions.js b/packages/jest-config/src/Descriptions.js index 1d8b7e249952..ddf7897304dc 100644 --- a/packages/jest-config/src/Descriptions.js +++ b/packages/jest-config/src/Descriptions.js @@ -64,7 +64,7 @@ export default ({ setupTestFrameworkScriptFile: 'The path to a module that runs some code to configure or set up the testing framework before each test', setupTestsAfterJest: - 'The paths to modules that run some code to configure or set up the testing environment after each test', + 'A list of paths to modules that run some code to configure or set up the testing framework before each test', snapshotSerializers: 'A list of paths to snapshot serializer modules Jest should use for snapshot testing', testEnvironment: 'The test environment that will be used for testing', From af4ac93f23751c7367788bd3ffdb8735e6d92ec7 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 7 Oct 2018 23:27:50 -0400 Subject: [PATCH 07/15] update snapshots --- .../jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap b/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap index eca0aa4e1a38..d64537a74c1f 100644 --- a/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap +++ b/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap @@ -141,7 +141,7 @@ module.exports = { // The path to a module that runs some code to configure or set up the testing framework before each test // setupTestFrameworkScriptFile: null, - // The paths to modules that run some code to configure or set up the testing environment after each test + // A list of paths to modules that run some code to configure or set up the testing framework before each test // setupTestsAfterJest: [], // A list of paths to snapshot serializer modules Jest should use for snapshot testing From f532ef188d53c7723a719e8389b5d7c191da140f Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 14 Oct 2018 11:30:09 -0400 Subject: [PATCH 08/15] rename config --- TestUtils.js | 2 +- .../__snapshots__/show_config.test.js.snap | 2 +- ...est.js => setup_files_after_env_config.test.js} | 12 ++++++------ .../__tests__/runner_patch.test.js | 0 .../__tests__/test1.test.js | 0 .../__tests__/test2.test.js | 0 .../package.json | 2 +- .../setup1.js | 0 .../setup2.js | 0 .../setup_hooks_into_runner.js | 0 .../src/legacy_code_todo_rewrite/jest_adapter.js | 6 +++--- packages/jest-cli/src/cli/args.js | 12 ++++++------ .../lib/__tests__/__snapshots__/init.test.js.snap | 6 +++--- packages/jest-config/src/Defaults.js | 2 +- packages/jest-config/src/Descriptions.js | 4 ++-- packages/jest-config/src/ValidConfig.js | 2 +- .../jest-config/src/__tests__/normalize.test.js | 14 +++++++------- packages/jest-config/src/index.js | 2 +- packages/jest-config/src/normalize.js | 6 +++--- packages/jest-jasmine2/src/index.js | 6 +++--- .../src/__tests__/fixtures/jest_config.js | 2 +- types/Argv.js | 2 +- types/Config.js | 6 +++--- 23 files changed, 44 insertions(+), 44 deletions(-) rename e2e/__tests__/{setup_tests_after_jest_config.test.js => setup_files_after_env_config.test.js} (77%) rename e2e/{setup-tests-after-jest-config => setup-files-after-env-config}/__tests__/runner_patch.test.js (100%) rename e2e/{setup-tests-after-jest-config => setup-files-after-env-config}/__tests__/test1.test.js (100%) rename e2e/{setup-tests-after-jest-config => setup-files-after-env-config}/__tests__/test2.test.js (100%) rename e2e/{setup-tests-after-jest-config => setup-files-after-env-config}/package.json (68%) rename e2e/{setup-tests-after-jest-config => setup-files-after-env-config}/setup1.js (100%) rename e2e/{setup-tests-after-jest-config => setup-files-after-env-config}/setup2.js (100%) rename e2e/{setup-tests-after-jest-config => setup-files-after-env-config}/setup_hooks_into_runner.js (100%) diff --git a/TestUtils.js b/TestUtils.js index 7ad2c9561dd9..9ee1dbf06c32 100644 --- a/TestUtils.js +++ b/TestUtils.js @@ -99,8 +99,8 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = { roots: [], runner: 'jest-runner', setupFiles: [], + setupFilesAfterEnv: [], setupTestFrameworkScriptFile: null, - setupTestsAfterJest: [], skipFilter: false, skipNodeResolution: false, snapshotResolver: null, diff --git a/e2e/__tests__/__snapshots__/show_config.test.js.snap b/e2e/__tests__/__snapshots__/show_config.test.js.snap index 933cf38a6ce1..a4b3f6138990 100644 --- a/e2e/__tests__/__snapshots__/show_config.test.js.snap +++ b/e2e/__tests__/__snapshots__/show_config.test.js.snap @@ -45,8 +45,8 @@ exports[`--showConfig outputs config info and exits 1`] = ` ], \\"runner\\": \\"jest-runner\\", \\"setupFiles\\": [], + \\"setupFilesAfterEnv\\": [], \\"setupTestFrameworkScriptFile\\": null, - \\"setupTestsAfterJest\\": [], \\"skipFilter\\": false, \\"snapshotSerializers\\": [], \\"testEnvironment\\": \\"jest-environment-jsdom\\", diff --git a/e2e/__tests__/setup_tests_after_jest_config.test.js b/e2e/__tests__/setup_files_after_env_config.test.js similarity index 77% rename from e2e/__tests__/setup_tests_after_jest_config.test.js rename to e2e/__tests__/setup_files_after_env_config.test.js index 92dc2788651b..5d60197f5d59 100644 --- a/e2e/__tests__/setup_tests_after_jest_config.test.js +++ b/e2e/__tests__/setup_files_after_env_config.test.js @@ -12,13 +12,13 @@ import path from 'path'; import {json as runWithJson} from '../runJest'; import {writeFiles} from '../Utils'; -const DIR = path.resolve(__dirname, '../setup-tests-after-jest-config'); +const DIR = path.resolve(__dirname, '../setup-files-after-env-config'); -describe('setupTestsAfterJest', () => { +describe('setupFilesAfterEnv', () => { it('requires multiple setup files before each file in the suite', () => { const pkgJson = { jest: { - setupTestsAfterJest: ['./setup1.js', './setup2.js'], + setupFilesAfterEnv: ['./setup1.js', './setup2.js'], }, }; @@ -26,7 +26,7 @@ describe('setupTestsAfterJest', () => { 'package.json': JSON.stringify(pkgJson, null, 2), }); - const result = runWithJson('setup-tests-after-jest-config', [ + const result = runWithJson('setup-files-after-env-config', [ 'test1.test.js', 'test2.test.js', ]); @@ -40,7 +40,7 @@ describe('setupTestsAfterJest', () => { it('requires setup files *after* the test runners are required', () => { const pkgJson = { jest: { - setupTestsAfterJest: ['./setup_hooks_into_runner.js'], + setupFilesAfterEnv: ['./setup_hooks_into_runner.js'], }, }; @@ -48,7 +48,7 @@ describe('setupTestsAfterJest', () => { 'package.json': JSON.stringify(pkgJson, null, 2), }); - const result = runWithJson('setup-tests-after-jest-config', [ + const result = runWithJson('setup-files-after-env-config', [ 'runner_patch.test.js', ]); diff --git a/e2e/setup-tests-after-jest-config/__tests__/runner_patch.test.js b/e2e/setup-files-after-env-config/__tests__/runner_patch.test.js similarity index 100% rename from e2e/setup-tests-after-jest-config/__tests__/runner_patch.test.js rename to e2e/setup-files-after-env-config/__tests__/runner_patch.test.js diff --git a/e2e/setup-tests-after-jest-config/__tests__/test1.test.js b/e2e/setup-files-after-env-config/__tests__/test1.test.js similarity index 100% rename from e2e/setup-tests-after-jest-config/__tests__/test1.test.js rename to e2e/setup-files-after-env-config/__tests__/test1.test.js diff --git a/e2e/setup-tests-after-jest-config/__tests__/test2.test.js b/e2e/setup-files-after-env-config/__tests__/test2.test.js similarity index 100% rename from e2e/setup-tests-after-jest-config/__tests__/test2.test.js rename to e2e/setup-files-after-env-config/__tests__/test2.test.js diff --git a/e2e/setup-tests-after-jest-config/package.json b/e2e/setup-files-after-env-config/package.json similarity index 68% rename from e2e/setup-tests-after-jest-config/package.json rename to e2e/setup-files-after-env-config/package.json index dc052bf1c7bb..32cc3f7e9df2 100644 --- a/e2e/setup-tests-after-jest-config/package.json +++ b/e2e/setup-files-after-env-config/package.json @@ -1,6 +1,6 @@ { "jest": { - "setupTestsAfterJest": [ + "setupFilesAfterEnv": [ "./setup_hooks_into_runner.js" ] } diff --git a/e2e/setup-tests-after-jest-config/setup1.js b/e2e/setup-files-after-env-config/setup1.js similarity index 100% rename from e2e/setup-tests-after-jest-config/setup1.js rename to e2e/setup-files-after-env-config/setup1.js diff --git a/e2e/setup-tests-after-jest-config/setup2.js b/e2e/setup-files-after-env-config/setup2.js similarity index 100% rename from e2e/setup-tests-after-jest-config/setup2.js rename to e2e/setup-files-after-env-config/setup2.js diff --git a/e2e/setup-tests-after-jest-config/setup_hooks_into_runner.js b/e2e/setup-files-after-env-config/setup_hooks_into_runner.js similarity index 100% rename from e2e/setup-tests-after-jest-config/setup_hooks_into_runner.js rename to e2e/setup-files-after-env-config/setup_hooks_into_runner.js diff --git a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js index b4e96babbaba..61bb3e3fa35b 100644 --- a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js +++ b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js @@ -77,9 +77,9 @@ const jestAdapter = async ( runtime.requireModule(config.setupTestFrameworkScriptFile); } - if (config.setupTestsAfterJest.length) { - for (let i = 0; i < config.setupTestsAfterJest.length; i++) { - runtime.requireModule(config.setupTestsAfterJest[i]); + if (config.setupFilesAfterEnv.length) { + for (let i = 0; i < config.setupFilesAfterEnv.length; i++) { + runtime.requireModule(config.setupFilesAfterEnv[i]); } } diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 0d8024dfe34d..7ff687b4ea87 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -505,18 +505,18 @@ export const options = { 'set up the testing environment before each test. ', type: 'array', }, + setupFilesAfterEnv: { + description: + 'A list of paths to modules that run some code to configure or ' + + 'set up the testing framework before each test ', + type: 'array', + }, setupTestFrameworkScriptFile: { description: 'The path to a module that runs some code to configure or ' + 'set up the testing framework before each test.', type: 'string', }, - setupTestsAfterJest: { - description: - 'A list of paths to modules that run some code to configure or ' + - 'set up the testing framework before each test ', - type: 'array', - }, showConfig: { default: undefined, description: 'Print your jest config and then exits.', diff --git a/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap b/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap index d64537a74c1f..2d156a757f6e 100644 --- a/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap +++ b/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap @@ -138,12 +138,12 @@ module.exports = { // The paths to modules that run some code to configure or set up the testing environment before each test // setupFiles: [], + // A list of paths to modules that run some code to configure or set up the testing framework before each test + // setupFilesAfterEnv: [], + // The path to a module that runs some code to configure or set up the testing framework before each test // setupTestFrameworkScriptFile: null, - // A list of paths to modules that run some code to configure or set up the testing framework before each test - // setupTestsAfterJest: [], - // A list of paths to snapshot serializer modules Jest should use for snapshot testing // snapshotSerializers: [], diff --git a/packages/jest-config/src/Defaults.js b/packages/jest-config/src/Defaults.js index 98cf9f2372c6..d0d038be55b8 100644 --- a/packages/jest-config/src/Defaults.js +++ b/packages/jest-config/src/Defaults.js @@ -61,8 +61,8 @@ export default ({ runTestsByPath: false, runner: 'jest-runner', setupFiles: [], + setupFilesAfterEnv: [], setupTestFrameworkScriptFile: null, - setupTestsAfterJest: [], skipFilter: false, snapshotSerializers: [], testEnvironment: 'jest-environment-jsdom', diff --git a/packages/jest-config/src/Descriptions.js b/packages/jest-config/src/Descriptions.js index ddf7897304dc..88c3a6418a83 100644 --- a/packages/jest-config/src/Descriptions.js +++ b/packages/jest-config/src/Descriptions.js @@ -61,10 +61,10 @@ export default ({ "Allows you to use a custom runner instead of Jest's default test runner", setupFiles: 'The paths to modules that run some code to configure or set up the testing environment before each test', + setupFilesAfterEnv: + 'A list of paths to modules that run some code to configure or set up the testing framework before each test', setupTestFrameworkScriptFile: 'The path to a module that runs some code to configure or set up the testing framework before each test', - setupTestsAfterJest: - 'A list of paths to modules that run some code to configure or set up the testing framework before each test', snapshotSerializers: 'A list of paths to snapshot serializer modules Jest should use for snapshot testing', testEnvironment: 'The test environment that will be used for testing', diff --git a/packages/jest-config/src/ValidConfig.js b/packages/jest-config/src/ValidConfig.js index c9f6b5b63828..e84a9eab5359 100644 --- a/packages/jest-config/src/ValidConfig.js +++ b/packages/jest-config/src/ValidConfig.js @@ -87,8 +87,8 @@ export default ({ runTestsByPath: false, runner: 'jest-runner', setupFiles: ['/setup.js'], + setupFilesAfterEnv: ['/testSetupFile.js'], setupTestFrameworkScriptFile: '/testSetupFile.js', - setupTestsAfterJest: ['/testSetupFile.js'], silent: true, skipFilter: false, skipNodeResolution: false, diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 6b0559976ad1..b0fca75fb5e1 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -330,7 +330,7 @@ describe('haste', () => { }); }); -describe('setupTestsAfterJest', () => { +describe('setupFilesAfterEnv', () => { let Resolver; beforeEach(() => { Resolver = require('jest-resolve'); @@ -344,36 +344,36 @@ describe('setupTestsAfterJest', () => { const {options} = normalize( { rootDir: '/root/path/foo', - setupTestsAfterJest: ['bar/baz'], + setupFilesAfterEnv: ['bar/baz'], }, {}, ); - expect(options.setupTestsAfterJest).toEqual([expectedPathFooBar]); + expect(options.setupFilesAfterEnv).toEqual([expectedPathFooBar]); }); it('does not change absolute paths', () => { const {options} = normalize( { rootDir: '/root/path/foo', - setupTestsAfterJest: ['/an/abs/path'], + setupFilesAfterEnv: ['/an/abs/path'], }, {}, ); - expect(options.setupTestsAfterJest).toEqual([expectedPathAbs]); + expect(options.setupFilesAfterEnv).toEqual([expectedPathAbs]); }); it('substitutes tokens', () => { const {options} = normalize( { rootDir: '/root/path/foo', - setupTestsAfterJest: ['/bar/baz'], + setupFilesAfterEnv: ['/bar/baz'], }, {}, ); - expect(options.setupTestsAfterJest).toEqual([expectedPathFooBar]); + expect(options.setupFilesAfterEnv).toEqual([expectedPathFooBar]); }); }); diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 6f8248719510..0c70ee3f1808 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -184,8 +184,8 @@ const groupOptions = ( roots: options.roots, runner: options.runner, setupFiles: options.setupFiles, + setupFilesAfterEnv: options.setupFilesAfterEnv, setupTestFrameworkScriptFile: options.setupTestFrameworkScriptFile, - setupTestsAfterJest: options.setupTestsAfterJest, skipFilter: options.skipFilter, skipNodeResolution: options.skipNodeResolution, snapshotResolver: options.snapshotResolver, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 78ab58feb0de..7d4c5d2d4052 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -110,8 +110,8 @@ const setupPreset = ( options.setupFiles = (preset.setupFiles || []).concat(options.setupFiles); } - if (!options.setupTestsAfterJest) { - options.setupTestsAfterJest = []; + if (!options.setupFilesAfterEnv) { + options.setupFilesAfterEnv = []; } if (options.modulePathIgnorePatterns && preset.modulePathIgnorePatterns) { @@ -419,7 +419,7 @@ export default function normalize(options: InitialOptions, argv: Argv) { value = normalizeCollectCoverageOnlyFrom(options, key); break; case 'setupFiles': - case 'setupTestsAfterJest': + case 'setupFilesAfterEnv': case 'snapshotSerializers': value = options[key] && diff --git a/packages/jest-jasmine2/src/index.js b/packages/jest-jasmine2/src/index.js index 55ecfd14e856..277b6601f231 100644 --- a/packages/jest-jasmine2/src/index.js +++ b/packages/jest-jasmine2/src/index.js @@ -131,9 +131,9 @@ async function jasmine2( runtime.requireModule(config.setupTestFrameworkScriptFile); } - if (config.setupTestsAfterJest.length) { - for (let i = 0; i < config.setupTestsAfterJest.length; i++) { - runtime.requireModule(config.setupTestsAfterJest[i]); + if (config.setupFilesAfterEnv.length) { + for (let i = 0; i < config.setupFilesAfterEnv.length; i++) { + runtime.requireModule(config.setupFilesAfterEnv[i]); } } diff --git a/packages/jest-validate/src/__tests__/fixtures/jest_config.js b/packages/jest-validate/src/__tests__/fixtures/jest_config.js index 9e5c30305386..549b5548d665 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jest_config.js +++ b/packages/jest-validate/src/__tests__/fixtures/jest_config.js @@ -107,8 +107,8 @@ const validConfig = { rootDir: '/', roots: [''], setupFiles: ['/setup.js'], + setupFilesAfterEnv: ['/testSetupFile.js'], setupTestFrameworkScriptFile: '/testSetupFile.js', - setupTestsAfterJest: ['/testSetupFile.js'], silent: true, snapshotSerializers: ['my-serializer-module'], testEnvironment: 'jest-environment-jsdom', diff --git a/types/Argv.js b/types/Argv.js index 30376724ce6c..db34ebac2437 100644 --- a/types/Argv.js +++ b/types/Argv.js @@ -72,7 +72,7 @@ export type Argv = {| runInBand: boolean, setupFiles: Array, setupTestFrameworkScriptFile: string, - setupTestsAfterJest: Array, + setupFilesAfterEnv: Array, showConfig: boolean, silent: boolean, snapshotSerializers: Array, diff --git a/types/Config.js b/types/Config.js index ddc785ab1037..5cfde84ee647 100644 --- a/types/Config.js +++ b/types/Config.js @@ -66,7 +66,7 @@ export type DefaultOptions = {| runTestsByPath: boolean, setupFiles: Array, setupTestFrameworkScriptFile: ?Path, - setupTestsAfterJest: Array, + setupFilesAfterEnv: Array, skipFilter: boolean, snapshotSerializers: Array, testEnvironment: string, @@ -152,7 +152,7 @@ export type InitialOptions = { scriptPreprocessor?: string, setupFiles?: Array, setupTestFrameworkScriptFile?: Path, - setupTestsAfterJest?: Array, + setupFilesAfterEnv?: Array, silent?: boolean, skipFilter?: boolean, skipNodeResolution?: boolean, @@ -275,7 +275,7 @@ export type ProjectConfig = {| runner: string, setupFiles: Array, setupTestFrameworkScriptFile: ?Path, - setupTestsAfterJest: Array, + setupFilesAfterEnv: Array, skipFilter: boolean, skipNodeResolution: boolean, snapshotResolver: ?Path, From 4801f3710bf38764f39066182e19bd8b89359af8 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 14 Oct 2018 11:35:44 -0400 Subject: [PATCH 09/15] remove package.json after all tests --- e2e/__tests__/setup_files_after_env_config.test.js | 10 ++++++++++ e2e/setup-files-after-env-config/package.json | 7 ------- 2 files changed, 10 insertions(+), 7 deletions(-) delete mode 100644 e2e/setup-files-after-env-config/package.json diff --git a/e2e/__tests__/setup_files_after_env_config.test.js b/e2e/__tests__/setup_files_after_env_config.test.js index 5d60197f5d59..92066827459c 100644 --- a/e2e/__tests__/setup_files_after_env_config.test.js +++ b/e2e/__tests__/setup_files_after_env_config.test.js @@ -8,12 +8,22 @@ */ 'use strict'; +import fs from 'fs'; import path from 'path'; import {json as runWithJson} from '../runJest'; import {writeFiles} from '../Utils'; const DIR = path.resolve(__dirname, '../setup-files-after-env-config'); +const pkgJsonOutputFilePath = path.join( + process.cwd(), + 'e2e/setup-files-after-env-config/package.json', +); + +afterAll(() => { + fs.unlinkSync(pkgJsonOutputFilePath); +}); + describe('setupFilesAfterEnv', () => { it('requires multiple setup files before each file in the suite', () => { const pkgJson = { diff --git a/e2e/setup-files-after-env-config/package.json b/e2e/setup-files-after-env-config/package.json deleted file mode 100644 index 32cc3f7e9df2..000000000000 --- a/e2e/setup-files-after-env-config/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "jest": { - "setupFilesAfterEnv": [ - "./setup_hooks_into_runner.js" - ] - } -} \ No newline at end of file From d419af329d30c68f4e8537e21c6c2821053aa4ec Mon Sep 17 00:00:00 2001 From: David Druker Date: Sat, 20 Oct 2018 12:25:51 -0400 Subject: [PATCH 10/15] remove setupTestFrameworkScriptFile option --- TestUtils.js | 1 - docs/Configuration.md | 12 ++--- .../__snapshots__/show_config.test.js.snap | 1 - ...t_framework_script_file_cli_config.test.js | 40 ---------------- .../__tests__/runner_patch.test.js | 13 ----- .../__tests__/test1.test.js | 13 ----- .../__tests__/test2.test.js | 13 ----- .../package.json | 5 -- .../setup1.js | 8 ---- .../setup_hooks_into_runner.js | 8 ---- jest.config.js | 2 +- .../legacy_code_todo_rewrite/jest_adapter.js | 4 -- packages/jest-cli/src/cli/args.js | 6 --- .../__tests__/__snapshots__/init.test.js.snap | 3 -- packages/jest-config/src/Defaults.js | 1 - packages/jest-config/src/Deprecated.js | 10 ++++ packages/jest-config/src/Descriptions.js | 2 - packages/jest-config/src/ValidConfig.js | 1 - .../__snapshots__/normalize.test.js.snap | 23 +++++++++ .../src/__tests__/normalize.test.js | 48 +++++++------------ packages/jest-config/src/index.js | 1 - packages/jest-config/src/normalize.js | 20 ++++++-- packages/jest-jasmine2/src/index.js | 4 -- .../src/__tests__/fixtures/jest_config.js | 1 - types/Argv.js | 1 - types/Config.js | 2 - 26 files changed, 71 insertions(+), 172 deletions(-) delete mode 100644 e2e/__tests__/setup_test_framework_script_file_cli_config.test.js delete mode 100644 e2e/setup-test-framework-script-file-cli-config/__tests__/runner_patch.test.js delete mode 100644 e2e/setup-test-framework-script-file-cli-config/__tests__/test1.test.js delete mode 100644 e2e/setup-test-framework-script-file-cli-config/__tests__/test2.test.js delete mode 100644 e2e/setup-test-framework-script-file-cli-config/package.json delete mode 100644 e2e/setup-test-framework-script-file-cli-config/setup1.js delete mode 100644 e2e/setup-test-framework-script-file-cli-config/setup_hooks_into_runner.js diff --git a/TestUtils.js b/TestUtils.js index 9ee1dbf06c32..e50088693bdf 100644 --- a/TestUtils.js +++ b/TestUtils.js @@ -100,7 +100,6 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = { runner: 'jest-runner', setupFiles: [], setupFilesAfterEnv: [], - setupTestFrameworkScriptFile: null, skipFilter: false, skipNodeResolution: false, snapshotResolver: null, diff --git a/docs/Configuration.md b/docs/Configuration.md index 03991fbdbd37..6e2842431ba7 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -630,17 +630,15 @@ Default: `[]` The paths to modules that run some code to configure or set up the testing environment before each test. Since every test runs in its own environment, these scripts will be executed in the testing environment immediately before executing the test code itself. -It's worth noting that this code will execute _before_ [`setupTestFrameworkScriptFile`](#setuptestframeworkscriptfile-string). +### `setupFilesAfterEnv` [array] -### `setupTestFrameworkScriptFile` [string] - -Default: `undefined` +Default: `[]` -The path to a module that runs some code to configure or set up the testing framework before each test. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment. +Multiple paths to modules that run some code to configure or set up the testing framework before each test. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment. -If you want this path to be [relative to the root directory of your project](#rootdir-string), please include `` inside the path string, like `"/a-configs-folder"`. +If you want this path to be [relative to the root directory of your project](#rootdir-string), please include `` inside a paths string, like `"/a-configs-folder"`. -For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in this module. +For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules. ### `snapshotResolver` [string] diff --git a/e2e/__tests__/__snapshots__/show_config.test.js.snap b/e2e/__tests__/__snapshots__/show_config.test.js.snap index a4b3f6138990..b42881dbc0ad 100644 --- a/e2e/__tests__/__snapshots__/show_config.test.js.snap +++ b/e2e/__tests__/__snapshots__/show_config.test.js.snap @@ -46,7 +46,6 @@ exports[`--showConfig outputs config info and exits 1`] = ` \\"runner\\": \\"jest-runner\\", \\"setupFiles\\": [], \\"setupFilesAfterEnv\\": [], - \\"setupTestFrameworkScriptFile\\": null, \\"skipFilter\\": false, \\"snapshotSerializers\\": [], \\"testEnvironment\\": \\"jest-environment-jsdom\\", diff --git a/e2e/__tests__/setup_test_framework_script_file_cli_config.test.js b/e2e/__tests__/setup_test_framework_script_file_cli_config.test.js deleted file mode 100644 index 4dcd170a08e2..000000000000 --- a/e2e/__tests__/setup_test_framework_script_file_cli_config.test.js +++ /dev/null @@ -1,40 +0,0 @@ -/** - * 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'; - -import {json as runWithJson} from '../runJest'; - -describe('--setupTestFrameworkScriptFile setup.js', () => { - it('requires a setup file before each file in the suite', () => { - const result = runWithJson('setup-test-framework-script-file-cli-config', [ - '--setupTestFrameworkScriptFile', - './setup1.js', - 'test1.test.js', - 'test2.test.js', - ]); - - expect(result.status).toBe(0); - expect(result.json.numTotalTests).toBe(2); - expect(result.json.numPassedTests).toBe(2); - expect(result.json.testResults.length).toBe(2); - }); - - it('requires setup files *after* the test runners are required', () => { - const result = runWithJson('setup-test-framework-script-file-cli-config', [ - '--setupTestFrameworkScriptFile', - './setup_hooks_into_runner.js', - 'runner_patch.test.js', - ]); - - expect(result.json.numTotalTests).toBe(1); - expect(result.json.numPassedTests).toBe(1); - expect(result.json.testResults.length).toBe(1); - expect(result.status).toBe(0); - }); -}); diff --git a/e2e/setup-test-framework-script-file-cli-config/__tests__/runner_patch.test.js b/e2e/setup-test-framework-script-file-cli-config/__tests__/runner_patch.test.js deleted file mode 100644 index d823155baa9a..000000000000 --- a/e2e/setup-test-framework-script-file-cli-config/__tests__/runner_patch.test.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * 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'; - -describe('setupFile', () => { - it('patches jasmine in setup file', () => { - expect(global.describeDefined).toBe(true); - }); -}); diff --git a/e2e/setup-test-framework-script-file-cli-config/__tests__/test1.test.js b/e2e/setup-test-framework-script-file-cli-config/__tests__/test1.test.js deleted file mode 100644 index 5a2eb6e580e0..000000000000 --- a/e2e/setup-test-framework-script-file-cli-config/__tests__/test1.test.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * 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'; - -describe('test', () => { - it('has predefined global variable', () => { - expect(global.definedInSetupFile).toEqual(true); - }); -}); diff --git a/e2e/setup-test-framework-script-file-cli-config/__tests__/test2.test.js b/e2e/setup-test-framework-script-file-cli-config/__tests__/test2.test.js deleted file mode 100644 index 5a2eb6e580e0..000000000000 --- a/e2e/setup-test-framework-script-file-cli-config/__tests__/test2.test.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * 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'; - -describe('test', () => { - it('has predefined global variable', () => { - expect(global.definedInSetupFile).toEqual(true); - }); -}); diff --git a/e2e/setup-test-framework-script-file-cli-config/package.json b/e2e/setup-test-framework-script-file-cli-config/package.json deleted file mode 100644 index 148788b25446..000000000000 --- a/e2e/setup-test-framework-script-file-cli-config/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "jest": { - "testEnvironment": "node" - } -} diff --git a/e2e/setup-test-framework-script-file-cli-config/setup1.js b/e2e/setup-test-framework-script-file-cli-config/setup1.js deleted file mode 100644 index c03a0bc8ad12..000000000000 --- a/e2e/setup-test-framework-script-file-cli-config/setup1.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * 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. - */ - -global.definedInSetupFile = true; diff --git a/e2e/setup-test-framework-script-file-cli-config/setup_hooks_into_runner.js b/e2e/setup-test-framework-script-file-cli-config/setup_hooks_into_runner.js deleted file mode 100644 index 9c08b2dfa05a..000000000000 --- a/e2e/setup-test-framework-script-file-cli-config/setup_hooks_into_runner.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * 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. - */ - -global.describeDefined = !!global.describe; diff --git a/jest.config.js b/jest.config.js index 4385ccffd943..8873ad993dd2 100644 --- a/jest.config.js +++ b/jest.config.js @@ -21,7 +21,7 @@ module.exports = { 'e2e/runtime-internal-module-registry/__mocks__', ], projects: ['', '/examples/*/'], - setupTestFrameworkScriptFile: '/testSetupFile.js', + setupFilesAfterEnv: ['/testSetupFile.js'], snapshotSerializers: [ '/packages/pretty-format/build/plugins/convert_ansi.js', ], diff --git a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js index 61bb3e3fa35b..60e77ca26259 100644 --- a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js +++ b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js @@ -73,10 +73,6 @@ const jestAdapter = async ( } }); - if (config.setupTestFrameworkScriptFile) { - runtime.requireModule(config.setupTestFrameworkScriptFile); - } - if (config.setupFilesAfterEnv.length) { for (let i = 0; i < config.setupFilesAfterEnv.length; i++) { runtime.requireModule(config.setupFilesAfterEnv[i]); diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 7ff687b4ea87..fd5319b166e7 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -511,12 +511,6 @@ export const options = { 'set up the testing framework before each test ', type: 'array', }, - setupTestFrameworkScriptFile: { - description: - 'The path to a module that runs some code to configure or ' + - 'set up the testing framework before each test.', - type: 'string', - }, showConfig: { default: undefined, description: 'Print your jest config and then exits.', diff --git a/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap b/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap index 2d156a757f6e..59579c65367c 100644 --- a/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap +++ b/packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap @@ -141,9 +141,6 @@ module.exports = { // A list of paths to modules that run some code to configure or set up the testing framework before each test // setupFilesAfterEnv: [], - // The path to a module that runs some code to configure or set up the testing framework before each test - // setupTestFrameworkScriptFile: null, - // A list of paths to snapshot serializer modules Jest should use for snapshot testing // snapshotSerializers: [], diff --git a/packages/jest-config/src/Defaults.js b/packages/jest-config/src/Defaults.js index d0d038be55b8..788333e96c13 100644 --- a/packages/jest-config/src/Defaults.js +++ b/packages/jest-config/src/Defaults.js @@ -62,7 +62,6 @@ export default ({ runner: 'jest-runner', setupFiles: [], setupFilesAfterEnv: [], - setupTestFrameworkScriptFile: null, skipFilter: false, snapshotSerializers: [], testEnvironment: 'jest-environment-jsdom', diff --git a/packages/jest-config/src/Deprecated.js b/packages/jest-config/src/Deprecated.js index 907cb2705924..1b21359bba8e 100644 --- a/packages/jest-config/src/Deprecated.js +++ b/packages/jest-config/src/Deprecated.js @@ -53,6 +53,16 @@ export default { Please update your configuration.`, + setupTestFrameworkScriptFile: (options: { + setupTestFrameworkScriptFile: Array, + }) => ` Option ${chalk.bold( + '"setupTestFrameworkScriptFile"', + )} was replaced by configuration ${chalk.bold( + '"setupFilesAfterEnv"', + )}, which supports multiple paths. + + Please update your configuration.`, + testPathDirs: (options: { testPathDirs: Array, }) => ` Option ${chalk.bold('"testPathDirs"')} was replaced by ${chalk.bold( diff --git a/packages/jest-config/src/Descriptions.js b/packages/jest-config/src/Descriptions.js index 88c3a6418a83..a7bd4b768fd4 100644 --- a/packages/jest-config/src/Descriptions.js +++ b/packages/jest-config/src/Descriptions.js @@ -63,8 +63,6 @@ export default ({ 'The paths to modules that run some code to configure or set up the testing environment before each test', setupFilesAfterEnv: 'A list of paths to modules that run some code to configure or set up the testing framework before each test', - setupTestFrameworkScriptFile: - 'The path to a module that runs some code to configure or set up the testing framework before each test', snapshotSerializers: 'A list of paths to snapshot serializer modules Jest should use for snapshot testing', testEnvironment: 'The test environment that will be used for testing', diff --git a/packages/jest-config/src/ValidConfig.js b/packages/jest-config/src/ValidConfig.js index e84a9eab5359..2c597127e862 100644 --- a/packages/jest-config/src/ValidConfig.js +++ b/packages/jest-config/src/ValidConfig.js @@ -88,7 +88,6 @@ export default ({ runner: 'jest-runner', setupFiles: ['/setup.js'], setupFilesAfterEnv: ['/testSetupFile.js'], - setupTestFrameworkScriptFile: '/testSetupFile.js', silent: true, skipFilter: false, skipNodeResolution: false, diff --git a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap index 19770435b020..14867435f30a 100644 --- a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap +++ b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap @@ -47,6 +47,29 @@ exports[`rootDir throws if the options is missing a rootDir property 1`] = ` " `; +exports[`setupTestFrameworkScriptFile logs a deprecation warning and error when \`setupTestFrameworkScriptFile\` is used 1`] = ` +"Validation Error: + + Options: setupTestFrameworkScriptFile and setupFilesAfterEnv cannot be used together. + Please change your configuration to only use setupFilesAfterEnv. + + Configuration Documentation: + https://jestjs.io/docs/configuration.html +" +`; + +exports[`setupTestFrameworkScriptFile logs a deprecation warning and error when \`setupTestFrameworkScriptFile\` is used 2`] = ` +" Deprecation Warning: + + Option \\"setupTestFrameworkScriptFile\\" was replaced by configuration \\"setupFilesAfterEnv\\", which supports multiple paths. + + Please update your configuration. + + Configuration Documentation: + https://jestjs.io/docs/configuration.html +" +`; + exports[`testEnvironment throws on invalid environment names 1`] = ` "Validation Error: diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index b0fca75fb5e1..2a329a22d3d1 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -379,7 +379,11 @@ describe('setupFilesAfterEnv', () => { describe('setupTestFrameworkScriptFile', () => { let Resolver; + let consoleWarn; + beforeEach(() => { + console.warn = jest.fn(); + consoleWarn = console.warn; Resolver = require('jest-resolve'); Resolver.findNodeModule = jest.fn( name => @@ -387,40 +391,22 @@ describe('setupTestFrameworkScriptFile', () => { ); }); - it('normalizes the path according to rootDir', () => { - const {options} = normalize( - { - rootDir: '/root/path/foo', - setupTestFrameworkScriptFile: 'bar/baz', - }, - {}, - ); - - expect(options.setupTestFrameworkScriptFile).toEqual(expectedPathFooBar); - }); - - it('does not change absolute paths', () => { - const {options} = normalize( - { - rootDir: '/root/path/foo', - setupTestFrameworkScriptFile: '/an/abs/path', - }, - {}, - ); - - expect(options.setupTestFrameworkScriptFile).toEqual(expectedPathAbs); + afterEach(() => { + console.warn = consoleWarn; }); - it('substitutes tokens', () => { - const {options} = normalize( - { - rootDir: '/root/path/foo', - setupTestFrameworkScriptFile: '/bar/baz', - }, - {}, - ); + it('logs a deprecation warning and error when `setupTestFrameworkScriptFile` is used', () => { + expect(() => + normalize( + { + rootDir: '/root/path/foo', + setupTestFrameworkScriptFile: 'bar/baz', + }, + {}, + ), + ).toThrowErrorMatchingSnapshot(); - expect(options.setupTestFrameworkScriptFile).toEqual(expectedPathFooBar); + expect(consoleWarn.mock.calls[0][0]).toMatchSnapshot(); }); }); diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js index 0c70ee3f1808..ec6a856c75f0 100644 --- a/packages/jest-config/src/index.js +++ b/packages/jest-config/src/index.js @@ -185,7 +185,6 @@ const groupOptions = ( runner: options.runner, setupFiles: options.setupFiles, setupFilesAfterEnv: options.setupFilesAfterEnv, - setupTestFrameworkScriptFile: options.setupTestFrameworkScriptFile, skipFilter: options.skipFilter, skipNodeResolution: options.skipNodeResolution, snapshotResolver: options.snapshotResolver, diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index 7d4c5d2d4052..ea3a64a30606 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -110,10 +110,6 @@ const setupPreset = ( options.setupFiles = (preset.setupFiles || []).concat(options.setupFiles); } - if (!options.setupFilesAfterEnv) { - options.setupFilesAfterEnv = []; - } - if (options.modulePathIgnorePatterns && preset.modulePathIgnorePatterns) { options.modulePathIgnorePatterns = preset.modulePathIgnorePatterns.concat( options.modulePathIgnorePatterns, @@ -258,6 +254,10 @@ const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { options.setupFiles = []; } + if (!options.setupFilesAfterEnv) { + options.setupFilesAfterEnv = []; + } + return options; }; @@ -371,6 +371,17 @@ export default function normalize(options: InitialOptions, argv: Argv) { ), ); + if (options.setupTestFrameworkScriptFile && options.setupFilesAfterEnv) { + throw createConfigError( + ` Options: ${chalk.bold( + 'setupTestFrameworkScriptFile', + )} and ${chalk.bold('setupFilesAfterEnv')} cannot be used together. + Please change your configuration to only use ${chalk.bold( + 'setupFilesAfterEnv', + )}.`, + ); + } + if (options.preset) { options = setupPreset(options, options.preset); } @@ -458,7 +469,6 @@ export default function normalize(options: InitialOptions, argv: Argv) { case 'globalTeardown': case 'moduleLoader': case 'runner': - case 'setupTestFrameworkScriptFile': case 'snapshotResolver': case 'testResultsProcessor': case 'testRunner': diff --git a/packages/jest-jasmine2/src/index.js b/packages/jest-jasmine2/src/index.js index 277b6601f231..ab015dc65cba 100644 --- a/packages/jest-jasmine2/src/index.js +++ b/packages/jest-jasmine2/src/index.js @@ -127,10 +127,6 @@ async function jasmine2( testPath, }); - if (config.setupTestFrameworkScriptFile) { - runtime.requireModule(config.setupTestFrameworkScriptFile); - } - if (config.setupFilesAfterEnv.length) { for (let i = 0; i < config.setupFilesAfterEnv.length; i++) { runtime.requireModule(config.setupFilesAfterEnv[i]); diff --git a/packages/jest-validate/src/__tests__/fixtures/jest_config.js b/packages/jest-validate/src/__tests__/fixtures/jest_config.js index 549b5548d665..31a04cedb0d2 100644 --- a/packages/jest-validate/src/__tests__/fixtures/jest_config.js +++ b/packages/jest-validate/src/__tests__/fixtures/jest_config.js @@ -108,7 +108,6 @@ const validConfig = { roots: [''], setupFiles: ['/setup.js'], setupFilesAfterEnv: ['/testSetupFile.js'], - setupTestFrameworkScriptFile: '/testSetupFile.js', silent: true, snapshotSerializers: ['my-serializer-module'], testEnvironment: 'jest-environment-jsdom', diff --git a/types/Argv.js b/types/Argv.js index db34ebac2437..7339b5594882 100644 --- a/types/Argv.js +++ b/types/Argv.js @@ -71,7 +71,6 @@ export type Argv = {| roots: Array, runInBand: boolean, setupFiles: Array, - setupTestFrameworkScriptFile: string, setupFilesAfterEnv: Array, showConfig: boolean, silent: boolean, diff --git a/types/Config.js b/types/Config.js index 5cfde84ee647..2a5b79d4c2f3 100644 --- a/types/Config.js +++ b/types/Config.js @@ -65,7 +65,6 @@ export type DefaultOptions = {| runner: string, runTestsByPath: boolean, setupFiles: Array, - setupTestFrameworkScriptFile: ?Path, setupFilesAfterEnv: Array, skipFilter: boolean, snapshotSerializers: Array, @@ -274,7 +273,6 @@ export type ProjectConfig = {| roots: Array, runner: string, setupFiles: Array, - setupTestFrameworkScriptFile: ?Path, setupFilesAfterEnv: Array, skipFilter: boolean, skipNodeResolution: boolean, From ebb14c1f39421ee656e92886d03576be7cbd346d Mon Sep 17 00:00:00 2001 From: David Druker Date: Sat, 20 Oct 2018 13:26:53 -0400 Subject: [PATCH 11/15] remove empty line --- packages/jest-config/src/normalize.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index f7c0cc9ae7a8..bdb107cab2d6 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -110,7 +110,6 @@ const setupPreset = ( if (options.setupFiles) { options.setupFiles = (preset.setupFiles || []).concat(options.setupFiles); } - if (options.modulePathIgnorePatterns && preset.modulePathIgnorePatterns) { options.modulePathIgnorePatterns = preset.modulePathIgnorePatterns.concat( options.modulePathIgnorePatterns, From db2ce7ef4950e13ef25b07d1d862f34a2408aee3 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sat, 20 Oct 2018 15:14:29 -0400 Subject: [PATCH 12/15] updates per pr review --- CHANGELOG.md | 2 +- docs/Configuration.md | 4 +++- .../legacy_code_todo_rewrite/jest_adapter.js | 6 +---- .../__snapshots__/normalize.test.js.snap | 24 +++++++++---------- .../src/__tests__/normalize.test.js | 17 ++++++++++--- packages/jest-config/src/normalize.js | 17 +++++++++---- packages/jest-jasmine2/src/index.js | 6 +---- 7 files changed, 44 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbf5d5d3ec59..3d2ee3860dfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Features -- `[jest-jasmine2/jest-circus/jest-cli]` Setup before tests but after framework loads ([#7119](https://github.com/facebook/jest/pull/7119)) +- `[jest-jasmine2/jest-circus/jest-cli]` [**BREAKING**] Setup before tests but after framework loads ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-jasmine2/jest-circus/jest-cli]` Add test.todo ([#6996](https://github.com/facebook/jest/pull/6996)) - `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661)) - `[jest-haste-map]` Add `getFileIterator` to `HasteFS` for faster file iteration ([#7010](https://github.com/facebook/jest/pull/7010)). diff --git a/docs/Configuration.md b/docs/Configuration.md index 8a90e6c29d76..786e8ef673e6 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -636,10 +636,12 @@ Default: `[]` Multiple paths to modules that run some code to configure or set up the testing framework before each test. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment. -If you want this path to be [relative to the root directory of your project](#rootdir-string), please include `` inside a paths string, like `"/a-configs-folder"`. +If you want a path to be [relative to the root directory of your project](#rootdir-string), please include `` inside a path's string, like `"/a-configs-folder"`. For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules. +It's also worth noting that [`setupFiles`](#setupfiles-array) will execute _before_ [`setupFilesAfterEnv`](#setupFilesAfterEnv-array). + ### `snapshotResolver` [string] Default: `undefined` diff --git a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js index 60e77ca26259..bf2d87b9f4bd 100644 --- a/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js +++ b/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js @@ -73,11 +73,7 @@ const jestAdapter = async ( } }); - if (config.setupFilesAfterEnv.length) { - for (let i = 0; i < config.setupFilesAfterEnv.length; i++) { - runtime.requireModule(config.setupFilesAfterEnv[i]); - } - } + config.setupFilesAfterEnv.forEach(path => runtime.requireModule(path)); runtime.requireModule(testPath); const results = await runAndTransformResultsToJestFormat({ diff --git a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap index 8ef183493e1e..9eb36c229fa6 100644 --- a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap +++ b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap @@ -47,18 +47,7 @@ exports[`rootDir throws if the options is missing a rootDir property 1`] = ` " `; -exports[`setupTestFrameworkScriptFile logs a deprecation warning and error when \`setupTestFrameworkScriptFile\` is used 1`] = ` -"Validation Error: - - Options: setupTestFrameworkScriptFile and setupFilesAfterEnv cannot be used together. - Please change your configuration to only use setupFilesAfterEnv. - - Configuration Documentation: - https://jestjs.io/docs/configuration.html -" -`; - -exports[`setupTestFrameworkScriptFile logs a deprecation warning and error when \`setupTestFrameworkScriptFile\` is used 2`] = ` +exports[`setupTestFrameworkScriptFile logs a deprecation warning when \`setupTestFrameworkScriptFile\` is used 1`] = ` " Deprecation Warning: Option \\"setupTestFrameworkScriptFile\\" was replaced by configuration \\"setupFilesAfterEnv\\", which supports multiple paths. @@ -70,6 +59,17 @@ exports[`setupTestFrameworkScriptFile logs a deprecation warning and error when " `; +exports[`setupTestFrameworkScriptFile logs an error when \`setupTestFrameworkScriptFile\` and \`setupFilesAfterEnv\` are used 1`] = ` +"Validation Error: + + Options: setupTestFrameworkScriptFile and setupFilesAfterEnv cannot be used together. + Please change your configuration to only use setupFilesAfterEnv. + + Configuration Documentation: + https://jestjs.io/docs/configuration.html +" +`; + exports[`testEnvironment throws on invalid environment names 1`] = ` "Validation Error: diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 0baf81811218..f890d1500786 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -395,18 +395,29 @@ describe('setupTestFrameworkScriptFile', () => { console.warn = consoleWarn; }); - it('logs a deprecation warning and error when `setupTestFrameworkScriptFile` is used', () => { + it('logs a deprecation warning when `setupTestFrameworkScriptFile` is used', () => { + normalize( + { + rootDir: '/root/path/foo', + setupTestFrameworkScriptFile: 'bar/baz', + }, + {}, + ); + + expect(consoleWarn.mock.calls[0][0]).toMatchSnapshot(); + }); + + it('logs an error when `setupTestFrameworkScriptFile` and `setupFilesAfterEnv` are used', () => { expect(() => normalize( { rootDir: '/root/path/foo', + setupFilesAfterEnv: ['bar/baz'], setupTestFrameworkScriptFile: 'bar/baz', }, {}, ), ).toThrowErrorMatchingSnapshot(); - - expect(consoleWarn.mock.calls[0][0]).toMatchSnapshot(); }); }); diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index bdb107cab2d6..f5c7db067bf3 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -254,10 +254,6 @@ const normalizeMissingOptions = (options: InitialOptions): InitialOptions => { options.setupFiles = []; } - if (!options.setupFilesAfterEnv) { - options.setupFilesAfterEnv = []; - } - return options; }; @@ -371,7 +367,14 @@ export default function normalize(options: InitialOptions, argv: Argv) { ), ); - if (options.setupTestFrameworkScriptFile && options.setupFilesAfterEnv) { + if (!options.setupFilesAfterEnv) { + options.setupFilesAfterEnv = []; + } + + if ( + options.setupTestFrameworkScriptFile && + options.setupFilesAfterEnv.length >= 1 + ) { throw createConfigError( ` Options: ${chalk.bold( 'setupTestFrameworkScriptFile', @@ -382,6 +385,10 @@ export default function normalize(options: InitialOptions, argv: Argv) { ); } + if (options.setupTestFrameworkScriptFile) { + options.setupFilesAfterEnv.push(options.setupTestFrameworkScriptFile); + } + if (options.preset) { options = setupPreset(options, options.preset); } diff --git a/packages/jest-jasmine2/src/index.js b/packages/jest-jasmine2/src/index.js index 5d67e112f034..b6fef61428b1 100644 --- a/packages/jest-jasmine2/src/index.js +++ b/packages/jest-jasmine2/src/index.js @@ -147,11 +147,7 @@ async function jasmine2( testPath, }); - if (config.setupFilesAfterEnv.length) { - for (let i = 0; i < config.setupFilesAfterEnv.length; i++) { - runtime.requireModule(config.setupFilesAfterEnv[i]); - } - } + config.setupFilesAfterEnv.forEach(path => runtime.requireModule(path)); if (globalConfig.enabledTestsMap) { env.specFilter = spec => { From 7b322df3a39786b7d925bd1d90ed2c4dde9a814d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 20 Oct 2018 21:16:27 +0200 Subject: [PATCH 13/15] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d2ee3860dfe..317eadc68518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Features -- `[jest-jasmine2/jest-circus/jest-cli]` [**BREAKING**] Setup before tests but after framework loads ([#7119](https://github.com/facebook/jest/pull/7119)) +- `[jest-config]` [**BREAKING**] Setup before tests but after framework loads ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-jasmine2/jest-circus/jest-cli]` Add test.todo ([#6996](https://github.com/facebook/jest/pull/6996)) - `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661)) - `[jest-haste-map]` Add `getFileIterator` to `HasteFS` for faster file iteration ([#7010](https://github.com/facebook/jest/pull/7010)). From 6ba2beb7dfe3128c725a4cce0185835b09901b25 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sat, 20 Oct 2018 15:22:47 -0400 Subject: [PATCH 14/15] updates per pr review --- docs/Configuration.md | 4 ++-- packages/jest-config/src/normalize.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 786e8ef673e6..a4c1e1ad3249 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -630,6 +630,8 @@ Default: `[]` The paths to modules that run some code to configure or set up the testing environment. Each setupFile will be run once per test file. Since every test runs in its own environment, these scripts will be executed in the testing environment immediately before executing the test code itself. +It's also worth noting that `setupFiles` will execute _before_ [`setupFilesAfterEnv`](#setupFilesAfterEnv-array). + ### `setupFilesAfterEnv` [array] Default: `[]` @@ -640,8 +642,6 @@ If you want a path to be [relative to the root directory of your project](#rootd For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules. -It's also worth noting that [`setupFiles`](#setupfiles-array) will execute _before_ [`setupFilesAfterEnv`](#setupFilesAfterEnv-array). - ### `snapshotResolver` [string] Default: `undefined` diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index f5c7db067bf3..97e568153cc3 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -373,7 +373,7 @@ export default function normalize(options: InitialOptions, argv: Argv) { if ( options.setupTestFrameworkScriptFile && - options.setupFilesAfterEnv.length >= 1 + options.setupFilesAfterEnv.length > 0 ) { throw createConfigError( ` Options: ${chalk.bold( From cd8e2ae214e1a68faf59a080e6909e13dc492068 Mon Sep 17 00:00:00 2001 From: David Druker Date: Sun, 21 Oct 2018 09:44:03 -0400 Subject: [PATCH 15/15] copy changes --- CHANGELOG.md | 2 +- docs/Configuration.md | 6 ++++-- packages/jest-cli/src/cli/args.js | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 317eadc68518..45dabec0321a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Features -- `[jest-config]` [**BREAKING**] Setup before tests but after framework loads ([#7119](https://github.com/facebook/jest/pull/7119)) +- `[jest-config]` [**BREAKING**] Deprecate `setupTestFrameworkScriptFile` in favor of new `setupFilesAfterEnv` ([#7119](https://github.com/facebook/jest/pull/7119)) - `[jest-jasmine2/jest-circus/jest-cli]` Add test.todo ([#6996](https://github.com/facebook/jest/pull/6996)) - `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661)) - `[jest-haste-map]` Add `getFileIterator` to `HasteFS` for faster file iteration ([#7010](https://github.com/facebook/jest/pull/7010)). diff --git a/docs/Configuration.md b/docs/Configuration.md index a4c1e1ad3249..f98882ac147d 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -628,7 +628,7 @@ If you need to restrict your test-runner to only run in serial rather then being Default: `[]` -The paths to modules that run some code to configure or set up the testing environment. Each setupFile will be run once per test file. Since every test runs in its own environment, these scripts will be executed in the testing environment immediately before executing the test code itself. +A list of paths to modules that run some code to configure or set up the testing environment. Each setupFile will be run once per test file. Since every test runs in its own environment, these scripts will be executed in the testing environment immediately before executing the test code itself. It's also worth noting that `setupFiles` will execute _before_ [`setupFilesAfterEnv`](#setupFilesAfterEnv-array). @@ -636,12 +636,14 @@ It's also worth noting that `setupFiles` will execute _before_ [`setupFilesAfter Default: `[]` -Multiple paths to modules that run some code to configure or set up the testing framework before each test. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment. +A list of paths to modules that run some code to configure or set up the testing framework before each test. Since [`setupFiles`](#setupfiles-array) executes before the test framework is installed in the environment, this script file presents you the opportunity of running some code immediately after the test framework has been installed in the environment. If you want a path to be [relative to the root directory of your project](#rootdir-string), please include `` inside a path's string, like `"/a-configs-folder"`. For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in these modules. +_Note: `setupTestFrameworkScriptFile` is deprecated in favor of `setupFilesAfterEnv`._ + ### `snapshotResolver` [string] Default: `undefined` diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index 911b54235e9a..99e92b21f959 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -501,7 +501,7 @@ export const options = { }, setupFiles: { description: - 'The paths to modules that run some code to configure or ' + + 'A list of paths to modules that run some code to configure or ' + 'set up the testing environment before each test. ', type: 'array', },