-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathtest-utils.ts
176 lines (150 loc) · 4.79 KB
/
test-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Architect, BuilderOutput, ScheduleOptions, Target } from '@angular-devkit/architect';
import { WorkspaceNodeModulesArchitectHost } from '@angular-devkit/architect/node';
import { TestProjectHost, TestingArchitectHost } from '@angular-devkit/architect/testing';
import { BrowserBuilderOutput } from '@angular-devkit/build-angular';
import {
Path,
getSystemPath,
join,
json,
normalize,
schema,
virtualFs,
workspaces,
} from '@angular-devkit/core';
// Default timeout for large specs is 2.5 minutes.
jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000;
export const workspaceRoot = join(normalize(__dirname), `../../test/hello-world-app/`);
export const host = new TestProjectHost(workspaceRoot);
export const outputPath: Path = normalize('dist');
export const browserTargetSpec = { project: 'app', target: 'build' };
export const devServerTargetSpec = { project: 'app', target: 'serve' };
export const extractI18nTargetSpec = { project: 'app', target: 'extract-i18n' };
export const karmaTargetSpec = { project: 'app', target: 'test' };
export const tslintTargetSpec = { project: 'app', target: 'lint' };
export const protractorTargetSpec = { project: 'app-e2e', target: 'e2e' };
export async function createArchitect(workspaceRoot: Path) {
const registry = new schema.CoreSchemaRegistry();
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
const workspaceSysPath = getSystemPath(workspaceRoot);
const { workspace } = await workspaces.readWorkspace(
workspaceSysPath,
workspaces.createWorkspaceHost(host),
);
const architectHost = new TestingArchitectHost(
workspaceSysPath,
workspaceSysPath,
new WorkspaceNodeModulesArchitectHost(workspace, workspaceSysPath),
);
const architect = new Architect(architectHost, registry);
return {
workspace,
architectHost,
architect,
};
}
export interface BrowserBuildOutput {
output: BuilderOutput;
files: { [file: string]: Promise<string> };
}
export async function browserBuild(
architect: Architect,
host: virtualFs.Host,
target: Target,
overrides?: json.JsonObject,
scheduleOptions?: ScheduleOptions,
): Promise<BrowserBuildOutput> {
const run = await architect.scheduleTarget(target, overrides, scheduleOptions);
const output = (await run.result) as BrowserBuilderOutput;
expect(output.success).toBe(true);
if (!output.success) {
await run.stop();
return {
output,
files: {},
};
}
const [{ path }] = output.outputs;
expect(path).toBeTruthy();
const outputPath = normalize(path);
const fileNames = await host.list(outputPath).toPromise();
const files = fileNames.reduce((acc: { [name: string]: Promise<string> }, path) => {
let cache: Promise<string> | null = null;
Object.defineProperty(acc, path, {
enumerable: true,
get() {
if (cache) {
return cache;
}
if (!fileNames.includes(path)) {
return Promise.reject('No file named ' + path);
}
cache = host
.read(join(outputPath, path))
.toPromise()
.then((content) => virtualFs.fileBufferToString(content));
return cache;
},
});
return acc;
}, {});
await run.stop();
return {
output,
files,
};
}
export const lazyModuleFiles: { [path: string]: string } = {
'src/app/lazy/lazy-routing.module.ts': `
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class LazyRoutingModule { }
`,
'src/app/lazy/lazy.module.ts': `
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LazyRoutingModule } from './lazy-routing.module';
@NgModule({
imports: [
CommonModule,
LazyRoutingModule
],
declarations: []
})
export class LazyModule { }
`,
};
export const lazyModuleFnImport: { [path: string]: string } = {
'src/app/app.module.ts': `
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { RouterModule } from '@angular/router';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
RouterModule.forRoot([
{ path: 'lazy', loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule) }
])
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
`,
};