Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run code transforms over global{Setup,Teardown} #7562

Merged
merged 6 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion e2e/custom-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"name": "custom-resolver",
"jest": {
"globalSetup": "foo",
"resolver": "./resolver.js"
"resolver": "./resolver.js",
"transformIgnorePatterns": [
"/node_modules/",
"/packages/"
]
}
}
5 changes: 5 additions & 0 deletions e2e/global-setup/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.

module.exports = {
presets: ['@babel/preset-flow'],
};
6 changes: 5 additions & 1 deletion e2e/global-setup/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"transformIgnorePatterns": [
"/node_modules/",
"/packages/"
]
}
}
2 changes: 2 additions & 0 deletions e2e/global-setup/projects.jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ module.exports = {
globalSetup: '<rootDir>/setup.js',
rootDir: path.resolve(__dirname, './project-1'),
testMatch: ['<rootDir>/**/*.test.js'],
transformIgnorePatterns: ['/node_modules/', '/packages/'],
},
{
displayName: 'project-2',
globalSetup: '<rootDir>/setup.js',
rootDir: path.resolve(__dirname, './project-2'),
testMatch: ['<rootDir>/**/*.test.js'],
transformIgnorePatterns: ['/node_modules/', '/packages/'],
},
],
};
3 changes: 2 additions & 1 deletion e2e/global-setup/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
6 changes: 5 additions & 1 deletion e2e/global-teardown/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"transformIgnorePatterns": [
"/node_modules/",
"/packages/"
]
}
}
2 changes: 2 additions & 0 deletions e2e/global-teardown/projects.jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ module.exports = {
globalTeardown: '<rootDir>/teardown.js',
rootDir: path.resolve(__dirname, './project-1'),
testMatch: ['<rootDir>/**/*.test.js'],
transformIgnorePatterns: ['/node_modules/', '/packages/'],
},
{
displayName: 'project-2',
globalTeardown: '<rootDir>/teardown.js',
rootDir: path.resolve(__dirname, './project-2'),
testMatch: ['<rootDir>/**/*.test.js'],
transformIgnorePatterns: ['/node_modules/', '/packages/'],
},
],
};
2 changes: 2 additions & 0 deletions packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
64 changes: 45 additions & 19 deletions packages/jest-cli/src/runGlobalHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
*
* @flow
*/

import type {GlobalConfig} from 'types/Config';
import type {Test} from 'types/TestRunner';

import {extname} from 'path';
import pEachSeries from 'p-each-series';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using p-each-series allows us to make sure one require hook does not interfere with another

import {addHook} from 'pirates';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pirates is what babel-register uses

import Runtime from 'jest-runtime';

export default ({
allTests,
globalConfig,
Expand All @@ -17,7 +23,7 @@ export default ({
allTests: Array<Test>,
globalConfig: GlobalConfig,
moduleName: 'globalSetup' | 'globalTeardown',
}): Promise<?(any[])> => {
}): Promise<void> => {
const globalModulePaths = new Set(
allTests.map(test => test.context.config[moduleName]),
);
Expand All @@ -27,24 +33,44 @@ 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 => {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
if (!modulePath) {
return;
}

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 Runtime.ScriptTransformer(projectConfig);

const revertHook = addHook(
(code, filename) =>
transformer.transformSource(filename, code, false).code || code,
{
exts: [extname(modulePath)],
matcher: transformer._shouldTransform.bind(transformer),
},
);

// $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();
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down