From e8b3d5921b8a6279b5a420f4ab7014fad15db992 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 4 Jan 2019 10:33:18 +0100 Subject: [PATCH] fix: set cwd in babel-jest (#7574) --- CHANGELOG.md | 1 + e2e/__tests__/transform.test.js | 2 +- .../babel-jest/__tests__/changed_cwd.test.js | 22 ++++++++++++++++ e2e/transform/babel-jest/some-dir/.gitkeep | 0 packages/babel-jest/src/index.js | 26 +++++++++---------- 5 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 e2e/transform/babel-jest/__tests__/changed_cwd.test.js create mode 100644 e2e/transform/babel-jest/some-dir/.gitkeep diff --git a/CHANGELOG.md b/CHANGELOG.md index beaa01e200d7..6689292c5053 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,6 +103,7 @@ - `[pretty-format]` Omit unnecessary symbol filter for object keys ([#7457](https://github.com/facebook/jest/pull/7457)) - `[jest-runtime]` Fix `requireActual` on node_modules with mock present ([#7404](https://github.com/facebook/jest/pull/7404)) - `[jest-resolve]` Fix `isBuiltinModule` to support versions of node without `module.builtinModules` ([#7565](https://github.com/facebook/jest/pull/7565)) +- `[babel-jest]` Set `cwd` to be resilient to it changing during the runtime of the tests ([#7574](https://github.com/facebook/jest/pull/7574)) ### Chore & Maintenance diff --git a/e2e/__tests__/transform.test.js b/e2e/__tests__/transform.test.js index 619af7c52cf6..08cdbc27bdd9 100644 --- a/e2e/__tests__/transform.test.js +++ b/e2e/__tests__/transform.test.js @@ -29,7 +29,7 @@ describe('babel-jest', () => { // --no-cache because babel can cache stuff and result in false green const {json} = runWithJson(dir, ['--no-cache']); expect(json.success).toBe(true); - expect(json.numTotalTests).toBeGreaterThanOrEqual(1); + expect(json.numTotalTests).toBeGreaterThanOrEqual(2); }); it('instruments only specific files and collects coverage', () => { diff --git a/e2e/transform/babel-jest/__tests__/changed_cwd.test.js b/e2e/transform/babel-jest/__tests__/changed_cwd.test.js new file mode 100644 index 000000000000..ff28e2da1596 --- /dev/null +++ b/e2e/transform/babel-jest/__tests__/changed_cwd.test.js @@ -0,0 +1,22 @@ +/** + * 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'; + +const path = require('path'); + +beforeAll(() => { + process.chdir(path.resolve(__dirname, '../some-dir')); + + // even though we change the cwd, correct config is still found + require('../this-directory-is-covered/ExcludedFromCoverage'); +}); + +it('strips flowtypes using babel-jest and .babelrc', () => { + const a: string = 'a'; + expect(a).toBe('a'); +}); diff --git a/e2e/transform/babel-jest/some-dir/.gitkeep b/e2e/transform/babel-jest/some-dir/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/babel-jest/src/index.js b/packages/babel-jest/src/index.js index 87c7229c69bd..5837212da6ab 100644 --- a/packages/babel-jest/src/index.js +++ b/packages/babel-jest/src/index.js @@ -19,13 +19,19 @@ import crypto from 'crypto'; import fs from 'fs'; import path from 'path'; import {transformSync as babelTransform, loadPartialConfig} from '@babel/core'; -import babelIstanbulPlugin from 'babel-plugin-istanbul'; const THIS_FILE = fs.readFileSync(__filename); const jestPresetPath = require.resolve('babel-preset-jest'); +const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul'); +const cwd = process.cwd(); export const createTransformer = (options: any): Transformer => { - options = Object.assign({}, options, { + // Allow incoming options to override `cwd` + options = Object.assign({cwd}, options, { + caller: { + name: 'babel-jest', + supportsStaticESM: false, + }, compact: false, plugins: (options && options.plugins) || [], presets: ((options && options.presets) || []).concat(jestPresetPath), @@ -35,16 +41,6 @@ export const createTransformer = (options: any): Transformer => { delete options.cacheDirectory; delete options.filename; - const loadBabelOptions = filename => - loadPartialConfig({ - ...options, - caller: { - name: 'babel-jest', - supportsStaticESM: false, - }, - filename, - }); - return { canInstrument: true, getCacheKey( @@ -53,7 +49,7 @@ export const createTransformer = (options: any): Transformer => { configString: string, {instrument, rootDir}: CacheKeyOptions, ): string { - const babelOptions = loadBabelOptions(filename); + const babelOptions = loadPartialConfig({...options, filename}); const configPath = [ babelOptions.config || '', babelOptions.babelrc || '', @@ -86,7 +82,9 @@ export const createTransformer = (options: any): Transformer => { config: ProjectConfig, transformOptions?: TransformOptions, ): string | TransformedSource { - const babelOptions = {...loadBabelOptions(filename).options}; + const babelOptions = { + ...loadPartialConfig({...options, filename}).options, + }; if (transformOptions && transformOptions.instrument) { babelOptions.auxiliaryCommentBefore = ' istanbul ignore next ';