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: pass root in babel config #12689

Merged
merged 6 commits into from Apr 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 0 additions & 31 deletions .yarn/patches/react-native-npm-0.68.0-9eb3ecb60a.patch

This file was deleted.

2 changes: 0 additions & 2 deletions .yarnrc.yml
Expand Up @@ -18,8 +18,6 @@ packageExtensions:
peerDependencies:
"@babel/preset-env": ^7.1.6
react-native@*:
dependencies:
metro-babel-register: 0.67.0
peerDependencies:
"@babel/preset-env": ^7.1.6

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -59,6 +59,7 @@

### Fixes

- `[babel-jest]` [**BREAKING**] Pass `rootDir` as `root` in Babel's options ([#12689](https://github.com/facebook/jest/pull/12689))
- `[expect]` Move typings of `.not`, `.rejects` and `.resolves` modifiers outside of `Matchers` interface ([#12346](https://github.com/facebook/jest/pull/12346))
- `[expect]` Throw useful error if `expect.extend` is called with invalid matchers ([#12488](https://github.com/facebook/jest/pull/12488))
- `[expect]` Fix `iterableEquality` ignores other properties ([#8359](https://github.com/facebook/jest/pull/8359))
Expand Down
38 changes: 38 additions & 0 deletions e2e/__tests__/multiProjectRunner.test.ts
Expand Up @@ -572,3 +572,41 @@ describe("doesn't bleed module file extensions resolution with multiple workers"
expect(stderr).toMatch('PASS project2/__tests__/project2.test.js');
});
});

describe('Babel config in individual project works in multi-project', () => {
it('Prj-1 works individually', () => {
const result = runJest('multi-project-babel/prj-1');
expect(result.stderr).toMatch('PASS ./index.test.js');
expect(result.exitCode).toBe(0);
});
it('Prj-2 works individually', () => {
const result = runJest('multi-project-babel/prj-2');
expect(result.stderr).toMatch('PASS ./index.test.js');
expect(result.exitCode).toBe(0);
});
it('Prj-3 works individually', () => {
const result = runJest('multi-project-babel/prj-3');
expect(result.stderr).toMatch('PASS src/index.test.js');
expect(result.exitCode).toBe(0);
});
it('Prj-4 works individually', () => {
const result = runJest('multi-project-babel/prj-4');
expect(result.stderr).toMatch('PASS src/index.test.js');
expect(result.exitCode).toBe(0);
});
it('Prj-5 works individually', () => {
const result = runJest('multi-project-babel/prj-5');
expect(result.stderr).toMatch('PASS src/index.test.js');
expect(result.exitCode).toBe(0);
});
it('All project work when running from multiproject', () => {
const result = runJest('multi-project-babel');
expect(result.stderr).toMatch('PASS prj-1/index.test.js');
expect(result.stderr).toMatch('PASS prj-2/index.test.js');
expect(result.stderr).toMatch('PASS prj-3/src/index.test.js');
expect(result.stderr).toMatch('PASS prj-4/src/index.test.js');
expect(result.stderr).toMatch('PASS prj-5/src/index.test.js');
expect(result.stderr).toMatch('PASS prj-3/src/index.test.js');
expect(result.exitCode).toBe(0);
});
});
6 changes: 6 additions & 0 deletions e2e/global-setup/projects.jest.config.js
Expand Up @@ -15,13 +15,19 @@ module.exports = {
globalSetup: '<rootDir>/setup.js',
rootDir: path.resolve(__dirname, './project-1'),
testMatch: ['<rootDir>/**/*.test.js'],
transform: {
'\\.[jt]sx?$': [require.resolve('babel-jest'), {root: __dirname}],
},
transformIgnorePatterns: ['/node_modules/', '/packages/'],
},
{
displayName: 'project-2',
globalSetup: '<rootDir>/setup.js',
rootDir: path.resolve(__dirname, './project-2'),
testMatch: ['<rootDir>/**/*.test.js'],
transform: {
'\\.[jt]sx?$': [require.resolve('babel-jest'), {root: __dirname}],
},
transformIgnorePatterns: ['/node_modules/', '/packages/'],
},
],
Expand Down
15 changes: 15 additions & 0 deletions e2e/multi-project-babel/package.json
@@ -0,0 +1,15 @@
{
"jest": {
"projects": [
{
"rootDir": "<rootDir>/prj-1"
},
{
"rootDir": "<rootDir>/prj-2"
},
"<rootDir>/prj-3",
"<rootDir>/prj-4",
"<rootDir>/prj-5"
]
}
}
10 changes: 10 additions & 0 deletions e2e/multi-project-babel/prj-1/babel.config.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = {
presets: ['@babel/preset-flow'],
};
8 changes: 8 additions & 0 deletions e2e/multi-project-babel/prj-1/index.js
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = (text: string) => text;
12 changes: 12 additions & 0 deletions e2e/multi-project-babel/prj-1/index.test.js
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

const f = require('./');

it('Transpiles', () => {
expect(f('test')).toBe('test');
});
1 change: 1 addition & 0 deletions e2e/multi-project-babel/prj-1/package.json
@@ -0,0 +1 @@
{}
10 changes: 10 additions & 0 deletions e2e/multi-project-babel/prj-2/.babelrc.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = {
presets: ['@babel/preset-flow'],
};
8 changes: 8 additions & 0 deletions e2e/multi-project-babel/prj-2/index.js
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = (text: string) => text;
12 changes: 12 additions & 0 deletions e2e/multi-project-babel/prj-2/index.test.js
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

const f = require('./');

it('Transpiles', () => {
expect(f('test')).toBe('test');
});
1 change: 1 addition & 0 deletions e2e/multi-project-babel/prj-2/package.json
@@ -0,0 +1 @@
{}
5 changes: 5 additions & 0 deletions e2e/multi-project-babel/prj-3/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"rootDir": "src"
}
}
10 changes: 10 additions & 0 deletions e2e/multi-project-babel/prj-3/src/babel.config.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = {
presets: ['@babel/preset-flow'],
};
8 changes: 8 additions & 0 deletions e2e/multi-project-babel/prj-3/src/index.js
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = (text: string) => text;
12 changes: 12 additions & 0 deletions e2e/multi-project-babel/prj-3/src/index.test.js
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

