From b260bb8dd0bbbd8573bc0acd53e4f1f632db26e7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 29 Dec 2018 20:42:49 +0100 Subject: [PATCH 1/6] Run code transforms over `global{Setup,Teardown}` --- CHANGELOG.md | 1 + e2e/global-setup/babel.config.js | 5 ++ e2e/global-setup/setup.js | 3 +- packages/jest-cli/package.json | 2 + packages/jest-cli/src/runGlobalHook.js | 75 +++++++++++++++++++------- yarn.lock | 7 +++ 6 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 e2e/global-setup/babel.config.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 02fac518934b..87439b835f98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ - `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494)) - `[jest-runner]` Instantiate the test environment class with the current `testPath` ([#7442](https://github.com/facebook/jest/pull/7442)) - `[jest-config]` Always resolve jest-environment-jsdom from jest-config ([#7476](https://github.com/facebook/jest/pull/7476)) +- `[jest-cli]` Run code transforms over `global{Setup,Teardown}` ([#7562](https://github.com/facebook/jest/pull/7562)) ### Fixes diff --git a/e2e/global-setup/babel.config.js b/e2e/global-setup/babel.config.js new file mode 100644 index 000000000000..dcda2fba8b21 --- /dev/null +++ b/e2e/global-setup/babel.config.js @@ -0,0 +1,5 @@ +// Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + +module.exports = { + presets: ['@babel/preset-flow'], +}; diff --git a/e2e/global-setup/setup.js b/e2e/global-setup/setup.js index 94c420cd1be3..39e924c39daf 100644 --- a/e2e/global-setup/setup.js +++ b/e2e/global-setup/setup.js @@ -13,7 +13,8 @@ const path = require('path'); const DIR = path.join(os.tmpdir(), 'jest-global-setup'); module.exports = function() { - return new Promise((resolve, reject) => { + // This uses a flow annotation to show it can be transpiled + return new Promise((resolve, reject: any) => { createDirectory(DIR); const fileId = crypto.randomBytes(20).toString('hex'); fs.writeFileSync(path.join(DIR, fileId), 'setup'); diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index bfb7d9f3f18e..de39472c5048 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -32,6 +32,8 @@ "jest-worker": "^23.2.0", "micromatch": "^2.3.11", "node-notifier": "^5.2.1", + "p-each-series": "^1.0.0", + "pirates": "^4.0.0", "prompts": "^2.0.1", "realpath-native": "^1.0.0", "rimraf": "^2.5.4", diff --git a/packages/jest-cli/src/runGlobalHook.js b/packages/jest-cli/src/runGlobalHook.js index 844b39729726..0ce2767e043f 100644 --- a/packages/jest-cli/src/runGlobalHook.js +++ b/packages/jest-cli/src/runGlobalHook.js @@ -6,9 +6,22 @@ * * @flow */ + import type {GlobalConfig} from 'types/Config'; import type {Test} from 'types/TestRunner'; +import {extname, resolve, sep} from 'path'; +import pEachSeries from 'p-each-series'; +import {addHook} from 'pirates'; +import {ScriptTransformer} from 'jest-runtime'; + +const inJestsSource = __dirname.includes(`packages${sep}jest-cli`); +let packagesRoot; + +if (inJestsSource) { + packagesRoot = resolve(__dirname, '../../'); +} + export default ({ allTests, globalConfig, @@ -17,7 +30,7 @@ export default ({ allTests: Array, globalConfig: GlobalConfig, moduleName: 'globalSetup' | 'globalTeardown', -}): Promise => { +}): Promise => { const globalModulePaths = new Set( allTests.map(test => test.context.config[moduleName]), ); @@ -27,24 +40,48 @@ export default ({ } if (globalModulePaths.size > 0) { - return 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); - }), - ); + return pEachSeries(Array.from(globalModulePaths), async modulePath => { + if (!modulePath) { + return; + } + + const projectConfig = + allTests + .map(t => t.context.config) + .find(c => c[moduleName] === modulePath) || + // Fallback to first one + allTests[0].context.config; + + const transformer = new ScriptTransformer(projectConfig); + + const revertHook = addHook( + (code, filename) => + transformer.transformSource(filename, code, false).code || code, + { + exts: [extname(modulePath)], + matcher(filename) { + // `babel-jest` etc would normally be caught by `node_modules`, but not in Jest's own repo + if (inJestsSource && filename.includes(packagesRoot)) { + return false; + } + return transformer._shouldTransform(filename); + }, + }, + ); + + // $FlowFixMe + const globalModule = require(modulePath); + + if (typeof globalModule !== 'function') { + throw new TypeError( + `${moduleName} file must export a function at ${modulePath}`, + ); + } + + await globalModule(globalConfig); + + revertHook(); + }); } return Promise.resolve(); diff --git a/yarn.lock b/yarn.lock index 2ba5ee659775..ee6616cdedca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9557,6 +9557,13 @@ p-defer@^1.0.0: resolved "https://registry.yarnpkg.com/p-defer/-/p-defer-1.0.0.tgz#9f6eb182f6c9aa8cd743004a7d4f96b196b0fb0c" integrity sha1-n26xgvbJqozXQwBKfU+WsZaw+ww= +p-each-series@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/p-each-series/-/p-each-series-1.0.0.tgz#930f3d12dd1f50e7434457a22cd6f04ac6ad7f71" + integrity sha1-kw89Et0fUOdDRFeiLNbwSsatf3E= + dependencies: + p-reduce "^1.0.0" + p-finally@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae" From 09ef708fe8e5164897bd693f869c9f2be96bb245 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 31 Dec 2018 17:03:48 +0100 Subject: [PATCH 2/6] PR feedback --- CHANGELOG.md | 2 +- e2e/global-setup/package.json | 6 +++++- e2e/global-setup/projects.jest.config.js | 2 ++ e2e/global-teardown/package.json | 6 +++++- e2e/global-teardown/projects.jest.config.js | 2 ++ packages/jest-cli/src/runGlobalHook.js | 17 ++--------------- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87439b835f98..07746d020b08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - `[jest-haste-map]` [**BREAKING**] Remove name from hash in `HasteMap.getCacheFilePath` ([#7218](https://github.com/facebook/jest/pull/7218)) - `[babel-preset-jest]` [**BREAKING**] Export a function instead of an object for Babel 7 compatibility ([#7203](https://github.com/facebook/jest/pull/7203)) - `[jest-haste-map]` [**BREAKING**] Expose relative paths when getting the file iterator ([#7321](https://github.com/facebook/jest/pull/7321)) +- `[jest-cli]` [**BREAKING**] Run code transforms over `global{Setup,Teardown}` ([#7562](https://github.com/facebook/jest/pull/7562)) - `[jest-haste-map]` Add `hasteFS.getSize(path)` ([#7580](https://github.com/facebook/jest/pull/7580)) - `[jest-cli]` Print version ending in `-dev` when running a local Jest clone ([#7582](https://github.com/facebook/jest/pull/7582)) - `[jest-cli]` Add Support for `globalSetup` and `globalTeardown` in projects ([#6865](https://github.com/facebook/jest/pull/6865)) @@ -49,7 +50,6 @@ - `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494)) - `[jest-runner]` Instantiate the test environment class with the current `testPath` ([#7442](https://github.com/facebook/jest/pull/7442)) - `[jest-config]` Always resolve jest-environment-jsdom from jest-config ([#7476](https://github.com/facebook/jest/pull/7476)) -- `[jest-cli]` Run code transforms over `global{Setup,Teardown}` ([#7562](https://github.com/facebook/jest/pull/7562)) ### Fixes diff --git a/e2e/global-setup/package.json b/e2e/global-setup/package.json index 148788b25446..3dc54e8651bf 100644 --- a/e2e/global-setup/package.json +++ b/e2e/global-setup/package.json @@ -1,5 +1,9 @@ { "jest": { - "testEnvironment": "node" + "testEnvironment": "node", + "transformIgnorePatterns": [ + "/node_modules/", + "/packages/" + ] } } diff --git a/e2e/global-setup/projects.jest.config.js b/e2e/global-setup/projects.jest.config.js index 46ecc5a58c22..9d008ac5cd06 100644 --- a/e2e/global-setup/projects.jest.config.js +++ b/e2e/global-setup/projects.jest.config.js @@ -15,12 +15,14 @@ module.exports = { globalSetup: '/setup.js', rootDir: path.resolve(__dirname, './project-1'), testMatch: ['/**/*.test.js'], + transformIgnorePatterns: ['/node_modules/', '/packages/'], }, { displayName: 'project-2', globalSetup: '/setup.js', rootDir: path.resolve(__dirname, './project-2'), testMatch: ['/**/*.test.js'], + transformIgnorePatterns: ['/node_modules/', '/packages/'], }, ], }; diff --git a/e2e/global-teardown/package.json b/e2e/global-teardown/package.json index 148788b25446..3dc54e8651bf 100644 --- a/e2e/global-teardown/package.json +++ b/e2e/global-teardown/package.json @@ -1,5 +1,9 @@ { "jest": { - "testEnvironment": "node" + "testEnvironment": "node", + "transformIgnorePatterns": [ + "/node_modules/", + "/packages/" + ] } } diff --git a/e2e/global-teardown/projects.jest.config.js b/e2e/global-teardown/projects.jest.config.js index 9f2c416027ab..55baa56ebe15 100644 --- a/e2e/global-teardown/projects.jest.config.js +++ b/e2e/global-teardown/projects.jest.config.js @@ -15,12 +15,14 @@ module.exports = { globalTeardown: '/teardown.js', rootDir: path.resolve(__dirname, './project-1'), testMatch: ['/**/*.test.js'], + transformIgnorePatterns: ['/node_modules/', '/packages/'], }, { displayName: 'project-2', globalTeardown: '/teardown.js', rootDir: path.resolve(__dirname, './project-2'), testMatch: ['/**/*.test.js'], + transformIgnorePatterns: ['/node_modules/', '/packages/'], }, ], }; diff --git a/packages/jest-cli/src/runGlobalHook.js b/packages/jest-cli/src/runGlobalHook.js index 0ce2767e043f..8a8abe8aec97 100644 --- a/packages/jest-cli/src/runGlobalHook.js +++ b/packages/jest-cli/src/runGlobalHook.js @@ -10,18 +10,11 @@ import type {GlobalConfig} from 'types/Config'; import type {Test} from 'types/TestRunner'; -import {extname, resolve, sep} from 'path'; +import {extname} from 'path'; import pEachSeries from 'p-each-series'; import {addHook} from 'pirates'; import {ScriptTransformer} from 'jest-runtime'; -const inJestsSource = __dirname.includes(`packages${sep}jest-cli`); -let packagesRoot; - -if (inJestsSource) { - packagesRoot = resolve(__dirname, '../../'); -} - export default ({ allTests, globalConfig, @@ -59,13 +52,7 @@ export default ({ transformer.transformSource(filename, code, false).code || code, { exts: [extname(modulePath)], - matcher(filename) { - // `babel-jest` etc would normally be caught by `node_modules`, but not in Jest's own repo - if (inJestsSource && filename.includes(packagesRoot)) { - return false; - } - return transformer._shouldTransform(filename); - }, + matcher: transformer._shouldTransform.bind(transformer), }, ); From 3169d00614a688a37eb541de2620136841a62e08 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 18 Jan 2019 13:55:42 +0100 Subject: [PATCH 3/6] fix failing test --- e2e/custom-resolver/package.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/e2e/custom-resolver/package.json b/e2e/custom-resolver/package.json index dd319619f468..30c098aeff67 100644 --- a/e2e/custom-resolver/package.json +++ b/e2e/custom-resolver/package.json @@ -2,6 +2,10 @@ "name": "custom-resolver", "jest": { "globalSetup": "foo", - "resolver": "./resolver.js" + "resolver": "./resolver.js", + "transformIgnorePatterns": [ + "/node_modules/", + "/packages/" + ] } } From 86294581c6215acee675ceda99d2a59fe478a452 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 18 Jan 2019 13:57:55 +0100 Subject: [PATCH 4/6] remove double loop --- packages/jest-cli/src/runGlobalHook.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/jest-cli/src/runGlobalHook.js b/packages/jest-cli/src/runGlobalHook.js index 8a8abe8aec97..155cbf55e24f 100644 --- a/packages/jest-cli/src/runGlobalHook.js +++ b/packages/jest-cli/src/runGlobalHook.js @@ -38,12 +38,14 @@ export default ({ return; } - const projectConfig = - allTests - .map(t => t.context.config) - .find(c => c[moduleName] === modulePath) || - // Fallback to first one - allTests[0].context.config; + const correctConfig = allTests.find( + t => t.context.config[moduleName] === modulePath, + ); + + const projectConfig = correctConfig + ? correctConfig.context.config + : // Fallback to first config + allTests[0].context.config; const transformer = new ScriptTransformer(projectConfig); From 650a23e756665bc5858b00332489d7bd3362a06c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 18 Jan 2019 14:02:40 +0100 Subject: [PATCH 5/6] fix import after ESM revert --- packages/jest-cli/src/runGlobalHook.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-cli/src/runGlobalHook.js b/packages/jest-cli/src/runGlobalHook.js index 155cbf55e24f..cd5a4ff06bad 100644 --- a/packages/jest-cli/src/runGlobalHook.js +++ b/packages/jest-cli/src/runGlobalHook.js @@ -13,7 +13,7 @@ import type {Test} from 'types/TestRunner'; import {extname} from 'path'; import pEachSeries from 'p-each-series'; import {addHook} from 'pirates'; -import {ScriptTransformer} from 'jest-runtime'; +import Runtime from 'jest-runtime'; export default ({ allTests, @@ -47,7 +47,7 @@ export default ({ : // Fallback to first config allTests[0].context.config; - const transformer = new ScriptTransformer(projectConfig); + const transformer = new Runtime.ScriptTransformer(projectConfig); const revertHook = addHook( (code, filename) => From f1f85914671798835ddce7bb47b8950f09615cb6 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 18 Jan 2019 14:47:02 +0100 Subject: [PATCH 6/6] chore: fix copyright text --- e2e/global-setup/babel.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/global-setup/babel.config.js b/e2e/global-setup/babel.config.js index dcda2fba8b21..245aa3fd79db 100644 --- a/e2e/global-setup/babel.config.js +++ b/e2e/global-setup/babel.config.js @@ -1,4 +1,4 @@ -// Copyright (c) 2014-present, Facebook, Inc. All rights reserved. +// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. module.exports = { presets: ['@babel/preset-flow'],