From ed12d7e949e14b43f24582734adf917b49601fe2 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Sat, 6 Apr 2019 15:35:13 +0100 Subject: [PATCH] test(ivy): ngcc - improve and use the `MockLogger` (#29740) Previously the console logger was being used in integration tests leading to lots of output during test runs. PR Close #29740 --- packages/compiler-cli/ngcc/test/BUILD.bazel | 2 + .../ngcc/test/helpers/BUILD.bazel | 16 +++++++ .../ngcc/test/helpers/mock_logger.ts | 15 ++++--- .../ngcc/test/integration/ngcc_spec.ts | 43 +++++++++++++------ 4 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 packages/compiler-cli/ngcc/test/helpers/BUILD.bazel diff --git a/packages/compiler-cli/ngcc/test/BUILD.bazel b/packages/compiler-cli/ngcc/test/BUILD.bazel index 5a97a88dd6..7aabc69b43 100644 --- a/packages/compiler-cli/ngcc/test/BUILD.bazel +++ b/packages/compiler-cli/ngcc/test/BUILD.bazel @@ -11,6 +11,7 @@ ts_library( ), deps = [ "//packages/compiler-cli/ngcc", + "//packages/compiler-cli/ngcc/test/helpers", "//packages/compiler-cli/src/ngtsc/imports", "//packages/compiler-cli/src/ngtsc/partial_evaluator", "//packages/compiler-cli/src/ngtsc/path", @@ -46,6 +47,7 @@ ts_library( ), deps = [ "//packages/compiler-cli/ngcc", + "//packages/compiler-cli/ngcc/test/helpers", "//packages/compiler-cli/src/ngtsc/path", "//packages/compiler-cli/test:test_utils", "@npm//@types/mock-fs", diff --git a/packages/compiler-cli/ngcc/test/helpers/BUILD.bazel b/packages/compiler-cli/ngcc/test/helpers/BUILD.bazel new file mode 100644 index 0000000000..012a925b94 --- /dev/null +++ b/packages/compiler-cli/ngcc/test/helpers/BUILD.bazel @@ -0,0 +1,16 @@ +load("//tools:defaults.bzl", "ts_library") + +package(default_visibility = ["//packages/compiler-cli/ngcc:__subpackages__"]) + +ts_library( + name = "helpers", + testonly = True, + srcs = glob([ + "*.ts", + ]), + deps = [ + "//packages/compiler-cli/ngcc", + "//packages/compiler-cli/src/ngtsc/path", + "//packages/compiler-cli/src/ngtsc/testing", + ], +) diff --git a/packages/compiler-cli/ngcc/test/helpers/mock_logger.ts b/packages/compiler-cli/ngcc/test/helpers/mock_logger.ts index ddc9bfeb04..e82e77005e 100644 --- a/packages/compiler-cli/ngcc/test/helpers/mock_logger.ts +++ b/packages/compiler-cli/ngcc/test/helpers/mock_logger.ts @@ -9,9 +9,14 @@ import {Logger} from '../../src/logging/logger'; export class MockLogger implements Logger { - logs: string[][] = []; - debug(...args: string[]) { this.logs.push(args); } - info(...args: string[]) { this.logs.push(args); } - warn(...args: string[]) { this.logs.push(args); } - error(...args: string[]) { this.logs.push(args); } + logs: {[P in keyof Logger]: string[][]} = { + debug: [], + info: [], + warn: [], + error: [], + }; + debug(...args: string[]) { this.logs.debug.push(args); } + info(...args: string[]) { this.logs.info.push(args); } + warn(...args: string[]) { this.logs.warn.push(args); } + error(...args: string[]) { this.logs.error.push(args); } } \ No newline at end of file diff --git a/packages/compiler-cli/ngcc/test/integration/ngcc_spec.ts b/packages/compiler-cli/ngcc/test/integration/ngcc_spec.ts index a03647da1e..50838db295 100644 --- a/packages/compiler-cli/ngcc/test/integration/ngcc_spec.ts +++ b/packages/compiler-cli/ngcc/test/integration/ngcc_spec.ts @@ -11,10 +11,10 @@ import * as mockFs from 'mock-fs'; import {join} from 'path'; const Module = require('module'); -import {mainNgcc} from '../../src/main'; import {getAngularPackagesFromRunfiles, resolveNpmTreeArtifact} from '../../../test/runfile_helpers'; +import {mainNgcc} from '../../src/main'; import {EntryPointPackageJson} from '../../src/packages/entry_point'; -import {Logger} from '../../src/logging/logger'; +import {MockLogger} from '../helpers/mock_logger'; describe('ngcc main()', () => { beforeEach(createMockFileSystem); @@ -26,7 +26,11 @@ describe('ngcc main()', () => { }); it('should run ngcc without errors for esm5', () => { - expect(() => mainNgcc({basePath: '/node_modules', propertiesToConsider: ['esm5']})) + expect(() => mainNgcc({ + basePath: '/node_modules', + propertiesToConsider: ['esm5'], + logger: new MockLogger(), + })) .not.toThrow(); }); @@ -87,7 +91,9 @@ describe('ngcc main()', () => { () => { mainNgcc({ basePath: '/node_modules', - propertiesToConsider: ['main', 'esm5', 'module', 'fesm5'] + propertiesToConsider: ['main', 'esm5', 'module', 'fesm5'], + logger: new MockLogger(), + }); // * the `main` property is UMD, which is not yet supported. @@ -125,7 +131,9 @@ describe('ngcc main()', () => { mainNgcc({ basePath: '/node_modules', propertiesToConsider: ['main', 'module', 'fesm5', 'esm5'], - compileAllFormats: false + compileAllFormats: false, + logger: new MockLogger(), + }); // * The `main` is UMD, which is not yet supported, and so is not compiled. // * In the Angular packages fesm5 and module have the same underlying format, @@ -158,15 +166,21 @@ describe('ngcc main()', () => { mainNgcc({ basePath: '/node_modules', propertiesToConsider: ['module'], - compileAllFormats: false + compileAllFormats: false, + logger: new MockLogger(), + }); expect(loadPackage('@angular/core').__processed_by_ivy_ngcc__).toEqual({ module: '0.0.0-PLACEHOLDER', typings: '0.0.0-PLACEHOLDER', }); // If ngcc tries to write out the typings files again, this will throw an exception. - mainNgcc( - {basePath: '/node_modules', propertiesToConsider: ['esm5'], compileAllFormats: false}); + mainNgcc({ + basePath: '/node_modules', + propertiesToConsider: ['esm5'], + compileAllFormats: false, + logger: new MockLogger(), + }); expect(loadPackage('@angular/core').__processed_by_ivy_ngcc__).toEqual({ esm5: '0.0.0-PLACEHOLDER', module: '0.0.0-PLACEHOLDER', @@ -181,7 +195,9 @@ describe('ngcc main()', () => { mainNgcc({ basePath: '/node_modules', createNewEntryPointFormats: true, - propertiesToConsider: ['esm5'] + propertiesToConsider: ['esm5'], + logger: new MockLogger(), + }); // Updates the package.json @@ -222,9 +238,12 @@ describe('ngcc main()', () => { }); it('should use a custom logger if provided', () => { - const logger: Logger = jasmine.createSpyObj(['debug', 'info', 'warn', 'error']); - mainNgcc({basePath: '/node_modules', propertiesToConsider: ['esm2015'], logger}); - expect(logger.info).toHaveBeenCalled(); + const logger = new MockLogger(); + mainNgcc({ + basePath: '/node_modules', + propertiesToConsider: ['esm2015'], logger, + }); + expect(logger.logs.info).toContain(['Compiling @angular/common/http : esm2015 as esm2015']); }); }); });