From 90810b15dea599e8182231f1049d794b9917f4ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Wed, 11 Apr 2018 14:50:00 -0700 Subject: [PATCH] Updates babel-jest resolution (#5932) * Uses require.resolve to locate babel-jest * Updates snapshots --- integration-tests/Utils.js | 15 +++++++-- .../__snapshots__/globals.test.js.snap | 8 ++--- .../__snapshots__/show_config.test.js.snap | 6 ++++ integration-tests/__tests__/globals.test.js | 5 ++- .../src/__tests__/normalize.test.js | 29 +++++----------- packages/jest-config/src/normalize.js | 33 +++++++------------ 6 files changed, 45 insertions(+), 51 deletions(-) diff --git a/integration-tests/Utils.js b/integration-tests/Utils.js index 899145fafe6b..c5e446f3a0c6 100644 --- a/integration-tests/Utils.js +++ b/integration-tests/Utils.js @@ -129,7 +129,14 @@ const createEmptyPackage = ( ); }; -const extractSummary = (stdout: string) => { +type ExtractSummaryOptions = {| + stripLocation: boolean, +|}; + +const extractSummary = ( + stdout: string, + {stripLocation = false}: ExtractSummaryOptions = {}, +) => { const match = stdout.match( /Test Suites:.*\nTests.*\nSnapshots.*\nTime.*(\nRan all test suites)*.*\n*$/gm, ); @@ -147,11 +154,15 @@ const extractSummary = (stdout: string) => { .replace(/\d*\.?\d+m?s/g, '<>') .replace(/, estimated <>/g, ''); - const rest = cleanupStackTrace( + let rest = cleanupStackTrace( // remove all timestamps stdout.slice(0, -match[0].length).replace(/\s*\(\d*\.?\d+m?s\)$/gm, ''), ); + if (stripLocation) { + rest = rest.replace(/(at .*):\d+:\d+/g, '$1:<>:<>'); + } + return {rest, summary}; }; diff --git a/integration-tests/__tests__/__snapshots__/globals.test.js.snap b/integration-tests/__tests__/__snapshots__/globals.test.js.snap index 4dc71b817345..90e81f980f56 100644 --- a/integration-tests/__tests__/__snapshots__/globals.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/globals.test.js.snap @@ -32,8 +32,8 @@ exports[`cannot test with no implementation 1`] = ` 4 | test('test, no implementation'); 5 | - at packages/jest-jasmine2/build/jasmine/Env.js:429:15 - at __tests__/only-constructs.test.js:3:5 + at packages/jest-jasmine2/build/jasmine/Env.js:<>:<> + at __tests__/only-constructs.test.js:<>:<> " `; @@ -59,8 +59,8 @@ exports[`cannot test with no implementation with expand arg 1`] = ` 4 | test('test, no implementation'); 5 | - at packages/jest-jasmine2/build/jasmine/Env.js:429:15 - at __tests__/only-constructs.test.js:3:5 + at packages/jest-jasmine2/build/jasmine/Env.js:<>:<> + at __tests__/only-constructs.test.js:<>:<> " `; diff --git a/integration-tests/__tests__/__snapshots__/show_config.test.js.snap b/integration-tests/__tests__/__snapshots__/show_config.test.js.snap index 43a2c9b36c5e..22911ad113be 100644 --- a/integration-tests/__tests__/__snapshots__/show_config.test.js.snap +++ b/integration-tests/__tests__/__snapshots__/show_config.test.js.snap @@ -54,6 +54,12 @@ exports[`--showConfig outputs config info and exits 1`] = ` \\"testRunner\\": \\"<>/jest-jasmine2/build/index.js\\", \\"testURL\\": \\"about:blank\\", \\"timers\\": \\"real\\", + \\"transform\\": [ + [ + \\"^.+\\\\\\\\.jsx?$\\", + \\"<>/babel-jest/build/index.js\\" + ] + ], \\"transformIgnorePatterns\\": [ \\"/node_modules/\\" ], diff --git a/integration-tests/__tests__/globals.test.js b/integration-tests/__tests__/globals.test.js index 79cc76beaac8..22a8e60f6041 100644 --- a/integration-tests/__tests__/globals.test.js +++ b/integration-tests/__tests__/globals.test.js @@ -122,8 +122,7 @@ test('cannot test with no implementation', () => { const {stderr, status} = runJest(DIR); expect(status).toBe(1); - const {summary, rest} = extractSummary(stderr); - + const {summary, rest} = extractSummary(stderr, {stripLocation: true}); expect(rest).toMatchSnapshot(); expect(summary).toMatchSnapshot(); }); @@ -201,7 +200,7 @@ test('cannot test with no implementation with expand arg', () => { const {stderr, status} = runJest(DIR, ['--expand']); expect(status).toBe(1); - const {summary, rest} = extractSummary(stderr); + const {summary, rest} = extractSummary(stderr, {stripLocation: true}); expect(rest).toMatchSnapshot(); expect(summary).toMatchSnapshot(); }); diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 94003c410899..718e24404c32 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -25,7 +25,7 @@ let expectedPathAbs; let expectedPathAbsAnother; const findNodeModule = jest.fn(name => { - if (name.indexOf('jest-jasmine2') !== -1) { + if (name.match(/jest-jasmine2|babel-jest/)) { return name; } return null; @@ -300,7 +300,7 @@ describe('transform', () => { expect(options.transform).toEqual([ [DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util'], - [DEFAULT_JS_PATTERN, 'babel-jest'], + [DEFAULT_JS_PATTERN, require.resolve('babel-jest')], ['abs-path', '/qux/quux'], ]); }); @@ -684,7 +684,10 @@ describe('babel-jest', () => { beforeEach(() => { Resolver = require('jest-resolve'); Resolver.findNodeModule = jest.fn( - name => path.sep + 'node_modules' + path.sep + name, + name => + name.indexOf('babel-jest') === -1 + ? path.sep + 'node_modules' + path.sep + name + : name, ); }); @@ -697,9 +700,7 @@ describe('babel-jest', () => { ); expect(options.transform[0][0]).toBe(DEFAULT_JS_PATTERN); - expect(options.transform[0][1]).toEqual( - path.sep + 'node_modules' + path.sep + 'babel-jest', - ); + expect(options.transform[0][1]).toEqual(require.resolve('babel-jest')); expect(options.setupFiles).toEqual([ path.sep + 'node_modules' + @@ -723,7 +724,7 @@ describe('babel-jest', () => { ); expect(options.transform[0][0]).toBe(customJSPattern); - expect(options.transform[0][1]).toEqual('/node_modules/babel-jest'); + expect(options.transform[0][1]).toEqual(require.resolve('babel-jest')); expect(options.setupFiles).toEqual([ path.sep + 'node_modules' + @@ -734,20 +735,6 @@ describe('babel-jest', () => { ]); }); - it(`doesn't use babel-jest if its not available`, () => { - Resolver.findNodeModule = findNodeModule; - - const {options} = normalize( - { - rootDir: '/root', - }, - {}, - ); - - expect(options.transform).toEqual(undefined); - expect(options.setupFiles).toEqual([]); - }); - it('uses regenerator if babel-jest is explicitly specified', () => { const ROOT_DIR = '' + path.sep; diff --git a/packages/jest-config/src/normalize.js b/packages/jest-config/src/normalize.js index b53185643f3f..93b3ce7ae6cb 100644 --- a/packages/jest-config/src/normalize.js +++ b/packages/jest-config/src/normalize.js @@ -29,11 +29,7 @@ import { getTestEnvironment, resolve, } from './utils'; -import { - NODE_MODULES, - DEFAULT_JS_PATTERN, - DEFAULT_REPORTER_LABEL, -} from './constants'; +import {DEFAULT_JS_PATTERN, DEFAULT_REPORTER_LABEL} from './constants'; import {validateReporters} from './reporter_validation_errors'; import DEFAULT_CONFIG from './defaults'; import DEPRECATED_CONFIG from './deprecated'; @@ -103,7 +99,6 @@ const setupPreset = ( }; const setupBabelJest = (options: InitialOptions) => { - const basedir = options.rootDir; const transform = options.transform; let babelJest; if (transform) { @@ -113,24 +108,20 @@ const setupBabelJest = (options: InitialOptions) => { }); if (customJSPattern) { - const jsTransformer = Resolver.findNodeModule( - transform[customJSPattern], - {basedir}, - ); - if ( - jsTransformer && - jsTransformer.includes(NODE_MODULES + 'babel-jest') - ) { - babelJest = jsTransformer; + const customJSTransformer = transform[customJSPattern]; + + if (customJSTransformer === 'babel-jest') { + babelJest = require.resolve('babel-jest'); + transform[customJSPattern] = babelJest; + } else if (customJSTransformer.includes('babel-jest')) { + babelJest = customJSTransformer; } } } else { - babelJest = Resolver.findNodeModule('babel-jest', {basedir}); - if (babelJest) { - options.transform = { - [DEFAULT_JS_PATTERN]: 'babel-jest', - }; - } + babelJest = require.resolve('babel-jest'); + options.transform = { + [DEFAULT_JS_PATTERN]: babelJest, + }; } return babelJest;