angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

[test]: random execution of unit tests while using multiple stub components in same spec

Open jkuri opened this issue 8 years ago • 7 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Command (mark with an x)

- [ ] new
- [ ] build
- [ ] serve
- [x] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Versions

NodeJS: v10.13.0 npm: 6.4.1 Angular CLI:

Angular CLI: 7.0.6
Node: 10.13.0
OS: darwin 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

Repro steps

I discovered random behavior in unit testing components using ng test command. While using multiple stub components in same .spec.ts file those get executed randomly and not producing desired results. I created sample here. Reproduce steps:

git clone https://github.com/jkuri/abstruse.git -b v2.0.0
cd abstruse
npm install
ng test

Note that tests are sometimes executed correctly.

The log given by the failure

It's karma or Jasmine output about failing tests because wrong expectation of values.

Desired functionality

Tests should get executed in the way stub components are not affecting one another.

Mention any other details that might be useful

https://github.com/jkuri/abstruse/tree/v2.0.0

jkuri avatar Nov 20 '18 21:11 jkuri

If you use the karma-jasmine, you can set the random is false. like this:

client: {
  jasmine: {
    random: false,
  }
}

The jasmine changed the default set in the v3.x

yesterdaytime avatar Nov 28 '18 15:11 yesterdaytime

@jkuri I don't quite follow why you think this is a bug in Angular CLI. It sounds like it's mostly a Jasmine or Karma thing.

filipesilva avatar Nov 29 '18 12:11 filipesilva

@filipesilva I got a feeling it is related to cli compilation procedure or even more, it's @angular-devkit/build-angular/plugins/karma issue. Maybe I did not explained the problem very well, but for example, sometimes I get values from that stub component in this stub component. This causes tests failing sometimes randomly, but sometimes are passing just fine.

jkuri avatar Nov 29 '18 12:11 jkuri

I don't really see how the compilation would affect the runtime behaviour of your test stubs. If you can come up with a simple repro maybe we can investigate further but with the information available I don't think it's a CLI issue.

filipesilva avatar Nov 29 '18 14:11 filipesilva

@jkuri Are you sure all of your assertions are being reached? Your component under test uses an external template and stylesheet with input bindings that trigger ngOnChanges and update the view, so there's a lot of change detection/asynchronous stuff going on that could cause leaks in your tests.

See one possible explanation with a similar test setup here.

To troubleshoot you could try logging test A begin, test A stable, test B begin, test B stable, ... for each test and analyze any ungrouped outputs whenever the unexpected behavior occurs. It's possible that a fixture could fail to compile or never become stable, so you may also want to add catch to each compileComponents() and whenStable() and then fail() if so.

Or just don't depend on whenStable() (which is ironically unpredictable in my experience) and use Jasmine's done callbacks or another solution.

ParkerM avatar Dec 04 '18 04:12 ParkerM

Closing as inactive.

filipesilva avatar Dec 27 '18 13:12 filipesilva

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.