From 30e670757875f0a501203c9bfe7298fc05a4ee55 Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Fri, 19 Nov 2021 14:52:07 -0600 Subject: [PATCH] fix(testing): create root babel config for babel-jest option 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 --- e2e/jest/src/jest.test.ts | 12 ++++++++++++ packages/jest/src/generators/init/init.ts | 2 ++ .../generators/jest-project/jest-project.spec.ts | 9 +++++++++ .../src/generators/jest-project/lib/create-files.ts | 13 +++++++++++-- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/e2e/jest/src/jest.test.ts b/e2e/jest/src/jest.test.ts index 981f75faacd07..395af4348533d 100644 --- a/e2e/jest/src/jest.test.ts +++ b/e2e/jest/src/jest.test.ts @@ -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' + ); + }); }); diff --git a/packages/jest/src/generators/init/init.ts b/packages/jest/src/generators/init/init.ts index 77a296d135ff2..4ff3ef520ec77 100644 --- a/packages/jest/src/generators/init/init.ts +++ b/packages/jest/src/generators/init/init.ts @@ -72,6 +72,8 @@ function updateDependencies(tree: Tree, options: NormalizedSchema) { devDeps['@swc/jest'] = swcJestVersion; } else { devDeps['ts-jest'] = tsJestVersion; + // in some cases @nrwl/web will not already be present i.e. node only projects + devDeps['@nrwl/web'] = nxVersion; } return addDependenciesToPackageJson(tree, dependencies, devDeps); diff --git a/packages/jest/src/generators/jest-project/jest-project.spec.ts b/packages/jest/src/generators/jest-project/jest-project.spec.ts index e2fbd301e1bf1..2c04028d36cca 100644 --- a/packages/jest/src/generators/jest-project/jest-project.spec.ts +++ b/packages/jest/src/generators/jest-project/jest-project.spec.ts @@ -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, diff --git a/packages/jest/src/generators/jest-project/lib/create-files.ts b/packages/jest/src/generators/jest-project/lib/create-files.ts index d7c86edd1e0f4..ec928301e5bc1 100644 --- a/packages/jest/src/generators/jest-project/lib/create-files.ts +++ b/packages/jest/src/generators/jest-project/lib/create-files.ts @@ -1,9 +1,9 @@ import { JestProjectSchema } from '../schema'; import { - Tree, - offsetFromRoot, generateFiles, + offsetFromRoot, readProjectConfiguration, + Tree, } from '@nrwl/devkit'; import { join } from 'path'; @@ -33,4 +33,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: ['*'], + }) + ); + } }