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
This commit is contained in:
Pete Bacon Darwin 2019-04-06 15:35:13 +01:00 committed by Igor Minar
parent cf40105fc0
commit ed12d7e949
4 changed files with 59 additions and 17 deletions

View File

@ -11,6 +11,7 @@ ts_library(
), ),
deps = [ deps = [
"//packages/compiler-cli/ngcc", "//packages/compiler-cli/ngcc",
"//packages/compiler-cli/ngcc/test/helpers",
"//packages/compiler-cli/src/ngtsc/imports", "//packages/compiler-cli/src/ngtsc/imports",
"//packages/compiler-cli/src/ngtsc/partial_evaluator", "//packages/compiler-cli/src/ngtsc/partial_evaluator",
"//packages/compiler-cli/src/ngtsc/path", "//packages/compiler-cli/src/ngtsc/path",
@ -46,6 +47,7 @@ ts_library(
), ),
deps = [ deps = [
"//packages/compiler-cli/ngcc", "//packages/compiler-cli/ngcc",
"//packages/compiler-cli/ngcc/test/helpers",
"//packages/compiler-cli/src/ngtsc/path", "//packages/compiler-cli/src/ngtsc/path",
"//packages/compiler-cli/test:test_utils", "//packages/compiler-cli/test:test_utils",
"@npm//@types/mock-fs", "@npm//@types/mock-fs",

View File

@ -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",
],
)

View File

@ -9,9 +9,14 @@
import {Logger} from '../../src/logging/logger'; import {Logger} from '../../src/logging/logger';
export class MockLogger implements Logger { export class MockLogger implements Logger {
logs: string[][] = []; logs: {[P in keyof Logger]: string[][]} = {
debug(...args: string[]) { this.logs.push(args); } debug: [],
info(...args: string[]) { this.logs.push(args); } info: [],
warn(...args: string[]) { this.logs.push(args); } warn: [],
error(...args: string[]) { this.logs.push(args); } 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); }
} }

View File

@ -11,10 +11,10 @@ import * as mockFs from 'mock-fs';
import {join} from 'path'; import {join} from 'path';
const Module = require('module'); const Module = require('module');
import {mainNgcc} from '../../src/main';
import {getAngularPackagesFromRunfiles, resolveNpmTreeArtifact} from '../../../test/runfile_helpers'; import {getAngularPackagesFromRunfiles, resolveNpmTreeArtifact} from '../../../test/runfile_helpers';
import {mainNgcc} from '../../src/main';
import {EntryPointPackageJson} from '../../src/packages/entry_point'; import {EntryPointPackageJson} from '../../src/packages/entry_point';
import {Logger} from '../../src/logging/logger'; import {MockLogger} from '../helpers/mock_logger';
describe('ngcc main()', () => { describe('ngcc main()', () => {
beforeEach(createMockFileSystem); beforeEach(createMockFileSystem);
@ -26,7 +26,11 @@ describe('ngcc main()', () => {
}); });
it('should run ngcc without errors for esm5', () => { 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(); .not.toThrow();
}); });
@ -87,7 +91,9 @@ describe('ngcc main()', () => {
() => { () => {
mainNgcc({ mainNgcc({
basePath: '/node_modules', 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. // * the `main` property is UMD, which is not yet supported.
@ -125,7 +131,9 @@ describe('ngcc main()', () => {
mainNgcc({ mainNgcc({
basePath: '/node_modules', basePath: '/node_modules',
propertiesToConsider: ['main', 'module', 'fesm5', 'esm5'], 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. // * 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, // * In the Angular packages fesm5 and module have the same underlying format,
@ -158,15 +166,21 @@ describe('ngcc main()', () => {
mainNgcc({ mainNgcc({
basePath: '/node_modules', basePath: '/node_modules',
propertiesToConsider: ['module'], propertiesToConsider: ['module'],
compileAllFormats: false compileAllFormats: false,
logger: new MockLogger(),
}); });
expect(loadPackage('@angular/core').__processed_by_ivy_ngcc__).toEqual({ expect(loadPackage('@angular/core').__processed_by_ivy_ngcc__).toEqual({
module: '0.0.0-PLACEHOLDER', module: '0.0.0-PLACEHOLDER',
typings: '0.0.0-PLACEHOLDER', typings: '0.0.0-PLACEHOLDER',
}); });
// If ngcc tries to write out the typings files again, this will throw an exception. // If ngcc tries to write out the typings files again, this will throw an exception.
mainNgcc( mainNgcc({
{basePath: '/node_modules', propertiesToConsider: ['esm5'], compileAllFormats: false}); basePath: '/node_modules',
propertiesToConsider: ['esm5'],
compileAllFormats: false,
logger: new MockLogger(),
});
expect(loadPackage('@angular/core').__processed_by_ivy_ngcc__).toEqual({ expect(loadPackage('@angular/core').__processed_by_ivy_ngcc__).toEqual({
esm5: '0.0.0-PLACEHOLDER', esm5: '0.0.0-PLACEHOLDER',
module: '0.0.0-PLACEHOLDER', module: '0.0.0-PLACEHOLDER',
@ -181,7 +195,9 @@ describe('ngcc main()', () => {
mainNgcc({ mainNgcc({
basePath: '/node_modules', basePath: '/node_modules',
createNewEntryPointFormats: true, createNewEntryPointFormats: true,
propertiesToConsider: ['esm5'] propertiesToConsider: ['esm5'],
logger: new MockLogger(),
}); });
// Updates the package.json // Updates the package.json
@ -222,9 +238,12 @@ describe('ngcc main()', () => {
}); });
it('should use a custom logger if provided', () => { it('should use a custom logger if provided', () => {
const logger: Logger = jasmine.createSpyObj(['debug', 'info', 'warn', 'error']); const logger = new MockLogger();
mainNgcc({basePath: '/node_modules', propertiesToConsider: ['esm2015'], logger}); mainNgcc({
expect(logger.info).toHaveBeenCalled(); basePath: '/node_modules',
propertiesToConsider: ['esm2015'], logger,
});
expect(logger.logs.info).toContain(['Compiling @angular/common/http : esm2015 as esm2015']);
}); });
}); });
}); });