Ionic 2 : Test with jasmine and karma error 'ng test'

时光总嘲笑我的痴心妄想 提交于 2019-12-08 06:33:36

问题


I try to follow this tutorial :

http://lathonez.com/2017/ionic-2-unit-testing/

And when I run "ng test" I have this error :

C:\xampp\htdocs\AppFineMobile>ng test
27 03 2017 11:55:00.556:ERROR [preprocess]: Can not load "angular-cli", it is not registered!
  Perhaps you are missing some plugin?
No provider for "framework:angular-cli"! (Resolving: framework:angular-cli)
Error: No provider for "framework:angular-cli"! (Resolving: framework:angular-cli)
    at error (C:\xampp\htdocs\AppFineMobile\node_modules\di\lib\injector.js:22:12)
    at Object.get (C:\xampp\htdocs\AppFineMobile\node_modules\di\lib\injector.js:9:13)
    at Injector.get (C:\xampp\htdocs\AppFineMobile\node_modules\di\lib\injector.js:54:19)
    at C:\xampp\htdocs\AppFineMobile\node_modules\karma\lib\server.js:143:20
    at Array.forEach (native)
    at Server._start (C:\xampp\htdocs\AppFineMobile\node_modules\karma\lib\server.js:142:21)
    at Injector.invoke (C:\xampp\htdocs\AppFineMobile\node_modules\di\lib\injector.js:75:15)
    at Server.start (C:\xampp\htdocs\AppFineMobile\node_modules\karma\lib\server.js:103:18)
    at Promise (C:\xampp\htdocs\AppFineMobile\node_modules\@angular\cli\tasks\test.js:35:25)
    at Class.run (C:\xampp\htdocs\AppFineMobile\node_modules\@angular\cli\tasks\test.js:15:16)
    at Class.run (C:\xampp\htdocs\AppFineMobile\node_modules\@angular\cli\commands\test.js:99:25)
    at Class.<anonymous> (C:\xampp\htdocs\AppFineMobile\node_modules\@angular\cli\ember-cli\lib\models\command.js:134:17)
    at process._tickCallback (internal/process/next_tick.js:103:7)

I don't understand what I have to do. I had run this command at first :

npm install --save-dev @angular/cli @types/jasmine @types/node jasmine-core jasmine-spec-reporter karma karma-chrome-launcher karma-cli karma-jasmine karma-jasmine-html-reporter karma-coverage-istanbul-reporter

It work well, and I add all file mentioned in the tutorial ...

In my test.ts file I have an error in my import :

import { getTestBed, TestBed } from '@angular/core/testing';

I have : Cannot find module '@angular/core/testing', but it exist in my module folder...

Someone have an idea ?


回答1:


There looks to be three ways of unit testing ionic 2 projects going around at the moment.

  1. angular-cli ( deprecated but works fine for me )
  2. @angular/cli ( new shiny but tricky to upgrade too if you have an angular-cli setup already ) http://lathonez.com/2017/ionic-2-unit-testing/
  3. Using scripts called by "ionic test" ( see ionic blog for details and link to test example ) http://blog.ionic.io/basic-unit-testing-in-ionic/

There will soon be another way too

ionic blog: "We’re also working on baking unit testing right into the Ionic CLI (or more accurately, @ionic/app-scripts). Stay tuned for that."

If you can wait a bit I would wait for Ionic CLI version. It's likely to give you the least amount of problems long run, as the ionic team will keep it running will all the new shiny ionic goodness that comes along.




回答2:


I think your problem is that the karma.conf.js is still pointing at the old angular-cli this has been renamed to @angular/cli.

If you change all the angular-cli to @angular/cli in your karma.conf.js file that should point you in the right direction.

The format of the karma.conf.js has also changed so it may need to be updated.

For Update docs see https://github.com/angular/angular-cli/wiki/stories-rc.0-update



来源:https://stackoverflow.com/questions/43043428/ionic-2-test-with-jasmine-and-karma-error-ng-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!