-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
build.impl.spec.ts
142 lines (127 loc) · 3.94 KB
/
build.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
import { join } from 'path';
import { workspaces } from '@angular-devkit/core';
import { of } from 'rxjs';
import * as buildWebpack from '@angular-devkit/build-webpack';
jest.mock('tsconfig-paths-webpack-plugin');
import TsConfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import { MockBuilderContext } from '@nrwl/workspace/testing';
import { run, WebBuildBuilderOptions } from './build.impl';
import * as webConfigUtils from '../../utils/web.config';
import { getMockContext } from '../../utils/testing';
import * as indexHtmlUtils from '../../utils/third-party/cli-files/utilities/index-file/write-index-html';
describe('WebBuildBuilder', () => {
let context: MockBuilderContext;
let testOptions: WebBuildBuilderOptions;
let runWebpack: jasmine.Spy;
let writeIndexHtml: jasmine.Spy;
beforeEach(async () => {
context = await getMockContext();
testOptions = {
index: 'apps/webapp/src/index.html',
budgets: [],
baseHref: '/',
optimization: true,
deployUrl: '/',
scripts: ['apps/webapp/src/scripts.js'],
styles: ['apps/webapp/src/styles.css'],
main: 'apps/webapp/src/main.ts',
tsConfig: 'apps/webapp/tsconfig.app.json',
outputPath: 'dist/apps/webapp',
fileReplacements: [
{
replace: 'apps/webapp/environment/environment.ts',
with: 'apps/webapp/environment/environment.prod.ts'
},
{
replace: 'module1.ts',
with: 'module2.ts'
}
],
assets: [],
statsJson: false
};
const stats = {
stats: 'stats'
};
runWebpack = spyOn(buildWebpack, 'runWebpack').and.callFake(
(config, context, opts) => {
opts.logging({
toJson: () => stats,
toString: () => JSON.stringify(stats)
});
return of({
success: true,
emittedFiles: [
{
file: 'scripts.js',
extension: '.js'
},
{
file: 'styles.css',
extension: '.css'
}
]
});
}
);
spyOn(workspaces, 'readWorkspace').and.returnValue({
workspace: {
projects: {
get: () => ({
sourceRoot: join(__dirname, '../../..')
})
}
}
});
spyOn(webConfigUtils, 'getWebConfig').and.returnValue({
config: 'config'
});
writeIndexHtml = spyOn(indexHtmlUtils, 'writeIndexHtml').and.returnValue(
of(null)
);
(<any>TsConfigPathsPlugin).mockImplementation(
function MockPathsPlugin() {}
);
});
describe('run', () => {
it('should call runWebpack', async () => {
await run(testOptions, context).toPromise();
expect(runWebpack).toHaveBeenCalled();
});
it('should emit success', async () => {
const buildEvent = await run(testOptions, context).toPromise();
expect(buildEvent.success).toEqual(true);
});
it('should write the HTML', async () => {
await run(testOptions, context).toPromise();
expect(writeIndexHtml).toHaveBeenCalled();
});
describe('differential loading', () => {
it('should call runWebpack twice', async () => {
await run(testOptions, context).toPromise();
expect(runWebpack).toHaveBeenCalledTimes(2);
});
});
describe('webpackConfig option', () => {
it('should require the specified function and use the return value', async () => {
const mockFunction = jest.fn(config => ({
config: 'config'
}));
jest.mock('/root/apps/webapp/webpack.config.js', () => mockFunction, {
virtual: true
});
await run(
{
...testOptions,
webpackConfig: './apps/webapp/webpack.config.js'
},
context
).toPromise();
expect(mockFunction).toHaveBeenCalled();
expect(runWebpack.calls.first().args[0]).toEqual({
config: 'config'
});
});
});
});
});