-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
project-graph-utils.spec.ts
241 lines (216 loc) · 6.07 KB
/
project-graph-utils.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { PackageJson } from './package-json';
import { ProjectGraph } from '../config/project-graph';
import {
getProjectNameFromDirPath,
getSourceDirOfDependentProjects,
mergeNpmScriptsWithTargets,
} from './project-graph-utils';
jest.mock('nx/src/utils/fileutils', () => ({
...(jest.requireActual('nx/src/utils/fileutils') as any),
readJsonFile: (path) => {
if (!(path in jsonFileOverrides))
throw new Error('Tried to read non-mocked json file: ' + path);
return jsonFileOverrides[path];
},
}));
let jsonFileOverrides: Record<string, any> = {};
describe('project graph utils', () => {
describe('getSourceDirOfDependentProjects', () => {
const projGraph: ProjectGraph = {
nodes: {
'demo-app': {
name: 'demo-app',
type: 'app',
data: {
root: 'apps/demo-app',
},
},
ui: {
name: 'ui',
type: 'lib',
data: {
root: 'libs/ui',
sourceRoot: 'libs/ui/src',
projectType: 'library',
targets: {},
},
},
core: {
name: 'core',
type: 'lib',
data: {
root: 'libs/core',
sourceRoot: 'libs/core/src',
projectType: 'library',
targets: {},
},
},
},
dependencies: {
'demo-app': [
{
type: 'static',
source: 'demo-app',
target: 'ui',
},
{
type: 'static',
source: 'demo-app',
target: 'npm:chalk',
},
{
type: 'static',
source: 'demo-app',
target: 'core',
},
],
},
};
it('should correctly gather the source root dirs of the dependent projects', () => {
const paths = getSourceDirOfDependentProjects('demo-app', projGraph);
expect(paths.length).toBe(2);
expect(paths).toContain(projGraph.nodes['ui'].data.sourceRoot);
expect(paths).toContain(projGraph.nodes['core'].data.sourceRoot);
});
it('should handle circular dependencies', () => {
projGraph.dependencies['core'] = [
{
type: 'static',
source: 'core',
target: 'demo-app',
},
];
const paths = getSourceDirOfDependentProjects('demo-app', projGraph);
expect(paths).toContain(projGraph.nodes['ui'].data.sourceRoot);
});
it('should throw an error if the project does not exist', () => {
expect(() =>
getSourceDirOfDependentProjects('non-existent-app', projGraph)
).toThrowError();
});
it('should find the project given a file within its src root', () => {
expect(getProjectNameFromDirPath('apps/demo-app', projGraph)).toEqual(
'demo-app'
);
expect(getProjectNameFromDirPath('apps/demo-app/src', projGraph)).toEqual(
'demo-app'
);
expect(
getProjectNameFromDirPath('apps/demo-app/src/subdir/bla', projGraph)
).toEqual('demo-app');
});
it('should throw an error if the project name has not been found', () => {
expect(() => {
getProjectNameFromDirPath('apps/demo-app-unknown');
}).toThrowError();
});
});
describe('mergeNpmScriptsWithTargets', () => {
const packageJson: PackageJson = {
name: 'my-app',
version: '0.0.0',
scripts: {
build: 'echo 1',
},
};
const packageJsonBuildTarget = {
executor: 'nx:run-script',
options: {
script: 'build',
},
};
beforeAll(() => {
jsonFileOverrides['apps/my-app/package.json'] = packageJson;
});
afterAll(() => {
jsonFileOverrides = {};
});
it('should prefer project.json targets', () => {
const projectJsonTargets = {
build: {
executor: '@nrwl/workspace:run-commands',
options: {
command: 'echo 2',
},
},
};
const result = mergeNpmScriptsWithTargets(
'apps/my-app',
projectJsonTargets
);
expect(result).toEqual(projectJsonTargets);
});
it('should provide targets from project.json and package.json', () => {
const projectJsonTargets = {
clean: {
executor: '@nrwl/workspace:run-commands',
options: {
command: 'echo 2',
},
},
};
const result = mergeNpmScriptsWithTargets(
'apps/my-app',
projectJsonTargets
);
expect(result).toEqual({
...projectJsonTargets,
build: packageJsonBuildTarget,
});
});
it('should contain extended options from nx property in package.json', () => {
jsonFileOverrides['apps/my-other-app/package.json'] = {
name: 'my-other-app',
scripts: {
build: 'echo 1',
},
nx: {
targets: {
build: {
outputs: ['custom'],
},
},
},
};
const result = mergeNpmScriptsWithTargets('apps/my-other-app', null);
expect(result).toEqual({
build: { ...packageJsonBuildTarget, outputs: ['custom'] },
});
});
it('should work when project.json targets is null', () => {
const result = mergeNpmScriptsWithTargets('apps/my-app', null);
expect(result).toEqual({
build: {
executor: 'nx:run-script',
options: {
script: 'build',
},
},
});
});
it("should work when project root is ''", () => {
jsonFileOverrides['package.json'] = {
name: 'my-app',
scripts: {
test: 'echo testing',
},
};
const result = mergeNpmScriptsWithTargets('', {
build: {
executor: '@nrwl/workspace:run-commands',
options: { command: 'echo hi' },
},
});
expect(result).toEqual({
build: {
executor: '@nrwl/workspace:run-commands',
options: { command: 'echo hi' },
},
test: {
executor: 'nx:run-script',
options: { script: 'test' },
},
});
});
});
});