Skip to content

Commit 1abd173

Browse files
alan-agius4dgp1130
authored andcommitted
test(@angular-devkit/build-angular): refactor bundle budgets option test to use new test harness
With this change we replace the bundle budgets specs to use the new test harness.
1 parent 9300545 commit 1abd173

File tree

2 files changed

+185
-196
lines changed

2 files changed

+185
-196
lines changed

packages/angular_devkit/build_angular/src/builders/browser/specs/bundle-budgets_spec.ts

Lines changed: 0 additions & 196 deletions
This file was deleted.
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { logging } from '@angular-devkit/core';
10+
import { buildWebpackBrowser } from '../../index';
11+
import { Type } from '../../schema';
12+
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
13+
14+
describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
15+
const CSS_EXTENSIONS = ['css', 'scss', 'less', 'styl'];
16+
const BUDGET_NOT_MET_REGEXP = /Budget .+ was not met by/;
17+
18+
describe('Option: "bundleBudgets"', () => {
19+
it(`should not warn when size is below threshold`, async () => {
20+
harness.useTarget('build', {
21+
...BASE_OPTIONS,
22+
optimization: true,
23+
budgets: [{ type: Type.All, maximumWarning: '100mb' }],
24+
});
25+
26+
const { result, logs } = await harness.executeOnce();
27+
expect(result?.success).toBe(true);
28+
expect(logs).not.toContain(
29+
jasmine.objectContaining<logging.LogEntry>({
30+
level: 'warn',
31+
message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP),
32+
}),
33+
);
34+
});
35+
36+
it(`should error when size is above 'maximumError' threshold`, async () => {
37+
harness.useTarget('build', {
38+
...BASE_OPTIONS,
39+
optimization: true,
40+
budgets: [{ type: Type.All, maximumError: '100b' }],
41+
});
42+
43+
const { result, logs } = await harness.executeOnce();
44+
expect(result?.success).toBe(false);
45+
expect(logs).toContain(
46+
jasmine.objectContaining<logging.LogEntry>({
47+
level: 'error',
48+
message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP),
49+
}),
50+
);
51+
});
52+
53+
it(`should warn when size is above 'maximumWarning' threshold`, async () => {
54+
harness.useTarget('build', {
55+
...BASE_OPTIONS,
56+
optimization: true,
57+
budgets: [{ type: Type.All, maximumWarning: '100b' }],
58+
});
59+
60+
const { result, logs } = await harness.executeOnce();
61+
expect(result?.success).toBe(true);
62+
expect(logs).toContain(
63+
jasmine.objectContaining<logging.LogEntry>({
64+
level: 'warn',
65+
message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP),
66+
}),
67+
);
68+
});
69+
70+
CSS_EXTENSIONS.forEach((ext) => {
71+
it(`shows warnings for large component ${ext} when using 'anyComponentStyle' when AOT`, async () => {
72+
const cssContent = `
73+
.foo { color: white; padding: 1px; }
74+
.buz { color: white; padding: 2px; }
75+
.bar { color: white; padding: 3px; }
76+
`;
77+
78+
await harness.writeFiles({
79+
[`src/app/app.component.${ext}`]: cssContent,
80+
[`src/assets/foo.${ext}`]: cssContent,
81+
[`src/styles.${ext}`]: cssContent,
82+
});
83+
84+
await harness.modifyFile('src/app/app.component.ts', (content) =>
85+
content.replace('app.component.css', `app.component.${ext}`),
86+
);
87+
88+
harness.useTarget('build', {
89+
...BASE_OPTIONS,
90+
optimization: true,
91+
aot: true,
92+
styles: [`src/styles.${ext}`],
93+
budgets: [{ type: Type.AnyComponentStyle, maximumWarning: '1b' }],
94+
});
95+
96+
const { result, logs } = await harness.executeOnce();
97+
expect(result?.success).toBe(true);
98+
expect(logs).toContain(
99+
jasmine.objectContaining<logging.LogEntry>({
100+
level: 'warn',
101+
message: jasmine.stringMatching(new RegExp(`Warning.+app.component.${ext}`)),
102+
}),
103+
);
104+
});
105+
});
106+
107+
describe(`should ignore '.map' files`, () => {
108+
it(`when 'bundle' budget`, async () => {
109+
harness.useTarget('build', {
110+
...BASE_OPTIONS,
111+
sourceMap: true,
112+
optimization: true,
113+
extractLicenses: true,
114+
budgets: [{ type: Type.Bundle, name: 'main', maximumError: '1mb' }],
115+
});
116+
117+
const { result, logs } = await harness.executeOnce();
118+
expect(result?.success).toBe(true);
119+
expect(logs).not.toContain(
120+
jasmine.objectContaining<logging.LogEntry>({
121+
level: 'error',
122+
message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP),
123+
}),
124+
);
125+
});
126+
127+
it(`when 'intial' budget`, async () => {
128+
harness.useTarget('build', {
129+
...BASE_OPTIONS,
130+
sourceMap: true,
131+
optimization: true,
132+
extractLicenses: true,
133+
budgets: [{ type: Type.Initial, name: 'main', maximumError: '1mb' }],
134+
});
135+
136+
const { result, logs } = await harness.executeOnce();
137+
expect(result?.success).toBe(true);
138+
expect(logs).not.toContain(
139+
jasmine.objectContaining<logging.LogEntry>({
140+
level: 'error',
141+
message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP),
142+
}),
143+
);
144+
});
145+
146+
it(`when 'all' budget`, async () => {
147+
harness.useTarget('build', {
148+
...BASE_OPTIONS,
149+
sourceMap: true,
150+
optimization: true,
151+
extractLicenses: true,
152+
budgets: [{ type: Type.All, maximumError: '1mb' }],
153+
});
154+
155+
const { result, logs } = await harness.executeOnce();
156+
expect(result?.success).toBe(true);
157+
expect(logs).not.toContain(
158+
jasmine.objectContaining<logging.LogEntry>({
159+
level: 'error',
160+
message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP),
161+
}),
162+
);
163+
});
164+
165+
it(`when 'any' budget`, async () => {
166+
harness.useTarget('build', {
167+
...BASE_OPTIONS,
168+
sourceMap: true,
169+
optimization: true,
170+
extractLicenses: true,
171+
budgets: [{ type: Type.Any, maximumError: '1mb' }],
172+
});
173+
174+
const { result, logs } = await harness.executeOnce();
175+
expect(result?.success).toBe(true);
176+
expect(logs).not.toContain(
177+
jasmine.objectContaining<logging.LogEntry>({
178+
level: 'error',
179+
message: jasmine.stringMatching(BUDGET_NOT_MET_REGEXP),
180+
}),
181+
);
182+
});
183+
});
184+
});
185+
});

0 commit comments

Comments
 (0)