From cfff767b1271cd0e7fb90e3f54addb1f0fedbcc7 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Fri, 9 Jul 2021 16:30:09 +0300 Subject: [PATCH] fix(core): update root jest config with move generator (#6313) ISSUES CLOSED: #6303 --- .../move/lib/update-jest-config.spec.ts | 48 +++++++++++++++++++ .../generators/move/lib/update-jest-config.ts | 8 +--- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/packages/workspace/src/generators/move/lib/update-jest-config.spec.ts b/packages/workspace/src/generators/move/lib/update-jest-config.spec.ts index 48fbf1c02a..3f02701517 100644 --- a/packages/workspace/src/generators/move/lib/update-jest-config.spec.ts +++ b/packages/workspace/src/generators/move/lib/update-jest-config.spec.ts @@ -69,4 +69,52 @@ describe('updateJestConfig', () => { expect(rootJestConfigAfter).not.toContain('/libs/my-source'); expect(rootJestConfigAfter).toContain('/libs/my-destination'); }); + + it('should update jest configs properly even if project is in many layers of subfolders', async () => { + const jestConfig = `module.exports = { + name: 'some-test-dir-my-source', + preset: '../../jest.config.js', + coverageDirectory: '../../coverage/libs/some/test/dir/my-source', + snapshotSerializers: [ + 'jest-preset-angular/AngularSnapshotSerializer.js', + 'jest-preset-angular/HTMLCommentSerializer.js' + ] + };`; + const jestConfigPath = '/libs/other/test/dir/my-destination/jest.config.js'; + + const rootJestConfigPath = '/jest.config.js'; + + await libraryGenerator(tree, { + name: 'some/test/dir/my-source', + standaloneConfig: false, + }); + const projectConfig = readProjectConfiguration( + tree, + 'some-test-dir-my-source' + ); + tree.write(jestConfigPath, jestConfig); + + const schema: Schema = { + projectName: 'some-test-dir-my-source', + destination: 'other/test/dir/my-destination', + importPath: undefined, + updateImportPath: true, + }; + + updateJestConfig(tree, schema, projectConfig); + + const jestConfigAfter = tree.read(jestConfigPath, 'utf-8'); + const rootJestConfigAfter = tree.read(rootJestConfigPath, 'utf-8'); + expect(jestConfigAfter).toContain(`name: 'other-test-dir-my-destination'`); + expect(jestConfigAfter).toContain( + `coverageDirectory: '../../coverage/libs/other/test/dir/my-destination'` + ); + + expect(rootJestConfigAfter).not.toContain( + '/libs/some/test/dir/my-source' + ); + expect(rootJestConfigAfter).toContain( + '/libs/other/test/dir/my-destination' + ); + }); }); diff --git a/packages/workspace/src/generators/move/lib/update-jest-config.ts b/packages/workspace/src/generators/move/lib/update-jest-config.ts index 6bb16b944d..0ad7e1d6ad 100644 --- a/packages/workspace/src/generators/move/lib/update-jest-config.ts +++ b/packages/workspace/src/generators/move/lib/update-jest-config.ts @@ -1,4 +1,4 @@ -import { Tree, ProjectConfiguration, getWorkspaceLayout } from '@nrwl/devkit'; +import { Tree, ProjectConfiguration } from '@nrwl/devkit'; import * as path from 'path'; @@ -44,11 +44,7 @@ export function updateJestConfig( return; } - const { libsDir, appsDir } = getWorkspaceLayout(tree); - const findProject = new RegExp( - `\/(${libsDir}|${appsDir})\/${schema.projectName}`, - 'g' - ); + const findProject = new RegExp(`\/${project.root}`, 'g'); const oldRootJestConfigContent = tree.read(rootJestConfigPath, 'utf-8');