question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular stability is not reflected correctly in test environment when a setInterval is running

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/core/testing

Is this a regression?

No, it seems that the issue can be reproduced on Angular versions before and after Ivy.

Description

The stability of Angular when setInterval function is running is not set correctly in testing environment or the available tools do not provide a way to verify this. If the application is running in the browser and a setInterval is called in a component, then executing window.getAllAngularTestabilities()[0].isStable() in the browser console returns false. However, in testing environment, the behaviour looks different - if I start the interval inside a component, fixture.isStable() returns true, when it should not be not stable.

🔬 Minimal Reproduction

  1. Create a really simple component, which will call setInterval:
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `<button (click)="startInterval()">Start interval</button>`
})
export class AppComponent {

  startInterval() {
    setInterval(() => { }, 1000);
  }
}
  1. Open the browser and check the stability before any interactions with the component: window.getAllAngularTestabilities()[0].isStable(). The result should be true.

  2. Click the button to invoke the interval function. Run the same script again, the result should be false.

  3. Try to test the stability with the fixture tools:

import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  it('should make Angular unstable when there are unfinished tasks', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;

    app.startInterval(); // The interval has started
    fixture.detectChanges();  

    // The fixture should already be unstable

    expect(fixture.isStable()).toEqual(false); // Fails, the fixture is still stable
  });
});

  1. Running that test in async or fakeAsync zone still does not solve the problem.

🌍 Your Environment

Angular Version:


Angular CLI: 10.0.7
Node: 12.13.0
OS: darwin x64

Angular: 10.0.12
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.1000.7
@angular-devkit/build-angular     0.1000.7
@angular-devkit/build-optimizer   0.1000.7
@angular-devkit/build-webpack     0.1000.7
@angular-devkit/core              10.0.7
@angular-devkit/schematics        10.0.7
@angular/cli                      10.0.7
@ngtools/webpack                  10.0.7
@schematics/angular               10.0.7
@schematics/update                0.1000.7
rxjs                              6.5.5
typescript                        3.9.7
webpack                           4.43.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JiaLiPassioncommented, Sep 23, 2020

@dimitarspassov, there is no such API built in, but it is good to have such kind of API, I will try to think about how to implement it.

0reactions
dimitarspassovcommented, Sep 23, 2020

@JiaLiPassion Does the existing testing API provide options for running tests inside ngZone?

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Why is Angular stable in test environment when ...
If the interval has started, the same script returns false , which is what I expect - an interval is running, so Angular...
Read more >
Component testing scenarios
The Angular testing environment does not know that the test changed the component's title . The ComponentFixtureAutoDetect service responds to asynchronous ...
Read more >
The Last Guide For Angular Change Detection You'll Ever ...
As developers, most of the time we do not need to care about change detection until we need to optimize the performance of...
Read more >
setInterval() - Web APIs - MDN Web Docs
For example, if using setInterval() to poll a remote server every 5 seconds, network latency, an unresponsive server, and a host of other...
Read more >
Node.js v19.3.0 Documentation
Use of the feature is not recommended in production environments. Stability: 2 - Stable. Compatibility with the npm ecosystem is a high priority....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found