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

ng test not working after upgrade to 6.0.0

Open DominicBoettger opened this issue 8 years ago β€’ 42 comments

After upgrading the cli the unit tests doesn't start anymore. Previous behaviour was that the unit tests were found and executed. Now it does not find the unit tests and says 0 of 0 executed.

Versions

Angular CLI: 6.0.0-rc.6
Node: 9.7.1
OS: win32 x64
Angular: 5.2.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker

Repro steps

  1. Upgrade to latest cli
  2. ng test

Observed behavior

27 04 2018 14:59:35.185:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
27 04 2018 14:59:35.191:INFO [launcher]: Launching browser Chrome with unlimited concurrency
27 04 2018 14:59:35.201:INFO [launcher]: Starting browser Chrome                                                                                                                                                                                                                      27 04 2018 15:00:26.221:INFO [Chrome 66.0.3359 (Windows 10 0.0.0)]: Connected on socket _q17vZKEaK6MoqgCAAAA with id 70169079
Chrome 66.0.3359 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.016 secs / 0 secs)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `ng test`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\myuser\AppData\Roaming\npm-cache\_logs\2018-04-27T13_00_28_789Z-debug.log
10 silly lifecycle [email protected]~test: Args: [ '/d /s /c', 'ng test' ]
11 silly lifecycle [email protected]~test: Returned: code: 1  signal: null
12 info lifecycle [email protected]~test: Failed to exec test script
13 verbose stack Error: [email protected] test: `ng test`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:285:16)
13 verbose stack     at EventEmitter.emit (events.js:127:13)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:127:13)
13 verbose stack     at maybeClose (internal/child_process.js:936:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
14 verbose pkgid [email protected]
15 verbose cwd C:\Users\myuser\myproj\myproj-web
16 verbose Windows_NT 10.0.16299
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "test"
18 verbose node v9.7.1
19 verbose npm  v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] test: `ng test`
22 error Exit status 1
23 error Failed at the [email protected] test script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

DominicBoettger avatar Apr 27 '18 14:04 DominicBoettger

