From bfd91096be550d93d4f0ac09319cd36b13903d90 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Fri, 11 Dec 2020 16:16:45 -0500 Subject: [PATCH] test(@angular-devkit/build-angular): add harness file jasmine helpers This change adds an `expectFile` jasmine helper function that reduces the amount of code necessary to check a file's existence, size, or content. Additional contextual information is also displayed when an expectation fails. --- .../build_angular/src/testing/index.ts | 2 +- .../src/testing/jasmine-helpers.ts | 33 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/testing/index.ts b/packages/angular_devkit/build_angular/src/testing/index.ts index afd1b6fe47..3300346cf5 100644 --- a/packages/angular_devkit/build_angular/src/testing/index.ts +++ b/packages/angular_devkit/build_angular/src/testing/index.ts @@ -6,4 +6,4 @@ * found in the LICENSE file at https://angular.io/license */ export { BuilderHarnessExecutionOptions, BuilderHarnessExecutionResult } from './builder-harness'; -export { describeBuilder } from './jasmine-helpers'; +export { HarnessFileMatchers, describeBuilder } from './jasmine-helpers'; diff --git a/packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts b/packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts index baef881131..223d9d0a48 100644 --- a/packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts +++ b/packages/angular_devkit/build_angular/src/testing/jasmine-helpers.ts @@ -16,14 +16,14 @@ const optionSchemaCache = new Map(); export function describeBuilder( builderHandler: BuilderHandlerFn, options: { name?: string; schemaPath: string }, - specDefinitions: (harness: BuilderHarness) => void, + specDefinitions: (harness: JasmineBuilderHarness) => void, ): void { let optionSchema = optionSchemaCache.get(options.schemaPath); if (optionSchema === undefined) { optionSchema = JSON.parse(readFileSync(options.schemaPath, 'utf8')) as json.schema.JsonSchema; optionSchemaCache.set(options.schemaPath, optionSchema); } - const harness = new BuilderHarness(builderHandler, host, { + const harness = new JasmineBuilderHarness(builderHandler, host, { builderName: options.name, optionSchema, }); @@ -36,3 +36,32 @@ export function describeBuilder( specDefinitions(harness); }); } + +class JasmineBuilderHarness extends BuilderHarness { + expectFile(path: string): HarnessFileMatchers { + return expectFile(path, this); + } +} + +export interface HarnessFileMatchers { + toExist(): boolean; + toNotExist(): boolean; + readonly content: jasmine.ArrayLikeMatchers; + readonly size: jasmine.Matchers; +} + +export function expectFile(path: string, harness: BuilderHarness): HarnessFileMatchers { + return { + toExist: () => expect(harness.hasFile(path)).toBe(true, 'Expected file to exist: ' + path), + toNotExist: () => + expect(harness.hasFile(path)).toBe(false, 'Expected file to not exist: ' + path), + get content() { + return expect(harness.readFile(path)).withContext(`With file content for '${path}'`); + }, + get size() { + return expect(Buffer.byteLength(harness.readFile(path))).withContext( + `With file size for '${path}'`, + ); + }, + }; +}