/
package.impl.spec.ts
211 lines (189 loc) · 6.09 KB
/
package.impl.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
import { JsonObject } from '@angular-devkit/core';
import { MockBuilderContext } from '@nrwl/workspace/testing';
import { BuildAngularLibraryBuilderOptions, run } from './package.impl';
import { getMockContext } from '../../utils/testing';
import * as projectGraphUtils from '@nrwl/workspace/src/core/project-graph';
import * as workspaceUtils from '@nrwl/workspace';
import {
ProjectGraph,
ProjectType
} from '@nrwl/workspace/src/core/project-graph';
import * as fileUtils from '@nrwl/workspace/src/utils/fileutils';
jest.mock('ng-packagr');
import * as ngPackagrImport from 'ng-packagr';
import * as ng from '@angular/compiler-cli';
import { NgPackagr } from 'ng-packagr';
class NgPackagrMock extends NgPackagr {
constructor() {
super(null);
}
}
describe('AngularLibraryWebBuildBuilder', () => {
let context: MockBuilderContext;
let testOptions: BuildAngularLibraryBuilderOptions & JsonObject;
let ngPackagrMock;
beforeEach(async () => {
context = await getMockContext();
// NgPackagr has some weird fluent API. I wonder whether
// I could simplify this mock
ngPackagrMock = new NgPackagrMock();
spyOn(ngPackagrMock, 'build').and.callFake(() => {
return Promise.resolve();
});
spyOn(ngPackagrMock, 'forProject').and.callThrough();
spyOn(ngPackagrMock, 'withTsConfig').and.callThrough();
spyOn(ngPackagrImport, 'ngPackagr').and.callFake(() => {
return ngPackagrMock;
});
// used for updating the json files
spyOn(fileUtils, 'writeJsonFile');
// parent tsconfig
spyOn(ng, 'readConfiguration').and.callFake(() => {
return {
options: {
paths: {
'@proj/buildable-child': []
}
}
};
});
spyOn(fileUtils, 'fileExists').and.returnValue(true);
context.target = {
project: 'buildable-parent',
target: null
};
testOptions = {
tsConfig: 'libs/publishable-parent/tsconfig.lib.json',
project: 'libs/publishable-parent/ng-package.json'
};
});
it('should invoke ng-packagr for a libary without any dependencies', async () => {
spyOn(projectGraphUtils, 'createProjectGraph').and.callFake(() => {
return {
nodes: {
'buildable-parent': {
type: ProjectType.lib,
name: 'buildable-parent',
data: { files: [], root: 'libs/buildable-parent' }
}
},
dependencies: {}
} as ProjectGraph;
});
// act
const result = await run(testOptions, context).toPromise();
expect(result.success).toBeTruthy();
expect(ngPackagrMock.build).toHaveBeenCalled();
});
describe('with dependent libraries', () => {
beforeEach(() => {
// create project graph with dependencies
spyOn(projectGraphUtils, 'createProjectGraph').and.callFake(() => {
return {
nodes: {
'buildable-parent': {
type: ProjectType.lib,
name: 'buildable-parent',
data: { files: [], root: 'libs/buildable-parent' }
},
'buildable-child': {
type: ProjectType.lib,
name: 'buildable-child',
data: {
files: [],
root: 'libs/buildable-child',
prefix: 'proj',
architect: {
build: {
builder: 'any builder'
}
}
}
}
},
dependencies: {
'buildable-parent': [
{
type: ProjectType.lib,
target: 'buildable-child',
source: null
}
],
'buildable-child': []
}
} as ProjectGraph;
});
});
it('should properly set the TSConfig paths', async () => {
spyOn(workspaceUtils, 'readJsonFile').and.returnValue({
name: '@proj/buildable-child',
version: '1.2.3'
});
// act
const result = await run(testOptions, context).toPromise();
// assert
expect(result.success).toBeTruthy();
expect(ngPackagrMock.withTsConfig).toHaveBeenCalledWith(
jasmine.objectContaining({
options: {
paths: { '@proj/buildable-child': ['dist/libs/buildable-child'] }
}
})
);
});
it('should update the package.json', async () => {
spyOn(workspaceUtils, 'readJsonFile').and.callFake((path: string) => {
if (path.endsWith('buildable-parent/package.json')) {
return {
name: '@proj/buildable-parent',
version: '3.3.3'
};
} else {
return {
name: '@proj/buildable-child',
version: '1.2.3'
};
}
});
// act
const result = await run(testOptions, context).toPromise();
// assert
expect(result.success).toBeTruthy();
expect(fileUtils.writeJsonFile).toHaveBeenCalledWith(
'dist/libs/buildable-parent/package.json',
jasmine.objectContaining({
dependencies: {
'@proj/buildable-child': '1.2.3'
}
})
);
});
['dependencies', 'devDependencies', 'peerDependencies'].forEach(
(depConfigName: string) => {
it(`should not update the package.json if the ${depConfigName} already contain a matching entry`, async () => {
spyOn(workspaceUtils, 'readJsonFile').and.callFake((path: string) => {
if (path.endsWith('buildable-parent/package.json')) {
return {
name: '@proj/buildable-parent',
version: '1.2.3',
[depConfigName]: {
'@proj/buildable-child': '1.1.1'
}
};
} else {
return {
name: '@proj/buildable-child',
version: '1.2.3'
};
}
});
// act
const result = await run(testOptions, context).toPromise();
// assert
expect(result.success).toBeTruthy();
expect(fileUtils.writeJsonFile).not.toHaveBeenCalled();
});
}
);
});
});