Skip to content

Commit 5dba8a2

Browse files
clydinalan-agius4
authored andcommitted
test(@angular-devkit/build-angular): disable Webpack caching during harness-based unit tests
The default set of metadata fields provided to all builders executed via the BuilderHarness is controlled programmatically. The BuilderHarness is eventually intended to operate in-memory and without file system access unlike the legacy unit tests. The `cli.cache.enabled` option must therefore be set within the BuilderHarness initialization code. `cli.cache.enabled` set to false provides improved test isolation guarantees by disabling Webpack’s caching.
1 parent c44f122 commit 5dba8a2

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

packages/angular_devkit/build_angular/src/testing/builder-harness.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,27 @@ export interface BuilderHarnessExecutionOptions {
3939
useNativeFileWatching: boolean;
4040
}
4141

42+
/**
43+
* The default set of fields provided to all builders executed via the BuilderHarness.
44+
* `root` and `sourceRoot` are required for most Angular builders to function.
45+
* `cli.cache.enabled` set to false provides improved test isolation guarantees by disabling
46+
* the Webpack caching.
47+
*/
48+
const DEFAULT_PROJECT_METADATA = {
49+
root: '.',
50+
sourceRoot: 'src',
51+
cli: {
52+
cache: {
53+
enabled: false,
54+
},
55+
},
56+
};
57+
4258
export class BuilderHarness<T> {
4359
private readonly builderInfo: BuilderInfo;
4460
private schemaRegistry = new json.schema.CoreSchemaRegistry();
4561
private projectName = 'test';
46-
private projectMetadata: Record<string, unknown> = { root: '.', sourceRoot: 'src' };
62+
private projectMetadata: Record<string, unknown> = DEFAULT_PROJECT_METADATA;
4763
private targetName?: string;
4864
private options = new Map<string | null, T>();
4965
private builderTargets = new Map<
@@ -365,7 +381,7 @@ class HarnessBuilderContext implements BuilderContext {
365381

366382
get analytics(): analytics.Analytics {
367383
// Can be undefined even though interface does not allow it
368-
return (undefined as unknown) as analytics.Analytics;
384+
return undefined as unknown as analytics.Analytics;
369385
}
370386

371387
addTeardown(teardown: () => Promise<void> | void): void {
@@ -447,7 +463,7 @@ class HarnessBuilderContext implements BuilderContext {
447463
options: json.JsonObject,
448464
builderName: string,
449465
): Promise<T> {
450-
return (this.contextHost.validate(options, builderName) as unknown) as T;
466+
return this.contextHost.validate(options, builderName) as unknown as T;
451467
}
452468

453469
// Unused report methods

0 commit comments

Comments
 (0)