Skip to content

Commit

Permalink
fix: set cwd in babel-jest (jestjs#7574)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and captain-yossarian committed Jul 18, 2019
1 parent c4eaa8e commit e191f15
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
22 changes: 22 additions & 0 deletions e2e/transform/babel-jest/__tests__/changed_cwd.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
Empty file.
26 changes: 12 additions & 14 deletions packages/babel-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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(
Expand All @@ -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 || '',
Expand Down Expand Up @@ -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 ';
Expand Down

0 comments on commit e191f15

Please sign in to comment.