I have tried many different options, updated karma.conf, run rxjs-5-to-6-migrate using both tsconfig.app.json and tsconfig.spec.json, limiting the test to only one file using both fdescribe and an updated context all to no avail, still getting the same error with no indication of what might be the issue (I don't have angular fire installed).

10% building modules 1/1 modules 0 active(node:20307)
10% building modules 3/3 modules 0 active16 05 2018
16 05 2018 13:28:48.389:INFO [karma]: Karma v2.0.2 server started at http://0.0.0.0:9876/
16 05 2018 13:28:48.391:INFO [launcher]: Launching browser Chrome with unlimited concurrency
10% building modules 3/4 modules 1 active …Code/app/src sync /\.spec\.ts$/
16 05 2018 13:28:48.397:INFO [launcher]: Starting browser Chrome
16 05 2018 13:29:16.145:INFO [Chrome 66.0.3359 (Mac OS X 10.13.4)]: Connected on socket Oe1qLUQnfzz4NoimAAAA with id 50078300
Chrome 66.0.3359 (Mac OS X 10.13.4): Executed 0 of 0 ERROR (0.012 secs / 0 secs)

Jest does run but requires many tests to be updated in our application which is not at option at the moment.

Any guidance or tips appreciated! πŸ™

dmfaux avatar May 16 '18 11:05 dmfaux

I'm also experiencing this problem. ng serve and ng build runs without errors. I've tried to move files like package.json, tsconfig.*.json, test.ts, karma.conf.js, etc. from a clean project to my existing one but without luck.

I've also moved some tests from my existing app to the clean project and with some work they do run without any problems. Before upgrading to Angular 6 all tests were working fine.

Jest is also not an option for our project since almost all tests need to be updated.

I can confirm that the spec files are bundled by webpack and served to karma. However, inspecting the source tab in Chrome shows that there is indeed a part of the application missing.

My App: bildschirmfoto 2018-05-16 um 15 44 37

Vs. new Project: bildschirmfoto 2018-05-16 um 15 44 22

trayhem avatar May 16 '18 13:05 trayhem

If that might help, for our configuration it was the presence of rxjs-compat while testing that made everything fail (well, 0 of 0 executed), so in tsconfig.spec.json we added

"compilerOptions": {
        "paths": {
            "rxjs-compat": ["rxjs-compat.mock.ts"],
         }
}

where rxjs-compat.mock.ts is just

console.warn('False rxjs-compat loaded for testing, you should remove this whenever possible');

WilliamChelman avatar May 16 '18 15:05 WilliamChelman

@WilliamChelman this method did expose an issue with my paths which are defined in tsconfig.json but were not being defined in the tsconfig.spec.json, this was not an issue in Angular 5.x. I have updated tsconfig.spec.json with those now as well:

 "paths": {
      "rxjs-compat": ["rxjs-compat.mock.ts"],
      "@app/*": ["app/*"],
      "@env/*": ["environments/*"]
    }

The only change seems to be an addition of a SUCCESS entry in the output:

Chrome 66.0.3359 (Mac OS X 10.13.4): Executed 0 of 0 SUCCESS (0 secs / 0 secs)
Chrome 66.0.3359 (Mac OS X 10.13.4): Executed 0 of 0 ERROR (0.02 secs / 0 secs)

I am surprised no one from the team have weighed in on this as yet, not even to merely label the issue 😐

dmfaux avatar May 17 '18 04:05 dmfaux

In my situation I had one spec file that silently failed the whole test run. After commenting out this spec, I was able to run tests again.

To find the specific test, I ran ls src/**/*.spec.ts | grep -o ".........$" | cut -d. -f1 | sort | uniq to get the previous letter before ".spec.ts" part. Then I added these letters. one by one, to regular expression in test.ts file, like a\.spec\.ts, then (a|b)\.spec\.ts etc. At the beginning, I run more and more tests successfully, until the regex included corrupted test. For me, the last letter added was 'e', so it was either service or pipe specification.

Subsequently, I was able to find autogenerated service test, that caused the error. I am pretty sure, that the error was in beforeEach part, however I can't figure out the exact reason.

Hope my comment helps someone.

AndriiKobzar avatar May 19 '18 11:05 AndriiKobzar

@AndriiKobzar Thanks for your Tip. I've used adjusted it a litte to walk through the components in the test.ts step by step, eg. /(footer.*|header.*)\.spec\.ts$/ until one of the component tests failed. The problem was a missing service injection required by an include of the component.

trayhem avatar May 19 '18 12:05 trayhem

I also did several tests and modifications but still get the message Chrome 66.0.3359 (Windows 10.0.0): Executed 0 of 0 ERROR (0.009 secs / 0 secs)

If i create a new project it works without any problems. It's just not finding any tests in my existing project.

DominicBoettger avatar May 23 '18 13:05 DominicBoettger

#11011 Just some additional testing, rxjs-compat seems to be the culprit for me too.

Ristaaf avatar May 29 '18 11:05 Ristaaf

As mentioned in #11011 by @L2jLiga, I tried downgrading the @angular/cli to 1.7.4 and the tests are running once more. Might be a short term solution for those in a pinch.

EDIT: I spoke too soon, forgot that I was limiting the spec files targeting in test.ts, 1.7.4 does not work for me.

dmfaux avatar May 29 '18 12:05 dmfaux

Holy moly this is an incredibly annoying issue.

Using @AndriiKobzar method, came across a totally blank test file that ended in s.spec.ts. Looks like we have no other test that ended with that format, so running ONLY tests from require.context('(s)\.spec\.ts') means I at least end up with it showing console.log statements from test.js. (It's odd how it wouldn't show those BEFORE changing something in the same file containing those console.log statements? Not sure why? Almost like karma only shows console output when the test file completes, and not if it fails?)

Putting:

it('foo', () => {
  expect(true).toBe(true);
});

in the ONE blank test file allows it to run with one successful test. Not sure if this helps anyone, but my current plan is to just start slowly adding to this test till it breaks.

graypegg avatar May 29 '18 19:05 graypegg

My problem was with basePath property of karma.conf file. With @angular/cli - 1.7.4 I had this field empty and after upgrade Angular to version 6 I was experienced the same failing behaviour on tests. I've filled it in with / and they run πŸ‘Œ now.

danigar avatar May 30 '18 10:05 danigar

I'd like to look at this but really need a repro to investigate. Can someone put up a repro in github please? Then I can clone it and look into the problem.

filipesilva avatar May 31 '18 08:05 filipesilva

@filipesilva you can do reproduce it with these simple steps: https://github.com/angular/angular-cli/issues/10485#issuecomment-386990790

herkulano avatar May 31 '18 09:05 herkulano

@herkulano thank you for directing me to the repro, will look at it.

filipesilva avatar May 31 '18 09:05 filipesilva

@filipesilva to make it simpler I've created a repro with the problem: https://github.com/herkulano/ng-cli-issue-10485

herkulano avatar May 31 '18 09:05 herkulano

@filipesilva Also really simple to reproduce like this:

  1. ng new myapp
  2. edit polyfills.ts and add import "zone.js/dist/zone-patch-rxjs";
  3. npm install rxjs-compat
  4. ng test

Ristaaf avatar May 31 '18 10:05 Ristaaf

Hey all, thanks for the reports. I think I know what was happening. It was two things together:

  • AngularFire2 was importing zone directly (https://github.com/angular/angularfire2/issues/1597). This caused the "Zone already loaded." message.
  • the Angular CLI build system was splitting the common parts of polyfills and test but not loading the common bits. This resulted in no error messages and no tests ran.

The first problem was fixed in https://github.com/angular/angularfire2/pull/1599 and released in [email protected]. The second problem has a fix incoming in https://github.com/angular/devkit/pull/993.

@herkulano I got your repro to work by manually applying the changes in my PR and updating to [email protected].

If you want to try this yourself, open node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/test.js and replace

chunks: buildOptions.commonChunk ? 'all' : 'initial',`

with

chunks: (chunk) => chunk.name !== "polyfills",

@Ristaaf your repro seems to work fine if I add import 'zone.js/dist/zone-patch-rxjs'; after import 'zone.js/dist/zone'; .

filipesilva avatar May 31 '18 11:05 filipesilva

Greak work @filipesilva chunks: (chunk) => chunk.name !== "polyfills" fixed it!! Can't wait for update to update to angular 6 :)

elvirdolic avatar May 31 '18 12:05 elvirdolic

@filipesilva thank you, but you're missing the point.

The solution is not fixing everyone's code. It's not having silent errors so that we can fix it ourselves.

I already knew this was a problem with AngularFire, but the CLI or Karma gave me no pointers on where or why it was failing. I ended up moving to Jest, which had no problems showing exactly what was going on.

herkulano avatar May 31 '18 12:05 herkulano

@herkulano it gave you no pointers because of the problem I am fixing in angular/devkit#993.

the Angular CLI build system was splitting the common parts of polyfills and test but not loading the common bits. This resulted in no error messages and no tests ran.

filipesilva avatar May 31 '18 12:05 filipesilva