ionic-team / ionic-unit-testing-example

Example of adding unit testing in your Ionic 2.x or greater apps with Karma and Jasmine
Other
374 stars 145 forks source link

error was thrown in afterAll with angular 5 #79

Closed biaggi closed 6 years ago

biaggi commented 6 years ago

I've followed the instructions, but when I execute npm run test, I'm receiving the following error:

> starter-with-testing@0.0.1 test-ci /home/jmgomez/securitas/test/starter-with-testing
> karma start ./test-config/karma.conf.js --single-run

webpack: Compiled successfully.
webpack: Compiling...

webpack: Compiled with warnings.
16 02 2018 15:24:04.909:INFO [karma]: Karma v2.0.0 server started at http://0.0.0.0:9876/
16 02 2018 15:24:04.910:INFO [launcher]: Launching browser Chrome with unlimited concurrency
16 02 2018 15:24:04.931:INFO [launcher]: Starting browser Chrome
16 02 2018 15:24:05.989:INFO [Chrome 64.0.3282 (Linux 0.0.0)]: Connected on socket 6K6qqAOzO0RIRsN8AAAA with id 89855277
Chrome 64.0.3282 (Linux 0.0.0) ERROR
  {
    "message": "An error was thrown in afterAll\nUncaught TypeError: env.catchExceptions is not a function",
    "str": "An error was thrown in afterAll\nUncaught TypeError: env.catchExceptions is not a function"
  }
Chrome 64.0.3282 (Linux 0.0.0) ERROR
  {
    "message": "An error was thrown in afterAll\nUncaught TypeError: env.catchExceptions is not a function",
    "str": "An error was thrown in afterAll\nUncaught TypeError: env.catchExceptions is not a function"
  }
Chrome 64.0.3282 (Linux 0.0.0): Executed 0 of 0 ERROR (0.004 secs / 0 secs)
Chrome 64.0.3282 (Linux 0.0.0): Executed 0 of 0 ERROR (0.016 secs / 0 secs)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! starter-with-testing@0.0.1 test-ci: `karma start ./test-config/karma.conf.js --single-run`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the starter-with-testing@0.0.1 test-ci 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!     /home/jmgomez/.npm/_logs/2018-02-16T14_24_06_588Z-debug.log

Result for ionic info is the following one:

cli packages: (/home/jmgomez/.nvm/versions/node/v8.9.1/lib/node_modules)

    @ionic/cli-utils  : 1.19.1
    ionic (Ionic CLI) : 3.19.1

local packages:

    @ionic/app-scripts : 3.1.8
    Ionic Framework    : ionic-angular 3.9.2

System:

    Node : v8.9.1
    npm  : 5.6.0 
    OS   : Linux 4.4

Misc:

    backend : pro

My android version is 5 instead of 4 as you can check in the package.json uploaded in the repository https://github.com/biaggi/karma-test-failing. Any hint on what's happening there?

varghese-dev commented 6 years ago

@biaggi I was getting similar error and downgrading the jasmine to 2.99 version solved the issue for me. "jasmine": "2.99".

Try it.

biaggi commented 6 years ago

@varnit-abraham Thank you for your help, but seems that the problem is a different one, the npm list it's already reporting that jasmine's version is 2.99

npm list

...
├─┬ jasmine@2.99.0
│ ├── exit@0.1.2
│ ├── glob@7.1.2 deduped
│ └── jasmine-core@2.99.1
...
devinshoemaker commented 6 years ago

Thank you @varnit-abraham that seems to have fixed my issues, but do you know why this was causing issues? There must be some breaking change with Jasmine 3.0.0 but I'm not sure what library it's conflicting with.

anibalsanchez commented 6 years ago

Same issue here, and the version downgrade also solved the case:

npm install --save-dev jasmine@^2.99.0
biaggi commented 6 years ago

You are right... there must be some sort of dependency there that depends directly from jasmine@2.99.0

It works installing such package. Thank you all and I'm sorry for not trying it before

igcarun07 commented 6 years ago

@biaggi after install jasmine@2.99.0 it show cannot read property has undefined. Thanks in advance

hyungryuk-zz commented 6 years ago

@biaggi i solved it!! after install jasmine@2.99.0, install jasmine-core@^2.99.0 as well! try it

"jasmine": "^2.99.0", "jasmine-core": "^2.99.0",

ChrisGibb commented 6 years ago

Looks like might be a problem with karma-jasmine-html-reporter

{
    "message": "Uncaught TypeError: env.catchExceptions is not a function\nat node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:24:9\n\nTypeError: env.catchExceptions is not a function\n    at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:24:9\n    at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:58:3",
    "str": "Uncaught TypeError: env.catchExceptions is not a function\nat node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:24:9\n\nTypeError: env.catchExceptions is not a function\n    at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:24:9\n    at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:58:3"
  }
npm ls --depth=0 | grep jasmine

├── @types/jasmine@2.8.6
├── @types/jasminewd2@2.0.3
├── jasmine-core@3.1.0
├── jasmine-spec-reporter@4.2.1
├── karma-jasmine@1.0.2
├── karma-jasmine-diff-reporter@1.1.1
├── karma-jasmine-html-reporter@0.2.2

