Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

fakeAsync test fails with RxJS 6 #1056

Closed
cexbrayat opened this issue Mar 23, 2018 · 2 comments · Fixed by #1057
Closed

fakeAsync test fails with RxJS 6 #1056

cexbrayat opened this issue Mar 23, 2018 · 2 comments · Fixed by #1057

Comments

@cexbrayat
Copy link
Member

There is a possible problem with RxJS 6 in fakeAsync tests, and I think this might be on zone.js part, so I'm opening the issue here instead of on the Angular repo.

Let's take a simple component, with an interval observable modifying a field every second,
in a brand new CLI app (cli 1.7.2, ng 5.2.9, rxjs 5.5.7, zone.js 0.8.20):

import { Component, OnInit } from '@angular/core';
import { interval } from 'rxjs/observable/interval';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = -1;

  ngOnInit() {
    interval(1000).subscribe(n => this.title = n);
  }
}

We can write a simple test for this, verifying that when we tick 1 second, the title is updated:

import { fakeAsync, tick, discardPeriodicTasks } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {

  it('should update title every second', fakeAsync(() => {
    const component = new AppComponent();

    expect(component.title).toBe(-1);

    // start the observable
    component.ngOnInit();

    tick(1000);
    expect(component.title).toBe(0);

    tick(1000);
    expect(component.title).toBe(1);

    tick(1000);
    expect(component.title).toBe(2);

    discardPeriodicTasks();
  }));
});

The test is a success.

Now, when we bump the app to RxJS 6 (6.0.0-beta.1 ATM) and ng 6 (6.0.0-rc.0 ATM), and relaunch the same test fails:

Chrome 65.0.3325 (Mac OS X 10.13.3) AppComponent should update title every second FAILED
	Expected 2 to be 1.
	    at UserContext.<anonymous> src/app/app.component.spec.ts:18:29)
	    at UserContext.<anonymous> node_modules/@angular/core/fesm5/testing.js:413:1)
	    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:388:1)
	    at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke node_modules/zone.js/dist/zone-testing.js:239:1)
	Expected 4 to be 2.
	    at UserContext.<anonymous> src/app/app.component.spec.ts:21:29)
	    at UserContext.<anonymous> node_modules/@angular/core/fesm5/testing.js:413:1)
	    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:388:1)
	    at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke node_modules/zone.js/dist/zone-testing.js:239:1)

I then suppose this is an issue with zone.js and RxJS 6 (but maybe it's Angular fakeAsync, or RxJS itself?). I have this issue an a more complex project, this is a dumbed down example.

If you need easy reproduction, I created a repo with it: https://github.com/cexbrayat/rxjs6

master contains the CLI app, with the test succeeding in RXJS 5.5.
chore/rxjs6 contains the commit bumping to RXJS 6. Just run yarn and ng test to see the issue.
PR https://github.com/cexbrayat/rxjs6/pull/1 shows the difference between the two branches.

@JiaLiPassion
Copy link
Collaborator

@cexbrayat , thank you for posting the issue, I will test it.

@JiaLiPassion
Copy link
Collaborator

JiaLiPassion commented Mar 24, 2018

@cexbrayat , This is a zone.js issue, I have created a PR to fix this one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants