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

Updates babel-jest resolution #5932

Merged
merged 2 commits into from
Apr 11, 2018
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
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(
Copy link
Member

Choose a reason for hiding this comment

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

You are removing the resolution for packages that are not babel-jest, like ts-jest or ./my-custom-file or my-custom-transformer. You still need to support those as well.

Copy link
Contributor Author

@arcanis arcanis Apr 5, 2018

Choose a reason for hiding this comment

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

Hm, I see what you mean. Do you think it would be reasonable to have the same findNodeModule call as a fallback to avoid breaking the existing, but encourage people to use require.resolve in their configuration?

module.exports = {
  transform: {
    "*.ts": require.resolve('ts-jest')
  }
};

An issue is that it wouldn't work as-is in the package.json configuration, since functions can't be called here, so it would have to be kept inside a js configuration file.

Copy link
Contributor Author

@arcanis arcanis Apr 5, 2018

Choose a reason for hiding this comment

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

An alternative would be to defer resolving those paths until after the configuration has been completed, and then use the right resolver - but the same issue will occur for the resolver option, which ironically will need to be manually resolved by the user through require.resolve.

Copy link
Member

Choose a reason for hiding this comment

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

What if in Jest we call Resolver.findNodeModule(value). If it returns null, we call Resolver.findNodeModule(require.resolve(value)) (or just require.resolve(value) itself if possible). That way it doesn't affect users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But then it will resolve them relative to jest-config, not the user project. So ts-jest for example could fail because it would not be listed as a dependency of jest-config (whereas a require.resolve from the configuration file would always work, because the configuration file would be in the toplevel project).

Copy link
Member

Choose a reason for hiding this comment

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

Oh right, yeah this is a bit of a bummer :( I'd prefer not to demand people to rewrite their config with JS.

Copy link
Contributor Author

@arcanis arcanis Apr 6, 2018

Choose a reason for hiding this comment

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

Actually I just read the code again and I don't think this PR prevents external transformers from working - setupBabelJest is only used to detect babel jest and, if it can be found, automatically inject regenerator. Any other loader will be resolved and required later, through the regular resolver (cf here).

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