Skip to content

Commit

Permalink
fix(testing): create root babel config for babel-jest option
Browse files Browse the repository at this point in the history
when creating a jest project with babel-jest a root babel.config.json file will be create if one

doesn't exist and babel deps install for allowing jest tests to run successfully

ISSUES CLOSED: #6782
  • Loading branch information
barbados-clemens committed Dec 10, 2021
1 parent 46cd008 commit 7d1077d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions e2e/jest/src/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,16 @@ describe('Jest', () => {
); // text
expect(result.stdout).toContain('Coverage summary'); // text-summary
}, 90000);

it('should be able to test node lib with babel-jest', async () => {
const libName = uniq('babel-test-lib');
runCLI(
`generate @nrwl/node:lib ${libName} --buildable --importPath=@some-org/babel-test --publishable --babelJest`
);

const cliResults = await runCLIAsync(`test ${libName}`);
expect(cliResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
});
});
2 changes: 2 additions & 0 deletions packages/jest/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ function updateDependencies(tree: Tree, options: NormalizedSchema) {

if (options.babelJest) {
devDeps['babel-jest'] = babelJestVersion;
// in some cases @nrwl/web will not already be present i.e. node only projects
devDeps['@nrwl/web'] = nxVersion;
}

return addDependenciesToPackageJson(tree, dependencies, devDeps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ describe('jestProject', () => {
expect(tree.exists('libs/lib1/tsconfig.spec.json')).toBeTruthy();
});

it('should generate files w/babel-jest', async () => {
await jestProjectGenerator(tree, {
...defaultOptions,
project: 'lib1',
babelJest: true,
} as JestProjectSchema);
expect(tree.exists('babel.config.json')).toBeTruthy();
});

it('should alter workspace.json', async () => {
await jestProjectGenerator(tree, {
...defaultOptions,
Expand Down
10 changes: 10 additions & 0 deletions packages/jest/src/generators/jest-project/lib/create-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
offsetFromRoot,
generateFiles,
readProjectConfiguration,
stripIndents,
} from '@nrwl/devkit';
import { join } from 'path';

Expand All @@ -24,4 +25,13 @@ export function createFiles(tree: Tree, options: JestProjectSchema) {
if (options.setupFile === 'none') {
tree.delete(join(projectConfig.root, './src/test-setup.ts'));
}

if (options.babelJest && !tree.exists('babel.config.json')) {
tree.write(
'babel.config.json',
JSON.stringify({
babelrcRoots: ['*'],
})
);
}
}

0 comments on commit 7d1077d

Please sign in to comment.