Skip to content

Commit f916960

Browse files
clydinangular-robot[bot]
authored andcommitted
test(@angular-devkit/build-angular): port additional unit tests to esbuild builder
The following unit tests have been ported over to test the experimental esbuild-based browser application builder: * `extractLicenses` option * `main` option * `optimization.styles.inlineCritical` option * `styles` option * `subresourceIntegrity` option Several individual tests involving file output logging have been temporarily disabled until build and file output logging has been implemented for the builder.
1 parent 1e52863 commit f916960

File tree

5 files changed

+758
-0
lines changed

5 files changed

+758
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 { buildEsbuildBrowser } from '../../index';
10+
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
13+
describe('Option: "extractLicenses"', () => {
14+
it(`should generate '3rdpartylicenses.txt' when 'extractLicenses' is true`, async () => {
15+
harness.useTarget('build', {
16+
...BASE_OPTIONS,
17+
extractLicenses: true,
18+
});
19+
20+
const { result } = await harness.executeOnce();
21+
expect(result?.success).toBe(true);
22+
harness.expectFile('dist/3rdpartylicenses.txt').content.toContain('MIT');
23+
});
24+
25+
it(`should not generate '3rdpartylicenses.txt' when 'extractLicenses' is false`, async () => {
26+
harness.useTarget('build', {
27+
...BASE_OPTIONS,
28+
extractLicenses: false,
29+
});
30+
31+
const { result } = await harness.executeOnce();
32+
expect(result?.success).toBe(true);
33+
harness.expectFile('dist/3rdpartylicenses.txt').toNotExist();
34+
});
35+
36+
it(`should generate '3rdpartylicenses.txt' when 'extractLicenses' is not set`, async () => {
37+
harness.useTarget('build', {
38+
...BASE_OPTIONS,
39+
});
40+
41+
const { result } = await harness.executeOnce();
42+
expect(result?.success).toBe(true);
43+
harness.expectFile('dist/3rdpartylicenses.txt').content.toContain('MIT');
44+
});
45+
});
46+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 { buildEsbuildBrowser } from '../../index';
10+
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
13+
describe('Option: "inlineCritical"', () => {
14+
beforeEach(async () => {
15+
await harness.writeFile('src/styles.css', 'body { color: #000 }');
16+
});
17+
18+
it(`should extract critical css when 'inlineCritical' is true`, async () => {
19+
harness.useTarget('build', {
20+
...BASE_OPTIONS,
21+
optimization: {
22+
scripts: false,
23+
styles: {
24+
minify: true,
25+
inlineCritical: true,
26+
},
27+
fonts: false,
28+
},
29+
styles: ['src/styles.css'],
30+
});
31+
32+
const { result } = await harness.executeOnce();
33+
34+
expect(result?.success).toBe(true);
35+
harness
36+
.expectFile('dist/index.html')
37+
.content.toContain(
38+
`<link rel="stylesheet" href="styles.css" media="print" onload="this.media='all'">`,
39+
);
40+
harness.expectFile('dist/index.html').content.toContain(`body{color:#000}`);
41+
});
42+
43+
it(`should extract critical css when 'optimization' is unset`, async () => {
44+
harness.useTarget('build', {
45+
...BASE_OPTIONS,
46+
styles: ['src/styles.css'],
47+
optimization: undefined,
48+
});
49+
50+
const { result } = await harness.executeOnce();
51+
52+
expect(result?.success).toBe(true);
53+
harness
54+
.expectFile('dist/index.html')
55+
.content.toContain(
56+
`<link rel="stylesheet" href="styles.css" media="print" onload="this.media='all'">`,
57+
);
58+
harness.expectFile('dist/index.html').content.toContain(`body{color:#000}`);
59+
});
60+
61+
it(`should extract critical css when 'optimization' is true`, async () => {
62+
harness.useTarget('build', {
63+
...BASE_OPTIONS,
64+
styles: ['src/styles.css'],
65+
optimization: true,
66+
});
67+
68+
const { result } = await harness.executeOnce();
69+
70+
expect(result?.success).toBe(true);
71+
harness
72+
.expectFile('dist/index.html')
73+
.content.toContain(
74+
`<link rel="stylesheet" href="styles.css" media="print" onload="this.media='all'">`,
75+
);
76+
harness.expectFile('dist/index.html').content.toContain(`body{color:#000}`);
77+
});
78+
79+
it(`should not extract critical css when 'optimization' is false`, async () => {
80+
harness.useTarget('build', {
81+
...BASE_OPTIONS,
82+
styles: ['src/styles.css'],
83+
optimization: false,
84+
});
85+
86+
const { result } = await harness.executeOnce();
87+
88+
expect(result?.success).toBe(true);
89+
harness.expectFile('dist/index.html').content.not.toContain(`<style`);
90+
});
91+
92+
it(`should not extract critical css when 'inlineCritical' is false`, async () => {
93+
harness.useTarget('build', {
94+
...BASE_OPTIONS,
95+
styles: ['src/styles.css'],
96+
optimization: {
97+
scripts: false,
98+
styles: {
99+
minify: false,
100+
inlineCritical: false,
101+
},
102+
fonts: false,
103+
},
104+
});
105+
106+
const { result } = await harness.executeOnce();
107+
108+
expect(result?.success).toBe(true);
109+
harness.expectFile('dist/index.html').content.not.toContain(`<style`);
110+
});
111+
112+
it(`should extract critical css when using '@media all {}' and 'minify' is set to true`, async () => {
113+
harness.useTarget('build', {
114+
...BASE_OPTIONS,
115+
styles: ['src/styles.css'],
116+
optimization: {
117+
scripts: false,
118+
styles: {
119+
minify: true,
120+
inlineCritical: true,
121+
},
122+
fonts: false,
123+
},
124+
});
125+
126+
await harness.writeFile('src/styles.css', '@media all { body { color: #000 } }');
127+
128+
const { result } = await harness.executeOnce();
129+
expect(result?.success).toBe(true);
130+
harness
131+
.expectFile('dist/index.html')
132+
.content.toContain(
133+
`<link rel="stylesheet" href="styles.css" media="print" onload="this.media='all'">`,
134+
);
135+
harness.expectFile('dist/index.html').content.toContain(`body{color:#000}`);
136+
});
137+
});
138+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 { buildEsbuildBrowser } from '../../index';
10+
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => {
13+
describe('Option: "main"', () => {
14+
it('uses a provided TypeScript file', async () => {
15+
harness.useTarget('build', {
16+
...BASE_OPTIONS,
17+
main: 'src/main.ts',
18+
});
19+
20+
const { result } = await harness.executeOnce();
21+
22+
expect(result?.success).toBe(true);
23+
24+
harness.expectFile('dist/main.js').toExist();
25+
harness.expectFile('dist/index.html').toExist();
26+
});
27+
28+
it('uses a provided JavaScript file', async () => {
29+
await harness.writeFile('src/main.js', `console.log('main');`);
30+
31+
harness.useTarget('build', {
32+
...BASE_OPTIONS,
33+
main: 'src/main.js',
34+
});
35+
36+
const { result } = await harness.executeOnce();
37+
38+
expect(result?.success).toBe(true);
39+
40+
harness.expectFile('dist/main.js').toExist();
41+
harness.expectFile('dist/index.html').toExist();
42+
43+
harness.expectFile('dist/main.js').content.toContain('console.log("main")');
44+
});
45+
46+
it('fails and shows an error when file does not exist', async () => {
47+
harness.useTarget('build', {
48+
...BASE_OPTIONS,
49+
main: 'src/missing.ts',
50+
});
51+
52+
const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false });
53+
54+
expect(result?.success).toBe(false);
55+
expect(logs).toContain(
56+
jasmine.objectContaining({ message: jasmine.stringMatching('Could not resolve "') }),
57+
);
58+
59+
harness.expectFile('dist/main.js').toNotExist();
60+
harness.expectFile('dist/index.html').toNotExist();
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)