Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1a5c592

Browse files
alan-agius4filipesilva
authored andcommittedJul 12, 2021
test(@angular-devkit/build-angular): refactor server builder tests to use test harness
(cherry picked from commit 54c170b)
1 parent f6ab2e3 commit 1a5c592

File tree

8 files changed

+324
-295
lines changed

8 files changed

+324
-295
lines changed
 

‎packages/angular_devkit/build_angular/src/server/base_spec.ts

Lines changed: 0 additions & 176 deletions
This file was deleted.

‎packages/angular_devkit/build_angular/src/server/external_dependencies_spec.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎packages/angular_devkit/build_angular/src/server/resources-output-path_spec.ts

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 { execute } from '../../index';
10+
import { BASE_OPTIONS, SERVER_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(execute, SERVER_BUILDER_INFO, (harness) => {
13+
describe('Behavior: "Errors"', () => {
14+
it('should not try to resolve web-workers', async () => {
15+
harness.useTarget('test', {
16+
...BASE_OPTIONS,
17+
});
18+
19+
await harness.writeFiles({
20+
'src/app/app.worker.ts': `
21+
/// <reference lib="webworker" />
22+
23+
const foo: string = 'hello world';
24+
addEventListener('message', ({ data }) => {
25+
postMessage(foo);
26+
});
27+
`,
28+
'src/main.server.ts': `
29+
if (typeof Worker !== 'undefined') {
30+
const worker = new Worker(new URL('./app/app.worker', import.meta.url), { type: 'module' });
31+
worker.onmessage = ({ data }) => {
32+
console.log('page got message:', data);
33+
};
34+
worker.postMessage('hello');
35+
}
36+
`,
37+
});
38+
39+
const { result } = await harness.executeOnce();
40+
41+
expect(result?.success).toBeTrue();
42+
});
43+
});
44+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 { execute } from '../../index';
10+
import { BASE_OPTIONS, SERVER_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(execute, SERVER_BUILDER_INFO, (harness) => {
13+
describe('Option: "externalDependencies"', () => {
14+
it(`should not bundle dependency when set "externalDependencies" is set.`, async () => {
15+
harness.useTarget('server', {
16+
...BASE_OPTIONS,
17+
externalDependencies: ['@angular/core'],
18+
});
19+
20+
const { result } = await harness.executeOnce();
21+
expect(result?.success).toBe(true);
22+
23+
harness.expectFile('dist/main.js').content.toContain('require("@angular/core")');
24+
harness.expectFile('dist/main.js').content.not.toContain('require("@angular/common")');
25+
});
26+
27+
it(`should bundle all dependencies when "externalDependencies" is unset`, async () => {
28+
harness.useTarget('server', {
29+
...BASE_OPTIONS,
30+
});
31+
32+
const { result } = await harness.executeOnce();
33+
expect(result?.success).toBe(true);
34+
harness.expectFile('dist/main.js').content.not.toContain('require("@angular/core")');
35+
harness.expectFile('dist/main.js').content.not.toContain('require("@angular/common")');
36+
});
37+
});
38+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 { execute } from '../../index';
10+
import { BASE_OPTIONS, SERVER_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(execute, SERVER_BUILDER_INFO, (harness) => {
13+
describe('Option: "resourcesOutputPath"', () => {
14+
beforeEach(async () => {
15+
const img = await harness.readFile('src/spectrum.png');
16+
17+
await harness.writeFiles({
18+
'src/assets/component-img-relative.png': img,
19+
'src/assets/component-img-absolute.png': img,
20+
'src/app/app.component.css': `
21+
h3 { background: url('/assets/component-img-absolute.png'); }
22+
h4 { background: url('../assets/component-img-relative.png'); }
23+
`,
24+
});
25+
});
26+
27+
it(`should prepend value of "resourcesOutputPath" as part of the resource urls`, async () => {
28+
harness.useTarget('server', {
29+
...BASE_OPTIONS,
30+
resourcesOutputPath: 'out-assets',
31+
});
32+
33+
const { result } = await harness.executeOnce();
34+
expect(result?.success).toBe(true);
35+
36+
harness
37+
.expectFile('dist/main.js')
38+
.content.toContain(`url(/assets/component-img-absolute.png)`);
39+
harness
40+
.expectFile('dist/main.js')
41+
.content.toContain(`url(out-assets/component-img-relative.png)`);
42+
43+
// Assets are not emitted during a server builds.
44+
harness.expectFile('dist/out-assets/component-img-relative.png').toNotExist();
45+
});
46+
47+
it(`should not prepend anything when value of "resourcesOutputPath" is unset.`, async () => {
48+
harness.useTarget('server', {
49+
...BASE_OPTIONS,
50+
});
51+
52+
const { result } = await harness.executeOnce();
53+
expect(result?.success).toBe(true);
54+
55+
harness
56+
.expectFile('dist/main.js')
57+
.content.toContain(`url(/assets/component-img-absolute.png)`);
58+
harness.expectFile('dist/main.js').content.toContain(`url(component-img-relative.png)`);
59+
60+
// Assets are not emitted during a server builds.
61+
harness.expectFile('dist/component-img-relative.png').toNotExist();
62+
});
63+
});
64+
});
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 { execute } from '../../index';
10+
import { BASE_OPTIONS, SERVER_BUILDER_INFO, describeBuilder } from '../setup';
11+
12+
describeBuilder(execute, SERVER_BUILDER_INFO, (harness) => {
13+
describe('Option: "sourceMap"', () => {
14+
const INLINE_SOURCEMAP_MARKER = '/*# sourceMappingURL=data:application/json;base64,';
15+
16+
beforeEach(async () => {
17+
await harness.writeFiles({
18+
'src/app/app.component.css': `p { color: red; }`,
19+
});
20+
});
21+
22+
it(`should not generate sourceMaps when "sourceMap" option is unset`, async () => {
23+
harness.useTarget('server', {
24+
...BASE_OPTIONS,
25+
});
26+
27+
const { result } = await harness.executeOnce();
28+
expect(result?.success).toBe(true);
29+
harness.expectFile('dist/main.js.map').toNotExist();
30+
});
31+
32+
it(`should generate sourceMaps when "sourceMap" option is true`, async () => {
33+
harness.useTarget('server', {
34+
...BASE_OPTIONS,
35+
sourceMap: true,
36+
});
37+
38+
const { result } = await harness.executeOnce();
39+
expect(result?.success).toBe(true);
40+
harness.expectFile('dist/main.js.map').toExist();
41+
});
42+
43+
it(`should not generate sourceMaps when "sourceMap" option is false`, async () => {
44+
harness.useTarget('server', {
45+
...BASE_OPTIONS,
46+
sourceMap: false,
47+
});
48+
49+
const { result } = await harness.executeOnce();
50+
expect(result?.success).toBe(true);
51+
harness.expectFile('dist/main.js.map').toNotExist();
52+
});
53+
54+
it(`should not generate components sourceMaps when "styles" option is false`, async () => {
55+
harness.useTarget('server', {
56+
...BASE_OPTIONS,
57+
// Components sourcemaps are only present when optimization is false.
58+
optimization: false,
59+
sourceMap: {
60+
styles: false,
61+
scripts: true,
62+
},
63+
});
64+
65+
const { result } = await harness.executeOnce();
66+
expect(result?.success).toBe(true);
67+
harness.expectFile('dist/main.js.map').toExist();
68+
harness.expectFile('dist/main.js').content.toContain('sourceMappingURL=main.js.map');
69+
console.log(await harness.readFile('dist/main.js'));
70+
harness.expectFile('dist/main.js').content.not.toContain(INLINE_SOURCEMAP_MARKER);
71+
});
72+
73+
it(`should generate components sourceMaps when "styles" option is true`, async () => {
74+
harness.useTarget('server', {
75+
...BASE_OPTIONS,
76+
// Components sourcemaps are only present when optimization is false.
77+
optimization: false,
78+
sourceMap: {
79+
styles: true,
80+
scripts: true,
81+
},
82+
});
83+
84+
const { result } = await harness.executeOnce();
85+
expect(result?.success).toBe(true);
86+
harness.expectFile('dist/main.js.map').toExist();
87+
harness.expectFile('dist/main.js').content.toContain('sourceMappingURL=main.js.map');
88+
harness
89+
.expectFile('dist/main.js')
90+
.content.toContain('sourceMappingURL=data:application/json');
91+
});
92+
93+
it(`should generate components sourceMaps when "styles" option is unset`, async () => {
94+
harness.useTarget('server', {
95+
...BASE_OPTIONS,
96+
// Components sourcemaps are only present when optimization is false.
97+
optimization: false,
98+
sourceMap: {
99+
scripts: true,
100+
},
101+
});
102+
103+
const { result } = await harness.executeOnce();
104+
expect(result?.success).toBe(true);
105+
harness.expectFile('dist/main.js.map').toExist();
106+
harness.expectFile('dist/main.js').content.toContain('sourceMappingURL=main.js.map');
107+
harness.expectFile('dist/main.js').content.toContain(INLINE_SOURCEMAP_MARKER);
108+
});
109+
110+
it(`should generate scripts sourceMaps when "scripts" option is unset`, async () => {
111+
harness.useTarget('server', {
112+
...BASE_OPTIONS,
113+
sourceMap: {},
114+
});
115+
116+
const { result } = await harness.executeOnce();
117+
expect(result?.success).toBe(true);
118+
harness.expectFile('dist/main.js.map').toExist();
119+
harness.expectFile('dist/main.js').content.toContain('sourceMappingURL=main.js.map');
120+
});
121+
122+
it(`should not generate scripts sourceMaps when "scripts" option is false`, async () => {
123+
harness.useTarget('server', {
124+
...BASE_OPTIONS,
125+
sourceMap: {
126+
scripts: false,
127+
},
128+
});
129+
130+
const { result } = await harness.executeOnce();
131+
expect(result?.success).toBe(true);
132+
harness.expectFile('dist/main.js.map').toNotExist();
133+
harness.expectFile('dist/main.js').content.not.toContain('sourceMappingURL=main.js.map');
134+
});
135+
136+
it(`should not generate scripts sourceMaps when "scripts" option is true`, async () => {
137+
harness.useTarget('server', {
138+
...BASE_OPTIONS,
139+
sourceMap: {
140+
scripts: true,
141+
},
142+
});
143+
144+
const { result } = await harness.executeOnce();
145+
expect(result?.success).toBe(true);
146+
harness.expectFile('dist/main.js.map').toExist();
147+
harness.expectFile('dist/main.js').content.toContain('sourceMappingURL=main.js.map');
148+
});
149+
});
150+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 { Schema } from '../schema';
10+
11+
export { describeBuilder } from '../../testing';
12+
13+
export const SERVER_BUILDER_INFO = Object.freeze({
14+
name: '@angular-devkit/build-angular:server',
15+
schemaPath: __dirname + '/../schema.json',
16+
});
17+
18+
/**
19+
* Contains all required Server builder fields.
20+
* Also disables progress reporting to minimize logging output.
21+
*/
22+
export const BASE_OPTIONS = Object.freeze<Schema>({
23+
main: 'src/main.server.ts',
24+
tsConfig: 'src/tsconfig.server.json',
25+
progress: false,
26+
watch: false,
27+
outputPath: 'dist',
28+
});

0 commit comments

Comments
 (0)
Please sign in to comment.