-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
package.impl.spec.ts
171 lines (148 loc) · 6.02 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
jest.mock('@nrwl/workspace/src/core/project-graph');
jest.mock('@nrwl/workspace/src/utilities/buildable-libs-utils');
jest.mock('ng-packagr');
jest.mock('./ng-packagr-adjustments/ng-package/options.di');
import type { ExecutorContext } from '@nrwl/devkit';
import * as buildableLibsUtils from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import * as ngPackagr from 'ng-packagr';
import { BehaviorSubject } from 'rxjs';
import { NX_ENTRY_POINT_PROVIDERS } from './ng-packagr-adjustments/ng-package/entry-point/entry-point.di';
import { nxProvideOptions } from './ng-packagr-adjustments/ng-package/options.di';
import {
NX_PACKAGE_PROVIDERS,
NX_PACKAGE_TRANSFORM,
} from './ng-packagr-adjustments/ng-package/package.di';
import { packageExecutor } from './package.impl';
import type { BuildAngularLibraryExecutorOptions } from './schema';
describe('Package executor', () => {
let context: ExecutorContext;
let ngPackagrBuildMock: jest.Mock;
let ngPackagerWatchSubject: BehaviorSubject<void>;
let ngPackagrWatchMock: jest.Mock;
let ngPackagrWithBuildTransformMock: jest.Mock;
let ngPackagrWithTsConfigMock: jest.Mock;
let options: BuildAngularLibraryExecutorOptions;
beforeEach(async () => {
(
buildableLibsUtils.calculateProjectDependencies as jest.Mock
).mockImplementation(() => ({
target: {},
dependencies: [],
topLevelDependencies: [],
}));
ngPackagrBuildMock = jest.fn(() => Promise.resolve());
ngPackagerWatchSubject = new BehaviorSubject<void>(undefined);
ngPackagrWatchMock = jest.fn(() => ngPackagerWatchSubject.asObservable());
ngPackagrWithBuildTransformMock = jest.fn();
ngPackagrWithTsConfigMock = jest.fn();
(ngPackagr.NgPackagr as jest.Mock).mockImplementation(() => ({
build: ngPackagrBuildMock,
forProject: jest.fn(),
watch: ngPackagrWatchMock,
withBuildTransform: ngPackagrWithBuildTransformMock,
withTsConfig: ngPackagrWithTsConfigMock,
}));
context = {
root: '/root',
projectName: 'my-lib',
targetName: 'build',
configurationName: 'production',
workspace: { projects: { 'my-lib': { root: '/libs/my-lib' } } },
} as any;
options = { project: 'my-lib' };
});
afterEach(() => jest.clearAllMocks());
it('should return unsuccessful result when deps have not been built', async () => {
(
buildableLibsUtils.checkDependentProjectsHaveBeenBuilt as jest.Mock
).mockReturnValue(false);
const result = await packageExecutor(options, context).next();
expect(result.value).toEqual({ success: false });
expect(result.done).toBe(true);
});
it('should build the library when deps have been built', async () => {
(
buildableLibsUtils.checkDependentProjectsHaveBeenBuilt as jest.Mock
).mockReturnValue(true);
const result = await packageExecutor(options, context).next();
expect(ngPackagrBuildMock).toHaveBeenCalled();
expect(result.value).toEqual({ success: true });
expect(result.done).toBe(true);
});
it('should instantiate NgPackager with the right providers and set to use the right build transformation provider', async () => {
(
buildableLibsUtils.checkDependentProjectsHaveBeenBuilt as jest.Mock
).mockReturnValue(true);
const extraOptions: Partial<BuildAngularLibraryExecutorOptions> = {
tailwindConfig: 'path/to/tailwind.config.js',
watch: false,
};
const nxProvideOptionsResult = { ...extraOptions, cacheEnabled: true };
(nxProvideOptions as jest.Mock).mockImplementation(
() => nxProvideOptionsResult
);
const result = await packageExecutor(
{ ...options, ...extraOptions },
context
).next();
expect(ngPackagr.NgPackagr).toHaveBeenCalledWith([
...NX_PACKAGE_PROVIDERS,
...NX_ENTRY_POINT_PROVIDERS,
nxProvideOptionsResult,
]);
expect(ngPackagrWithBuildTransformMock).toHaveBeenCalledWith(
NX_PACKAGE_TRANSFORM.provide
);
expect(result.value).toEqual({ success: true });
expect(result.done).toBe(true);
});
it('should not set up incremental builds when tsConfig option is not set', async () => {
(
buildableLibsUtils.checkDependentProjectsHaveBeenBuilt as jest.Mock
).mockReturnValue(true);
const result = await packageExecutor(options, context).next();
expect(buildableLibsUtils.createTmpTsConfig).not.toHaveBeenCalled();
expect(ngPackagrWithTsConfigMock).not.toHaveBeenCalled();
expect(ngPackagrBuildMock).toHaveBeenCalled();
expect(result.value).toEqual({ success: true });
expect(result.done).toBe(true);
});
it('should process tsConfig for incremental builds when tsConfig option is set', async () => {
(
buildableLibsUtils.checkDependentProjectsHaveBeenBuilt as jest.Mock
).mockReturnValue(true);
const generatedTsConfig = '/root/tmp/my-lib/tsconfig.app.generated.json';
(buildableLibsUtils.createTmpTsConfig as jest.Mock).mockImplementation(
() => generatedTsConfig
);
const result = await packageExecutor(
{ ...options, tsConfig: '/root/my-lib/tsconfig.app.json' },
context
).next();
expect(buildableLibsUtils.createTmpTsConfig).toHaveBeenCalled();
expect(ngPackagrWithTsConfigMock).toHaveBeenCalledWith(generatedTsConfig);
expect(ngPackagrBuildMock).toHaveBeenCalled();
expect(result.value).toEqual({ success: true });
expect(result.done).toBe(true);
});
describe('--watch', () => {
it('should emit results everytime there are changes', async () => {
(
buildableLibsUtils.checkDependentProjectsHaveBeenBuilt as jest.Mock
).mockReturnValue(true);
const results = packageExecutor({ ...options, watch: true }, context);
ngPackagerWatchSubject.next();
let changes = 0;
for await (const result of results) {
changes++;
expect(result).toEqual({ success: true });
if (changes === 2) {
ngPackagerWatchSubject.complete();
} else {
ngPackagerWatchSubject.next();
}
}
expect(ngPackagrWatchMock).toHaveBeenCalledTimes(1);
});
});
});