Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c16c161

Browse files
devinshoemakerFrozenPandaz
authored andcommittedAug 18, 2020
fix(core): tests
1 parent 8f62e8d commit c16c161

File tree

3 files changed

+174
-2
lines changed

3 files changed

+174
-2
lines changed
 

Diff for: ‎e2e/workspace/src/workspace-aux-commands.test.ts

+143
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
uniq,
1616
updateFile,
1717
workspaceConfigName,
18+
setCurrentProjName,
19+
runCreateWorkspace,
1820
} from '@nrwl/e2e/utils';
1921

2022
forEachCli((cli) => {
@@ -666,6 +668,147 @@ forEachCli((cli) => {
666668
`import { fromLibOne } from '@proj/shared/${lib1}/data-access';`
667669
);
668670
});
671+
672+
it('should work for custom workspace layouts', () => {
673+
const lib1 = uniq('mylib');
674+
const lib2 = uniq('mylib');
675+
const lib3 = uniq('mylib');
676+
newProject();
677+
678+
let nxJson = readJson('nx.json');
679+
nxJson.workspaceLayout = { libsDir: 'packages' };
680+
updateFile('nx.json', JSON.stringify(nxJson));
681+
682+
runCLI(`generate @nrwl/workspace:lib ${lib1}/data-access`);
683+
684+
updateFile(
685+
`packages/${lib1}/data-access/src/lib/${lib1}-data-access.ts`,
686+
`export function fromLibOne() { console.log('This is completely pointless'); }`
687+
);
688+
689+
updateFile(
690+
`packages/${lib1}/data-access/src/index.ts`,
691+
`export * from './lib/${lib1}-data-access.ts'`
692+
);
693+
694+
/**
695+
* Create a library which imports a class from lib1
696+
*/
697+
698+
runCLI(`generate @nrwl/workspace:lib ${lib2}/ui`);
699+
700+
updateFile(
701+
`packages/${lib2}/ui/src/lib/${lib2}-ui.ts`,
702+
`import { fromLibOne } from '@proj/${lib1}/data-access';
703+
704+
export const fromLibTwo = () => fromLibOne(); }`
705+
);
706+
707+
/**
708+
* Create a library which has an implicit dependency on lib1
709+
*/
710+
711+
runCLI(`generate @nrwl/workspace:lib ${lib3}`);
712+
nxJson = JSON.parse(readFile('nx.json')) as NxJson;
713+
nxJson.projects[lib3].implicitDependencies = [`${lib1}-data-access`];
714+
updateFile(`nx.json`, JSON.stringify(nxJson));
715+
716+
/**
717+
* Now try to move lib1
718+
*/
719+
720+
const moveOutput = runCLI(
721+
`generate @nrwl/workspace:move --project ${lib1}-data-access shared/${lib1}/data-access`
722+
);
723+
724+
expect(moveOutput).toContain(`DELETE packages/${lib1}/data-access`);
725+
expect(exists(`packages/${lib1}/data-access`)).toBeFalsy();
726+
727+
const newPath = `packages/shared/${lib1}/data-access`;
728+
const newName = `shared-${lib1}-data-access`;
729+
730+
const readmePath = `${newPath}/README.md`;
731+
expect(moveOutput).toContain(`CREATE ${readmePath}`);
732+
checkFilesExist(readmePath);
733+
734+
const jestConfigPath = `${newPath}/jest.config.js`;
735+
expect(moveOutput).toContain(`CREATE ${jestConfigPath}`);
736+
checkFilesExist(jestConfigPath);
737+
const jestConfig = readFile(jestConfigPath);
738+
expect(jestConfig).toContain(`name: 'shared-${lib1}-data-access'`);
739+
expect(jestConfig).toContain(`preset: '../../../../jest.config.js'`);
740+
expect(jestConfig).toContain(
741+
`coverageDirectory: '../../../../coverage/${newPath}'`
742+
);
743+
744+
const tsConfigPath = `${newPath}/tsconfig.json`;
745+
expect(moveOutput).toContain(`CREATE ${tsConfigPath}`);
746+
checkFilesExist(tsConfigPath);
747+
748+
const tsConfigLibPath = `${newPath}/tsconfig.lib.json`;
749+
expect(moveOutput).toContain(`CREATE ${tsConfigLibPath}`);
750+
checkFilesExist(tsConfigLibPath);
751+
const tsConfigLib = readJson(tsConfigLibPath);
752+
expect(tsConfigLib.compilerOptions.outDir).toEqual(
753+
'../../../../dist/out-tsc'
754+
);
755+
756+
const tsConfigSpecPath = `${newPath}/tsconfig.spec.json`;
757+
expect(moveOutput).toContain(`CREATE ${tsConfigSpecPath}`);
758+
checkFilesExist(tsConfigSpecPath);
759+
const tsConfigSpec = readJson(tsConfigSpecPath);
760+
expect(tsConfigSpec.compilerOptions.outDir).toEqual(
761+
'../../../../dist/out-tsc'
762+
);
763+
764+
const indexPath = `${newPath}/src/index.ts`;
765+
expect(moveOutput).toContain(`CREATE ${indexPath}`);
766+
checkFilesExist(indexPath);
767+
768+
const rootClassPath = `${newPath}/src/lib/${lib1}-data-access.ts`;
769+
expect(moveOutput).toContain(`CREATE ${rootClassPath}`);
770+
checkFilesExist(rootClassPath);
771+
772+
expect(moveOutput).toContain('UPDATE nx.json');
773+
nxJson = JSON.parse(readFile('nx.json')) as NxJson;
774+
expect(nxJson.projects[`${lib1}-data-access`]).toBeUndefined();
775+
expect(nxJson.projects[newName]).toEqual({
776+
tags: [],
777+
});
778+
expect(nxJson.projects[lib3].implicitDependencies).toEqual([
779+
`shared-${lib1}-data-access`,
780+
]);
781+
782+
expect(moveOutput).toContain('UPDATE tsconfig.base.json');
783+
const rootTsConfig = readJson('tsconfig.base.json');
784+
expect(
785+
rootTsConfig.compilerOptions.paths[`@proj/${lib1}/data-access`]
786+
).toBeUndefined();
787+
expect(
788+
rootTsConfig.compilerOptions.paths[`@proj/shared/${lib1}/data-access`]
789+
).toEqual([`packages/shared/${lib1}/data-access/src/index.ts`]);
790+
791+
expect(moveOutput).toContain(`UPDATE ${workspace}.json`);
792+
const workspaceJson = readJson(`${workspace}.json`);
793+
expect(workspaceJson.projects[`${lib1}-data-access`]).toBeUndefined();
794+
const project = workspaceJson.projects[newName];
795+
expect(project).toBeTruthy();
796+
expect(project.root).toBe(newPath);
797+
expect(project.sourceRoot).toBe(`${newPath}/src`);
798+
expect(project.architect.lint.options.tsConfig).toEqual([
799+
`packages/shared/${lib1}/data-access/tsconfig.lib.json`,
800+
`packages/shared/${lib1}/data-access/tsconfig.spec.json`,
801+
]);
802+
803+
/**
804+
* Check that the import in lib2 has been updated
805+
*/
806+
const lib2FilePath = `packages/${lib2}/ui/src/lib/${lib2}-ui.ts`;
807+
const lib2File = readFile(lib2FilePath);
808+
expect(lib2File).toContain(
809+
`import { fromLibOne } from '@proj/shared/${lib1}/data-access';`
810+
);
811+
});
669812
});
670813

