Skip to content

Commit 037a1b8

Browse files
clydinfilipesilva
authored andcommitted
test(@angular-devkit/build-angular): add dev-server builder build inline critical CSS behavior tests
This change adds expanded unit tests for the dev-server builder's build inline critical CSS behavior using the builder test harness.
1 parent a24c212 commit 037a1b8

File tree

3 files changed

+97
-56
lines changed

3 files changed

+97
-56
lines changed

packages/angular_devkit/build_angular/src/dev-server/inline-critical-css-optimization_spec.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. 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+
import fetch from 'node-fetch'; // tslint:disable-line:no-implicit-dependencies
9+
import { mergeMap, take, timeout } from 'rxjs/operators';
10+
import { serveWebpackBrowser } from '../../index';
11+
import {
12+
BASE_OPTIONS,
13+
DEV_SERVER_BUILDER_INFO,
14+
describeBuilder,
15+
setupBrowserTarget,
16+
} from '../setup';
17+
18+
describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
19+
describe('Behavior: "browser builder inline critical css"', () => {
20+
beforeEach(async () => {
21+
setupBrowserTarget(harness, {
22+
optimization: {
23+
styles: {
24+
minify: true,
25+
inlineCritical: true,
26+
},
27+
},
28+
styles: ['src/styles.css'],
29+
});
30+
31+
await harness.writeFiles({
32+
'src/styles.css': 'body { color: #000 }',
33+
});
34+
35+
// Application code is not needed for these tests
36+
await harness.writeFile('src/main.ts', '');
37+
});
38+
39+
it('inlines critical css when enabled in the "browserTarget" options', async () => {
40+
harness.useTarget('serve', {
41+
...BASE_OPTIONS,
42+
});
43+
44+
await harness
45+
.execute()
46+
.pipe(
47+
timeout(39000),
48+
mergeMap(async ({ result }) => {
49+
expect(result?.success).toBeTrue();
50+
51+
if (result?.success) {
52+
const response = await fetch(`${result.baseUrl}index.html`);
53+
expect(await response.text()).toContain(`body{color:#000;}`);
54+
}
55+
}),
56+
take(1),
57+
)
58+
.toPromise();
59+
});
60+
});
61+
});

packages/angular_devkit/build_angular/src/dev-server/tests/setup.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import { json } from '@angular-devkit/core';
9+
import { readFileSync } from 'fs';
810
import { buildWebpackBrowser } from '../../browser';
911
import { Schema as BrowserSchema } from '../../browser/schema';
10-
import { BASE_OPTIONS as BROWSER_BASE_OPTIONS } from '../../browser/tests/setup';
12+
import {
13+
BASE_OPTIONS as BROWSER_BASE_OPTIONS,
14+
BROWSER_BUILDER_INFO,
15+
} from '../../browser/tests/setup';
1116
import { BuilderHarness } from '../../testing/builder-harness';
1217
import { Schema } from '../schema';
1318

@@ -34,12 +39,38 @@ export const BASE_OPTIONS = Object.freeze<Schema>({
3439
*/
3540
export const BUILD_TIMEOUT = 15000;
3641

42+
/**
43+
* Cached browser builder option schema
44+
*/
45+
let browserSchema: json.schema.JsonSchema | undefined = undefined;
46+
47+
/**
48+
* Adds a `build` target to a builder test harness for the browser builder with the base options
49+
* used by the browser builder tests.
50+
*
51+
* @param harness The builder harness to use when setting up the browser builder target
52+
* @param extraOptions The additional options that should be used when executing the target.
53+
*/
3754
export function setupBrowserTarget<T>(
3855
harness: BuilderHarness<T>,
3956
extraOptions?: Partial<BrowserSchema>,
4057
): void {
41-
harness.withBuilderTarget('build', buildWebpackBrowser, {
42-
...BROWSER_BASE_OPTIONS,
43-
...extraOptions,
44-
});
58+
if (!browserSchema) {
59+
browserSchema = JSON.parse(
60+
readFileSync(BROWSER_BUILDER_INFO.schemaPath, 'utf8'),
61+
) as json.schema.JsonSchema;
62+
}
63+
64+
harness.withBuilderTarget(
65+
'build',
66+
buildWebpackBrowser,
67+
{
68+
...BROWSER_BASE_OPTIONS,
69+
...extraOptions,
70+
},
71+
{
72+
builderName: BROWSER_BUILDER_INFO.name,
73+
optionSchema: browserSchema,
74+
},
75+
);
4576
}

0 commit comments

Comments
 (0)