Skip to content

Commit

Permalink
Updates babel-jest resolution (#5932)
Browse files Browse the repository at this point in the history
* Uses require.resolve to locate babel-jest

* Updates snapshots
  • Loading branch information
arcanis authored Apr 11, 2018
1 parent 45c1746 commit 90810b1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 51 deletions.
15 changes: 13 additions & 2 deletions integration-tests/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -147,11 +154,15 @@ const extractSummary = (stdout: string) => {
.replace(/\d*\.?\d+m?s/g, '<<REPLACED>>')
.replace(/, estimated <<REPLACED>>/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:<<LINE>>:<<COLUMN>>');
}

return {rest, summary};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:<<LINE>>:<<COLUMN>>
at __tests__/only-constructs.test.js:<<LINE>>:<<COLUMN>>
"
`;
Expand All @@ -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:<<LINE>>:<<COLUMN>>
at __tests__/only-constructs.test.js:<<LINE>>:<<COLUMN>>
"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ exports[`--showConfig outputs config info and exits 1`] = `
\\"testRunner\\": \\"<<REPLACED_JEST_PACKAGES_DIR>>/jest-jasmine2/build/index.js\\",
\\"testURL\\": \\"about:blank\\",
\\"timers\\": \\"real\\",
\\"transform\\": [
[
\\"^.+\\\\\\\\.jsx?$\\",
\\"<<REPLACED_JEST_PACKAGES_DIR>>/babel-jest/build/index.js\\"
]
],
\\"transformIgnorePatterns\\": [
\\"/node_modules/\\"
],
Expand Down
5 changes: 2 additions & 3 deletions integration-tests/__tests__/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down
29 changes: 8 additions & 21 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
]);
});
Expand Down Expand Up @@ -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,
);
});

Expand All @@ -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' +
Expand All @@ -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' +
Expand All @@ -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 = '<rootDir>' + path.sep;

Expand Down
33 changes: 12 additions & 21 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -103,7 +99,6 @@ const setupPreset = (
};

const setupBabelJest = (options: InitialOptions) => {
const basedir = options.rootDir;
const transform = options.transform;
let babelJest;
if (transform) {
Expand All @@ -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;
Expand Down

0 comments on commit 90810b1

Please sign in to comment.