Skip to content

Commit

Permalink
Cache env in reporter (#8326)
Browse files Browse the repository at this point in the history
* Cache env in reporter 🔨
Get rid of match

* Update snapshot
  • Loading branch information
Connormiha authored and SimenB committed Apr 18, 2019
1 parent 3e05c06 commit 5a66bbb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Expand Up @@ -30,6 +30,6 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:471:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:472:17)
at Object.require (index.js:10:1)
`;
Expand Up @@ -33,6 +33,6 @@ FAIL __tests__/test.js
| ^
4 |
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:229:17)
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:230:17)
at Object.require (index.js:3:18)
`;
12 changes: 11 additions & 1 deletion packages/jest-reporters/src/__tests__/summary_reporter.test.js
Expand Up @@ -6,7 +6,7 @@
*/
'use strict';

import SummaryReporter from '../summary_reporter';
let SummaryReporter;

const env = {...process.env};
const now = Date.now;
Expand All @@ -18,6 +18,12 @@ const globalConfig = {

let results = [];

function requireReporter() {
jest.isolateModules(() => {
SummaryReporter = require('../summary_reporter').default;
});
}

beforeEach(() => {
process.env.npm_lifecycle_event = 'test';
process.env.npm_lifecycle_script = 'jest';
Expand Down Expand Up @@ -50,6 +56,7 @@ test('snapshots needs update with npm test', () => {
};

process.env.npm_config_user_agent = 'npm';
requireReporter();
const testReporter = new SummaryReporter(globalConfig);
testReporter.onRunComplete(new Set(), aggregatedResults);
expect(results.join('')).toMatchSnapshot();
Expand All @@ -73,6 +80,7 @@ test('snapshots needs update with yarn test', () => {
};

process.env.npm_config_user_agent = 'yarn';
requireReporter();
const testReporter = new SummaryReporter(globalConfig);
testReporter.onRunComplete(new Set(), aggregatedResults);
expect(results.join('')).toMatchSnapshot();
Expand Down Expand Up @@ -108,6 +116,7 @@ test('snapshots all have results (no update)', () => {
testResults: {},
};

requireReporter();
const testReporter = new SummaryReporter(globalConfig);
testReporter.onRunComplete(new Set(), aggregatedResults);
expect(results.join('').replace(/\\/g, '/')).toMatchSnapshot();
Expand Down Expand Up @@ -143,6 +152,7 @@ test('snapshots all have results (after update)', () => {
testResults: {},
};

requireReporter();
const testReporter = new SummaryReporter(globalConfig);
testReporter.onRunComplete(new Set(), aggregatedResults);
expect(results.join('').replace(/\\/g, '/')).toMatchSnapshot();
Expand Down
16 changes: 11 additions & 5 deletions packages/jest-reporters/src/summary_reporter.ts
Expand Up @@ -44,6 +44,12 @@ const NPM_EVENTS = new Set([
'postrestart',
]);

const {
npm_config_user_agent,
npm_lifecycle_event,
npm_lifecycle_script,
} = process.env;

export default class SummaryReporter extends BaseReporter {
private _estimatedTime: number;
private _globalConfig: Config.GlobalConfig;
Expand Down Expand Up @@ -123,15 +129,15 @@ export default class SummaryReporter extends BaseReporter {
snapshots.updated
) {
let updateCommand;
const event = process.env.npm_lifecycle_event || '';
const event = npm_lifecycle_event || '';
const prefix = NPM_EVENTS.has(event) ? '' : 'run ';
const isYarn =
typeof process.env.npm_config_user_agent === 'string' &&
process.env.npm_config_user_agent.match('yarn') !== null;
typeof npm_config_user_agent === 'string' &&
npm_config_user_agent.includes('yarn');
const client = isYarn ? 'yarn' : 'npm';
const scriptUsesJest =
typeof process.env.npm_lifecycle_script === 'string' &&
process.env.npm_lifecycle_script.indexOf('jest') !== -1;
typeof npm_lifecycle_script === 'string' &&
npm_lifecycle_script.includes('jest');

if (globalConfig.watch || globalConfig.watchAll) {
updateCommand = 'press `u`';
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-resolve/src/index.ts
Expand Up @@ -39,8 +39,9 @@ const NATIVE_PLATFORM = 'native';
// We might be inside a symlink.
const cwd = process.cwd();
const resolvedCwd = realpath(cwd) || cwd;
const nodePaths = process.env.NODE_PATH
? process.env.NODE_PATH.split(path.delimiter)
const {NODE_PATH} = process.env;
const nodePaths = NODE_PATH
? NODE_PATH.split(path.delimiter)
.filter(Boolean)
// The resolver expects absolute paths.
.map(p => path.resolve(resolvedCwd, p))
Expand Down

0 comments on commit 5a66bbb

Please sign in to comment.