671814
describe('Remove Project', () => {

Diff for: ‎packages/workspace/src/schematics/move/lib/update-imports.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { from, Observable } from 'rxjs';
99
import { map } from 'rxjs/operators';
1010
import { Schema } from '../schema';
1111
import { normalizeSlashes } from './utils';
12+
import { ProjectType } from '@nrwl/workspace/src/utils/project-type';
1213

1314
/**
1415
* Updates all the imports in the workspace and modifies the tsconfig appropriately.
@@ -20,6 +21,9 @@ export function updateImports(schema: Schema) {
2021
return from(getWorkspace(tree)).pipe(
2122
map((workspace) => {
2223
const nxJson = readJsonInTree<NxJson>(tree, 'nx.json');
24+
const libsDir = nxJson.workspaceLayout?.libsDir
25+
? nxJson.workspaceLayout.libsDir
26+
: 'libs';
2327
const project = workspace.projects.get(schema.projectName);
2428

2529
if (project.extensions['projectType'] === 'application') {
@@ -29,7 +33,7 @@ export function updateImports(schema: Schema) {
2933

3034
const projectRef = {
3135
from: normalizeSlashes(
32-
`@${nxJson.npmScope}/${project.root.substr(5)}`
36+
`@${nxJson.npmScope}/${project.root.substr(libsDir.length + 1)}`
3337
),
3438
to: normalizeSlashes(`@${nxJson.npmScope}/${schema.destination}`),
3539
};
@@ -57,7 +61,7 @@ export function updateImports(schema: Schema) {
5761
}
5862

5963
const projectRoot = {
60-
from: project.root.substr(5),
64+
from: project.root.substr(libsDir.length + 1),
6165
to: schema.destination,
6266
};
6367

Diff for: ‎packages/workspace/src/schematics/move/lib/update-workspace.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Tree } from '@angular-devkit/schematics';
22
import { UnitTestTree } from '@angular-devkit/schematics/testing';
3+
import { NxJson, updateJsonInTree } from '@nrwl/workspace';
34
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
45
import { callRule } from '../../../utils/testing';
56
import { Schema } from '../schema';
@@ -207,4 +208,28 @@ describe('updateWorkspace Rule', () => {
207208
e2eProject.architect.e2e.configurations.production.devServerTarget
208209
).toBe('subfolder-my-destination:serve:production');
209210
});
211+
212+
it('honor custom workspace layouts', async () => {
213+
const schema: Schema = {
214+
projectName: 'my-source',
215+
destination: 'subfolder/my-destination',
216+
};
217+
218+
tree = (await callRule(
219+
updateJsonInTree<NxJson>('nx.json', (json) => {
220+
json.workspaceLayout = { appsDir: 'e2e', libsDir: 'packages' };
221+
return json;
222+
}),
223+
tree
224+
)) as UnitTestTree;
225+
226+
tree = (await callRule(updateWorkspace(schema), tree)) as UnitTestTree;
227+
228+
const workspace = JSON.parse(tree.read('workspace.json').toString());
229+
230+
const project = workspace.projects['subfolder-my-destination'];
231+
expect(project).toBeDefined();
232+
expect(project.root).toBe('e2e/subfolder/my-destination');
233+
expect(project.sourceRoot).toBe('e2e/subfolder/my-destination/src');
234+
});
210235
});

0 commit comments

Comments
 (0)
Please sign in to comment.