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

fix: set cwd in babel-jest #7574

Merged
merged 2 commits into from
Jan 4, 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 @@ -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 @@
/**
Copy link
Member Author

Choose a reason for hiding this comment

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

this test fails without the change in babel-jest, but it only failed due to the other files in this project having flow code in them. Should we have a more explicit test? Not sure how, though, so ideas welcome

* 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 =>
Copy link
Member Author

Choose a reason for hiding this comment

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

no real reason for this extra function

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