cartant / rxjs-spy

A debugging library for RxJS
https://cartant.github.io/rxjs-spy/
MIT License
702 stars 22 forks source link

Random "Error: Timeout" is triggered in unit test #39

Closed davidshen84 closed 5 years ago

davidshen84 commented 5 years ago

Environment

Angular CLI: 7.0.6
Node: 8.11.3
OS: win32 x64
Angular: 7.0.4
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.10.6
@angular-devkit/build-angular     0.10.6
@angular-devkit/build-optimizer   0.10.6
@angular-devkit/build-webpack     0.10.6
@angular-devkit/core              7.0.6
@angular-devkit/schematics        7.0.6
@angular/cli                      7.0.6
@ngtools/webpack                  7.0.6
@schematics/angular               7.0.6
@schematics/update                0.10.6
rxjs                              6.3.3
typescript                        3.1.6
webpack                           4.19.1

I can also reproduce this issue on a Linux environment.

Reproduce Steps

  1. Create a standard Angular app using the Angular CLI.
  2. Insert and call create() in the generated app.component.ts.
  3. Execute ng test.

Expected

All tests are run and pass.

Actual

One test failed with timeout error.

Analysis

I discovered this issue while working on my own project. It seems it will cause one unit test case to fail and it alway the same one. But interestingly, if I change the unit test code order, a different one will fail. Also, if I ran only one test case, this issue will not occur.

I know it does not make sense to use this library in unit tests. But it would be better if it does not interfere with unit tests.

cartant commented 5 years ago

You'll need to give me an example of precisely where you are calling create. If you are doing it in the app component and are using that in unit tests, you will be responsible for ensuring the spy is torn down.

davidshen84 commented 5 years ago

Complete souce code of app.component.ts.

import {Component} from '@angular/core';
import {create} from 'rxjs-spy';

create();

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

I thought calling create at the top is the only option...honestly, I am new to this library. If there's a better place to put create, please let me know.

cartant commented 5 years ago

I would suggest putting it in main.ts, which is a module that won't - AFAICT - be imported into any unit tests.

cartant commented 5 years ago

If you choose to place it somewhere else, be aware that create returns a spy instance and you can call teardown on it when (and where) you're done with it:

const spy = create();
// ...
spy.teardown();
davidshen84 commented 5 years ago

So the conclusion is that create should not be invoked during unit test. Thanks!

cartant commented 5 years ago

There's no reason why it cannot be invoked in a unit test - this project creates and tears down spies in its own unit tests - but you need to be aware of what you are doing regarding creating and tearing down spies. And you'd need a good reason for doing it - I don't know why you'd need a spy in a unit test.

davidshen84 commented 5 years ago

No, I don't need a spy in my unit test. The spy got created by the application code, and I did not take care of it.

On Sun, Nov 18, 2018 at 8:31 PM Nicholas Jamieson notifications@github.com wrote:

There's no reason why it cannot be invoked in a unit test - this project creates and tears down spies in its own unit tests - but you need to be aware of what you are doing regarding creating and tearing down spies. And you'd need a good reason for doing it - I don't know why you'd need a spy in a unit test.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/cartant/rxjs-spy/issues/39#issuecomment-439689411, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEAzRp7uctqAekkJ3k4_zToHurvHaUOks5uwVMwgaJpZM4Ynxjz .