Skip to content

Commit eca5dc3

Browse files
committed
test(@angular-devkit/build-angular): move ALL Browser Builder tests
To the new Architect API.
1 parent e8dab63 commit eca5dc3

36 files changed

+3621
-65
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 { Architect } from '@angular-devkit/architect/src/index2';
9+
import { join, normalize, relative, virtualFs } from '@angular-devkit/core';
10+
import { take, tap } from 'rxjs/operators';
11+
import { BrowserBuilderOutput } from "../../src/browser/index2";
12+
import { createArchitect, host } from '../utils';
13+
14+
15+
describe('Browser Builder allow js', () => {
16+
const targetSpec = { project: 'app', target: 'build' };
17+
let architect: Architect;
18+
19+
beforeEach(async () => {
20+
await host.initialize().toPromise();
21+
architect = (await createArchitect(host.root())).architect;
22+
});
23+
afterEach(async () => host.restore().toPromise());
24+
25+
it('works', async () => {
26+
host.writeMultipleFiles({
27+
'src/my-js-file.js': `console.log(1); export const a = 2;`,
28+
'src/main.ts': `import { a } from './my-js-file'; console.log(a);`,
29+
});
30+
31+
host.replaceInFile(
32+
'tsconfig.json',
33+
'"target": "es5"',
34+
'"target": "es5", "allowJs": true',
35+
);
36+
37+
const run = await architect.scheduleTarget(targetSpec);
38+
const output = await run.result as BrowserBuilderOutput;
39+
expect(output.success).toBe(true);
40+
41+
const content = virtualFs.fileBufferToString(
42+
await host.read(join(normalize(output.outputPath), 'main.js')).toPromise(),
43+
);
44+
45+
expect(content).toContain('var a = 2');
46+
47+
await run.stop();
48+
});
49+
50+
it('works with aot', async () => {
51+
host.writeMultipleFiles({
52+
'src/my-js-file.js': `console.log(1); export const a = 2;`,
53+
'src/main.ts': `import { a } from './my-js-file'; console.log(a);`,
54+
});
55+
56+
host.replaceInFile(
57+
'tsconfig.json',
58+
'"target": "es5"',
59+
'"target": "es5", "allowJs": true',
60+
);
61+
62+
const overrides = { aot: true };
63+
64+
const run = await architect.scheduleTarget(targetSpec, overrides);
65+
const output = await run.result as BrowserBuilderOutput;
66+
expect(output.success).toBe(true);
67+
68+
const content = virtualFs.fileBufferToString(
69+
await host.read(join(normalize(output.outputPath), 'main.js')).toPromise(),
70+
);
71+
72+
expect(content).toContain('var a = 2');
73+
74+
await run.stop();
75+
});
76+
77+
it('works with watch', async () => {
78+
host.writeMultipleFiles({
79+
'src/my-js-file.js': `console.log(1); export const a = 2;`,
80+
'src/main.ts': `import { a } from './my-js-file'; console.log(a);`,
81+
});
82+
83+
host.replaceInFile(
84+
'tsconfig.json',
85+
'"target": "es5"',
86+
'"target": "es5", "allowJs": true',
87+
);
88+
89+
const overrides = { watch: true };
90+
91+
let buildCount = 1;
92+
const run = await architect.scheduleTarget(targetSpec, overrides);
93+
94+
await run.output.pipe(
95+
tap((output: BrowserBuilderOutput) => {
96+
const path = relative(host.root(), join(normalize(output.outputPath), 'main.js'));
97+
const content = virtualFs.fileBufferToString(
98+
host.scopedSync().read(path),
99+
);
100+
101+
switch (buildCount) {
102+
case 1:
103+
expect(content).toContain('var a = 2');
104+
host.writeMultipleFiles({
105+
'src/my-js-file.js': `console.log(1); export const a = 1;`,
106+
});
107+
break;
108+
case 2:
109+
expect(content).toContain('var a = 1');
110+
break;
111+
}
112+
113+
buildCount++;
114+
}),
115+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
116+
take(2),
117+
).toPromise();
118+
119+
await run.stop();
120+
});
121+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
9+
import { Architect } from '@angular-devkit/architect/src/index2';
10+
import { runTargetSpec } from '@angular-devkit/architect/testing';
11+
import { join, normalize, virtualFs } from '@angular-devkit/core';
12+
import { tap } from 'rxjs/operators';
13+
import { BrowserBuilderOutput } from "../../src/browser/index2";
14+
import { browserTargetSpec, createArchitect, host } from '../utils';
15+
16+
17+
describe('Browser Builder AOT', () => {
18+
const targetSpec = { project: 'app', target: 'build' };
19+
let architect: Architect;
20+
21+
beforeEach(async () => {
22+
await host.initialize().toPromise();
23+
architect = (await createArchitect(host.root())).architect;
24+
});
25+
afterEach(async () => host.restore().toPromise());
26+
27+
it('works', async () => {
28+
const overrides = { aot: true };
29+
30+
const run = await architect.scheduleTarget(targetSpec, overrides);
31+
const output = await run.result as BrowserBuilderOutput;
32+
33+
expect(output.success).toBe(true);
34+
35+
const fileName = join(normalize(output.outputPath), 'main.js');
36+
const content = virtualFs.fileBufferToString(await host.read(normalize(fileName)).toPromise());
37+
expect(content).toMatch(/platformBrowser.*bootstrapModuleFactory.*AppModuleNgFactory/);
38+
39+
await run.stop();
40+
});
41+
});
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
9+
import { Architect } from '@angular-devkit/architect/src/index2';
10+
import { normalize, virtualFs } from '@angular-devkit/core';
11+
import { toArray } from 'rxjs/operators';
12+
import { BrowserBuilderOutput } from "../../src/browser/index2";
13+
import { createArchitect, host } from '../utils';
14+
15+
16+
describe('Browser Builder assets', () => {
17+
const targetSpec = { project: 'app', target: 'build' };
18+
let architect: Architect;
19+
20+
beforeEach(async () => {
21+
await host.initialize().toPromise();
22+
architect = (await createArchitect(host.root())).architect;
23+
});
24+
afterEach(async () => host.restore().toPromise());
25+
26+
it('works', async () => {
27+
const assets: { [path: string]: string } = {
28+
'./src/folder/.gitkeep': '',
29+
'./src/string-file-asset.txt': 'string-file-asset.txt',
30+
'./src/string-folder-asset/file.txt': 'string-folder-asset.txt',
31+
'./src/glob-asset.txt': 'glob-asset.txt',
32+
'./src/folder/folder-asset.txt': 'folder-asset.txt',
33+
'./src/output-asset.txt': 'output-asset.txt',
34+
};
35+
const matches: { [path: string]: string } = {
36+
'./dist/string-file-asset.txt': 'string-file-asset.txt',
37+
'./dist/string-folder-asset/file.txt': 'string-folder-asset.txt',
38+
'./dist/glob-asset.txt': 'glob-asset.txt',
39+
'./dist/folder/folder-asset.txt': 'folder-asset.txt',
40+
'./dist/output-folder/output-asset.txt': 'output-asset.txt',
41+
};
42+
host.writeMultipleFiles(assets);
43+
44+
const overrides = {
45+
assets: [
46+
'src/string-file-asset.txt',
47+
'src/string-folder-asset',
48+
{ glob: 'glob-asset.txt', input: 'src/', output: '/' },
49+
{ glob: 'glob-asset.txt', input: 'src/', output: '/' },
50+
{ glob: 'output-asset.txt', input: 'src/', output: '/output-folder' },
51+
{ glob: '**/*', input: 'src/folder', output: '/folder' },
52+
],
53+
};
54+
55+
const run = await architect.scheduleTarget(targetSpec, overrides);
56+
const output = await run.result as BrowserBuilderOutput;
57+
expect(output.success).toBe(true);
58+
59+
// Assets we expect should be there.
60+
Object.keys(matches).forEach(fileName => {
61+
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
62+
expect(content).toMatch(matches[fileName]);
63+
});
64+
// .gitkeep should not be there.
65+
expect(host.scopedSync().exists(normalize('./dist/folder/.gitkeep'))).toBe(false);
66+
67+
await run.stop();
68+
});
69+
70+
it('works with ignored patterns', async () => {
71+
const assets: { [path: string]: string } = {
72+
'./src/folder/.gitkeep': '',
73+
'./src/folder/asset-ignored.txt': '',
74+
'./src/folder/asset.txt': '',
75+
};
76+
77+
host.writeMultipleFiles(assets);
78+
79+
const overrides = {
80+
assets: [
81+
{
82+
glob: '**/*',
83+
ignore: ['asset-ignored.txt'],
84+
input: 'src/folder',
85+
output: '/folder',
86+
},
87+
],
88+
};
89+
90+
const run = await architect.scheduleTarget(targetSpec, overrides);
91+
const output = await run.result as BrowserBuilderOutput;
92+
expect(output.success).toBe(true);
93+
94+
expect(host.scopedSync().exists(normalize('./dist/folder/asset.txt'))).toBe(true);
95+
expect(host.scopedSync().exists(normalize('./dist/folder/asset-ignored.txt'))).toBe(false);
96+
expect(host.scopedSync().exists(normalize('./dist/folder/.gitkeep'))).toBe(false);
97+
98+
await run.stop();
99+
});
100+
101+
it('fails with non-absolute output path', async () => {
102+
const assets: { [path: string]: string } = {
103+
'./node_modules/some-package/node_modules-asset.txt': 'node_modules-asset.txt',
104+
};
105+
host.writeMultipleFiles(assets);
106+
const overrides = {
107+
assets: [{
108+
glob: '**/*', input: '../node_modules/some-package/', output: '../temp',
109+
}],
110+
};
111+
112+
const run = await architect.scheduleTarget(targetSpec, overrides);
113+
try {
114+
await run.result;
115+
expect('THE ABOVE LINE SHOULD THROW').toBe('');
116+
} catch {}
117+
118+
// The node_modules folder must be deleted, otherwise code that tries to find the
119+
// node_modules folder will hit this one and can fail.
120+
host.scopedSync().delete(normalize('./node_modules'));
121+
122+
await run.stop();
123+
});
124+
125+
it('fails with non-source root input path', async () => {
126+
const assets: { [path: string]: string } = {
127+
'./node_modules/some-package/node_modules-asset.txt': 'node_modules-asset.txt',
128+
};
129+
host.writeMultipleFiles(assets);
130+
const overrides = {
131+
assets: ['not-source-root/file.txt'],
132+
};
133+
134+
const run = await architect.scheduleTarget(targetSpec, overrides);
135+
try {
136+
await run.result;
137+
expect('THE ABOVE LINE SHOULD THROW').toBe('');
138+
} catch {}
139+
140+
// The node_modules folder must be deleted, otherwise code that tries to find the
141+
// node_modules folder will hit this one and can fail.
142+
host.scopedSync().delete(normalize('./node_modules'));
143+
144+
await run.stop();
145+
});
146+
147+
it('still builds with empty asset array', async () => {
148+
const overrides = {
149+
assets: [],
150+
};
151+
152+
const run = await architect.scheduleTarget(targetSpec, overrides);
153+
const events = await run.output.pipe(toArray()).toPromise();
154+
expect(events.length).toBe(1);
155+
156+
await run.stop();
157+
});
158+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
9+
import { Architect } from '@angular-devkit/architect/src/index2';
10+
import { runTargetSpec } from '@angular-devkit/architect/testing';
11+
import { join, normalize, virtualFs } from '@angular-devkit/core';
12+
import { tap } from 'rxjs/operators';
13+
import { BrowserBuilderOutput } from "../../src/browser/index2";
14+
import { browserTargetSpec, createArchitect, host } from '../utils';
15+
16+
17+
describe('Browser Builder base href', () => {
18+
const targetSpec = { project: 'app', target: 'build' };
19+
let architect: Architect;
20+
21+
beforeEach(async () => {
22+
await host.initialize().toPromise();
23+
architect = (await createArchitect(host.root())).architect;
24+
});
25+
afterEach(async () => host.restore().toPromise());
26+
27+
it('works', async () => {
28+
host.writeMultipleFiles({
29+
'src/my-js-file.js': `console.log(1); export const a = 2;`,
30+
'src/main.ts': `import { a } from './my-js-file'; console.log(a);`,
31+
});
32+
33+
const overrides = { baseHref: '/myUrl' };
34+
const run = await architect.scheduleTarget(targetSpec, overrides);
35+
const output = await run.result as BrowserBuilderOutput;
36+
37+
expect(output.success).toBe(true);
38+
const fileName = join(normalize(output.outputPath), 'index.html');
39+
const content = virtualFs.fileBufferToString(await host.read(fileName).toPromise());
40+
expect(content).toMatch(/<base href="\/myUrl">/);
41+
42+
await run.stop();
43+
});
44+
});

0 commit comments

Comments
 (0)