const f = require('./');

it('Transpiles', () => {
expect(f('test')).toBe('test');
});
10 changes: 10 additions & 0 deletions e2e/multi-project-babel/prj-4/.babelrc.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = {
presets: ['@babel/preset-flow'],
};
5 changes: 5 additions & 0 deletions e2e/multi-project-babel/prj-4/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"rootDir": "src"
}
}
8 changes: 8 additions & 0 deletions e2e/multi-project-babel/prj-4/src/index.js
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = (text: string) => text;
12 changes: 12 additions & 0 deletions e2e/multi-project-babel/prj-4/src/index.test.js
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

const f = require('./');

it('Transpiles', () => {
expect(f('test')).toBe('test');
});
10 changes: 10 additions & 0 deletions e2e/multi-project-babel/prj-5/.babelrc.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = {
presets: ['@babel/preset-flow'],
};
5 changes: 5 additions & 0 deletions e2e/multi-project-babel/prj-5/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"rootDir": "src"
}
}
8 changes: 8 additions & 0 deletions e2e/multi-project-babel/prj-5/src/index.js
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

module.exports = (text: string) => text;
12 changes: 12 additions & 0 deletions e2e/multi-project-babel/prj-5/src/index.test.js
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

const f = require('./');

it('Transpiles', () => {
expect(f('test')).toBe('test');
});
3 changes: 0 additions & 3 deletions examples/react-native/jest.config.js
Expand Up @@ -3,9 +3,6 @@ const {resolve} = require('path');
module.exports = {
preset: 'react-native',
testEnvironment: 'jsdom',
transform: {
'\\.(js|ts|tsx)$': require.resolve('react-native/jest/preprocessor.js'),
},
// this is specific to the Jest repo, not generally needed (the files we ignore will be in node_modules which is ignored by default)
transformIgnorePatterns: [resolve(__dirname, '../../packages')],
};
1 change: 1 addition & 0 deletions jest.config.mjs
Expand Up @@ -48,6 +48,7 @@ export default {
'/e2e/custom-*',
'/e2e/test-in-root',
'/e2e/run-programmatically-multiple-projects',
'/e2e/multi-project-babel',
'\\.snap$',
'/packages/.*/build',
'/packages/.*/src/__tests__/setPrettyPrint.ts',
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -165,8 +165,7 @@
"resolutions": {
"babel-jest": "workspace:*",
"jest": "workspace:*",
"jest-environment-node": "workspace:*",
"react-native": "patch:react-native@npm:0.68.1#.yarn/patches/react-native-npm-0.68.0-9eb3ecb60a.patch"
"jest-environment-node": "workspace:*"
},
"packageManager": "yarn@3.2.0"
}
5 changes: 3 additions & 2 deletions packages/babel-jest/src/index.ts
Expand Up @@ -173,10 +173,11 @@ export const createTransformer: TransformerCreator<
filename: string,
transformOptions: JestTransformOptions,
): TransformOptions {
const {cwd} = transformOptions.config;
// `cwd` first to allow incoming options to override it
const {cwd, rootDir} = transformOptions.config;
// `cwd` and `root` first to allow incoming options to override it
return {
cwd,
root: rootDir,
...options,
caller: {
...options.caller,
Expand Down
20 changes: 7 additions & 13 deletions packages/jest-runtime/src/__mocks__/createRuntime.js
Expand Up @@ -37,36 +37,30 @@ const setupModuleNameMapper = (config, rootDir) => {
return [];
};

const setupTransform = (config, rootDir) => {
const setupTransform = (config, rootDir, cwd) => {
if (config?.transform) {
const transform = config.transform;
return Object.keys(transform).map(regex => [
regex,
path.resolve(rootDir, transform[regex]),
]);
}
return [['^.+\\.[jt]sx?$', require.resolve('babel-jest')]];
return [['^.+\\.[jt]sx?$', require.resolve('babel-jest'), {root: cwd}]];
};

module.exports = async function createRuntime(filename, projectConfig) {
const rootDir = path.resolve(path.dirname(filename), 'test_root');
const cwd = path.resolve(__dirname, '../../../..');

const moduleNameMapper = setupModuleNameMapper(projectConfig, rootDir);
const transform = setupTransform(projectConfig, rootDir);
const transform = setupTransform(projectConfig, rootDir, cwd);

projectConfig = makeProjectConfig({
cacheDirectory: getCacheDirectory(),
cwd: path.resolve(__dirname, '..', '..', '..', '..'),
cwd,
haste: {
hasteImplModulePath: path.resolve(
__dirname,
'..',
'..',
'..',
'jest-haste-map',
'src',
'__tests__',
'haste_impl.js',
hasteImplModulePath: require.resolve(
'../../../jest-haste-map/src/__tests__/haste_impl.js',
),
},
id: `Runtime-${filename.replace(/\W/, '-')}.tests`,
Expand Down