From cb6ad971c364e0c3cecd9dfadffb96234013606c Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Wed, 8 May 2019 13:14:42 -0700 Subject: [PATCH] test(ivy): add onlyInIvy perf counter expectations (#30339) We have an issue where we would like to be able to test perf counter metrics in acceptance tests, but we are unable to do so, because it will break when those same tests are run with ViewEngine. This PR adds a testing utility to `onlyInIvy` that allows for testing of performance counters, and even gives readable errors for what value on `ngDevMode` is incorrect. Has typings for decent autocompletion as well. PR Close #30339 --- packages/core/test/acceptance/BUILD.bazel | 1 + packages/core/test/acceptance/styling_spec.ts | 12 +++++++ .../private/testing/src/ivy_test_selectors.ts | 34 ++++++++++++++++--- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/core/test/acceptance/BUILD.bazel b/packages/core/test/acceptance/BUILD.bazel index 997be2d82b..8f74f3f99a 100644 --- a/packages/core/test/acceptance/BUILD.bazel +++ b/packages/core/test/acceptance/BUILD.bazel @@ -13,6 +13,7 @@ ts_library( "//packages/compiler", "//packages/compiler/testing", "//packages/core", + "//packages/core/src/util", "//packages/core/testing", "//packages/platform-browser", "//packages/platform-browser-dynamic", diff --git a/packages/core/test/acceptance/styling_spec.ts b/packages/core/test/acceptance/styling_spec.ts index 6dd86a9876..fa5e7237b6 100644 --- a/packages/core/test/acceptance/styling_spec.ts +++ b/packages/core/test/acceptance/styling_spec.ts @@ -6,11 +6,15 @@ * found in the LICENSE file at https://angular.io/license */ import {Component, Directive, ElementRef} from '@angular/core'; +import {ngDevModeResetPerfCounters} from '@angular/core/src/util/ng_dev_mode'; import {TestBed} from '@angular/core/testing'; import {DomSanitizer, SafeStyle} from '@angular/platform-browser'; import {expect} from '@angular/platform-browser/testing/src/matchers'; +import {onlyInIvy} from '@angular/private/testing'; describe('styling', () => { + beforeEach(ngDevModeResetPerfCounters); + it('should render inline style and class attribute values on the element before a directive is instantiated', () => { @Component({ @@ -147,5 +151,13 @@ describe('styling', () => { const div = fixture.nativeElement.querySelector('div') as HTMLDivElement; expect(div.style.backgroundImage).toBe('url("#test")'); + + onlyInIvy('perf counters').expectPerfCounters({ + stylingApply: 2, + stylingApplyCacheMiss: 1, + stylingProp: 2, + stylingPropCacheMiss: 1, + tNode: 3, + }); }); }); diff --git a/packages/private/testing/src/ivy_test_selectors.ts b/packages/private/testing/src/ivy_test_selectors.ts index 516c1b4b91..02fdd3a819 100644 --- a/packages/private/testing/src/ivy_test_selectors.ts +++ b/packages/private/testing/src/ivy_test_selectors.ts @@ -90,23 +90,49 @@ export interface JasmineMethods { fit: typeof fit; describe: typeof describe; fdescribe: typeof fdescribe; + + /** + * Runs jasmine expectations against the provided keys for `ngDevMode`. + * + * Will not perform expectations for keys that are not provided. + * + * ```ts + * // Expect that `ngDevMode.styleMap` is `1`, and `ngDevMode.tNode` is `3`, but we don't care + * // about the other values. + * onlyInIvy('perf counters').expectPerfCounters({ + * stylingMap: 1, + * tNode: 3, + * }) + * ``` + */ + expectPerfCounters: (expectedCounters: Partial) => void; isEnabled: boolean; } const PASSTHROUGH: JasmineMethods = { - it: it, - fit: fit, - describe: describe, - fdescribe: fdescribe, + it, + fit, + describe, + fdescribe, + expectPerfCounters, isEnabled: true, }; function noop() {} +function expectPerfCounters(expectedCounters: Partial) { + Object.keys(expectedCounters).forEach(key => { + const expected = (expectedCounters as any)[key]; + const actual = (ngDevMode as any)[key]; + expect(actual).toBe(expected, `ngDevMode.${key}`); + }); +} + const IGNORE: JasmineMethods = { it: noop, fit: noop, describe: noop, fdescribe: noop, + expectPerfCounters: noop, isEnabled: false, };