Skip to content

Commit

Permalink
test(datepicker): ignore warnings that are unavoidable
Browse files Browse the repository at this point in the history
Track-related warnings now cause test failures by default.
So, if such a failure happens, we can either fix the code to avoid the failure when the code is incorrect (and the error is thus justified), or ignore the warning when the code is correct and the warning is unavoidable.

In the end, the test report is not be spammed with warnings anymore, and when there is a warning, the error allows knowing which test caused it.
  • Loading branch information
jnizet authored and maxokorokov committed Jun 7, 2024
1 parent 7c693d6 commit 2fd9727
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/datepicker/datepicker-navigation-select.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ComponentFixture } from '@angular/core/testing';
import { createGenericTestComponent, triggerEvent } from '../test/common';
import { createGenericTestComponent, ignoreTrackWarnings, triggerEvent } from '../test/common';
import { getMonthSelect, getYearSelect } from '../test/datepicker/common';

import { Component } from '@angular/core';
Expand Down Expand Up @@ -42,6 +42,7 @@ describe('ngb-datepicker-navigation-select', () => {
});

it('should generate year options correctly', () => {
ignoreTrackWarnings();
const fixture = createTestComponent(
`<ngb-datepicker-navigation-select [date]="date" [months]="months" [years]="years">`,
);
Expand Down
10 changes: 9 additions & 1 deletion src/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed, ComponentFixture, inject, fakeAsync, tick } from '@angular/core/testing';
import { createGenericTestComponent, triggerEvent } from '../test/common';
import { createGenericTestComponent, ignoreTrackWarnings, triggerEvent } from '../test/common';
import { getMonthSelect, getYearSelect, getNavigationLinks } from '../test/datepicker/common';

import { Component, TemplateRef, DebugElement } from '@angular/core';
Expand Down Expand Up @@ -173,6 +173,8 @@ describe('ngb-datepicker', () => {
});

it('should allow changing min/max dates at the same time', () => {
// hide track warnings because they're the year select options which change, which is expected and normal
ignoreTrackWarnings();
const fixture = createTestComponent('<ngb-datepicker [minDate]="minDate" [maxDate]="maxDate"></ngb-datepicker>');

expect(() => {
Expand Down Expand Up @@ -265,6 +267,8 @@ describe('ngb-datepicker', () => {
});

it('should allow infinite navigation when min/max dates are not set', () => {
// hide track warnings because they're the year select options which change, which is expected and normal
ignoreTrackWarnings();
const fixture = createTestComponent(`<ngb-datepicker [startDate]="date"></ngb-datepicker>`);

fixture.detectChanges();
Expand Down Expand Up @@ -321,6 +325,8 @@ describe('ngb-datepicker', () => {
});

it('should handle minDate edge case values', () => {
// hide track warnings because they're the year select options which change, which is expected and normal
ignoreTrackWarnings();
const fixture = createTestComponent(`<ngb-datepicker [minDate]="minDate" [startDate]="date"></ngb-datepicker>`);
const datepicker = fixture.debugElement.query(By.directive(NgbDatepicker)).injector.get(NgbDatepicker);

Expand Down Expand Up @@ -360,6 +366,8 @@ describe('ngb-datepicker', () => {
});

it('should handle maxDate edge case values', () => {
// hide track warnings because they're the year select options which change, which is expected and normal
ignoreTrackWarnings();
const fixture = createTestComponent(`<ngb-datepicker [maxDate]="maxDate" [startDate]="date"></ngb-datepicker>`);
const datepicker = fixture.debugElement.query(By.directive(NgbDatepicker)).injector.get(NgbDatepicker);

Expand Down
15 changes: 15 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@ import { getTestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';

import './test/jasmine.config';
import { areTrackWarningsIgnored, honorTrackWarnings } from './test/common';

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting(), {
errorOnUnknownElements: true,
errorOnUnknownProperties: true,
});

beforeEach(() => {
honorTrackWarnings();
const originalWarn = console.warn;
spyOn(console, 'warn').and.callFake((message: string, ...args: any[]) => {
if (message.includes('NG0956') || message.includes('NG0955')) {
if (!areTrackWarningsIgnored()) {
fail('track warning found: ' + message);
}
} else {
originalWarn(message, ...args);
}
});
});
25 changes: 24 additions & 1 deletion src/test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function createGenericTestComponent<T>(

export function isBrowserVisible(suiteName: string) {
if (document.hidden) {
console.warn(`${suiteName} tests were skipped because browser tab running these tests is hidden or inactive`);
// console.warn(`${suiteName} tests were skipped because browser tab running these tests is hidden or inactive`);
return false;
}
return true;
Expand All @@ -26,3 +26,26 @@ export function triggerEvent(element: DebugElement | HTMLElement, eventName: str
const evt = new Event(eventName, { bubbles: true, cancelable: false });
(element instanceof DebugElement ? element.nativeElement : element).dispatchEvent(evt);
}

let trackWarningsIgnored = false;

/**
* This function may be called by a test in order for a track-related warning
* to be ignored and not cause a test failure, because the warning is unavoidable
* and the code is correct.
*/
export function ignoreTrackWarnings() {
trackWarningsIgnored = true;
}

/**
* This function is called by the global beforeEach in test.ts in order to
* honor the track warnings, in case the previous test has ignored them.
*/
export function honorTrackWarnings() {
trackWarningsIgnored = false;
}

export function areTrackWarningsIgnored() {
return trackWarningsIgnored;
}

0 comments on commit 2fd9727

Please sign in to comment.