Reverting back fixes the issue

npm install karma@^2.99
npm install jasmine-core@^2.99
jackrvaughan commented 6 years ago

"jasmine": "^2.99.0", "jasmine-core": "^2.99.0"

Doesn't seem to fix it for me :/

leifwells commented 6 years ago

@jackrvaughan Make sure you downgrade your ts-loader to 3.5.0.

Take a look at my PR here: (https://github.com/ionic-team/ionic-unit-testing-example/pull/87)[https://github.com/ionic-team/ionic-unit-testing-example/pull/87]

jackrvaughan commented 6 years ago

EDIT: I had an unrelated issue (one of my tests was broken) that was throwing this same error. Sorry!

Thanks for the reply @leifwells !

I just noticed that this was an ionic specific thread. I don't use ionic, webpack, or ts-loader; but am hitting this issue all the same.

  "dependencies": {
    "@angular/animations": "^5.1.0",
    "@angular/common": "^5.1.0",
    "@angular/compiler": "^5.1.0",
    "@angular/core": "^5.1.0",
    "@angular/forms": "^5.1.0",
    "@angular/http": "^5.1.0",
    "@angular/platform-browser": "^5.1.0",
    "@angular/platform-browser-dynamic": "^5.1.0",
    "@angular/router": "^5.1.0",
    "core-js": "^2.4.1",
    "raven-js": "^3.22.3",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "^1.7.3",
    "@angular/compiler-cli": "^5.1.0",
    "@angular/language-service": "^5.1.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~4.0.1",
    "jasmine": "^2.99.0",
    "jasmine-core": "^2.99.0",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.3.3",
    "karma-jasmine": "^1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "^2.5.3"
  }
jdnav commented 5 years ago

I was facing the same error. This combination worked for me in v7:

./package.json

{
...
  "dependencies": {
    "@angular/animations": "~7.0.0",
    "@angular/common": "~7.0.0",
    "@angular/compiler": "~7.0.0",
    "@angular/core": "~7.0.0",
    "@angular/forms": "~7.0.0",
    "@angular/http": "~7.0.0",
    "@angular/platform-browser": "~7.0.0",
    "@angular/platform-browser-dynamic": "~7.0.0",
    "@angular/router": "~7.0.0",
    "core-js": "^2.5.4",
    "rxjs": "~6.3.3",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.10.0",
    "@angular/cli": "~7.0.4",
    "@angular/compiler-cli": "~7.0.0",
    "@angular/language-service": "~7.0.0",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.1.1"
  }
}

Good luck!

vipinsingh01992 commented 5 years ago

Facing this issue after doing all: An error was thrown in afterAll Uncaught TypeError: Cannot read property 'toLowerCase' of undefined thrown

AshuSingh07 commented 4 years ago

I added ts-loader:3.5.0 and typescript: 2.8.4 and it worked for me in Angular 5, ionic 3, Karma 1.71 and jasmine2.99.0 :

"devDependencies": { "@babel/register": "7.8.3", "@ionic/app-scripts": "^3.2.3", "@types/crypto-js": "3.1.43", "@types/es6-promise": "3.3.0", "@types/jasmine": "^2.8.6", "@types/node": "8.10.45", "@types/request-promise": "4.1.45", "angular2-template-loader": "0.6.2", "es6-template-strings": "2.0.1", "html-loader": "0.5.5", "ionic-mocks": "1.3.0", "istanbul-instrumenter-loader": "3.0.1", "jasmine": "2.99.0", "jasmine-core": "^2.99.0", "jasmine-spec-reporter": "4.2.1", "karma": "1.7.1", "karma-chrome-launcher": "2.2.0", "karma-coverage-istanbul-reporter": "1.4.3", "karma-firefox-launcher": "^1.3.0", "karma-jasmine": "1.1.2", "karma-jasmine-html-reporter": "0.2.2", "karma-sourcemap-loader": "^0.3.7", "karma-spec-reporter": "0.0.32", "karma-webpack": "2.0.13", "ts-loader": "^3.5.0", "typescript": "^2.8.4" },

Atul-Lohiya commented 2 years ago

Seems like angular/karma/jasmine still have this issue. I was using angular 10, jasmine 3.6, karma 5 and still ran into this issue. I did not write the afterAll anywhere and yet this was failing. The reason was async operations that were returning Observable.

Fix was to pass "done" function parameter to all test cases which were expecting aboe async behaviour and calling "done()" at the end of it something like below.

it('should not get any data', (done) => { SetupMockActivatedRoute_ParamMap(id, "someParamValue"); SetupDataFetcherService_GetData(id, someOtherParam); component.ngOnInit(); expect(component.data).toBeFalsy(); done(); });

Here, ngOnInit makes ajax call to get some data. Above test case expects to not get any data and instead show some message to user hence the falsy assertion. I had to to this in all of my test cases where Observable was being used.

Additionally, there were alot of default generated tests that were not taking care of this. So I cleaned up those to either remove them or fix them like above. And then I fixed all other such cases and now it works everytime in my CICD pipeline.

So in general if you are getting this error, chances are some mocking is not right or you are not using done() function.

Also, for reference, read @FDIM's comments at https://github.com/angular/angular/issues/33800.