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 = [
"//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",

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';
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); }
}

View File

@ -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']);
});
});
});