Unit testing 注入时引发错误<;平台>;在Ionic中服务的单元测试中

Unit testing 注入时引发错误<;平台>;在Ionic中服务的单元测试中,unit-testing,ionic3,Unit Testing,Ionic3,我刚刚开始用Ionic 3开发我的第一个应用程序,现在正在进行一些单元测试。现在,当平台是它的一个依赖项时,我面临着一个“编译”服务的单元测试的问题 以下是服务的摘要: @Injectable() export class MyService { constructor(public platform: Platform) {} myMethod(): Observable<any> { if (this.platform.is('android')) {

我刚刚开始用Ionic 3开发我的第一个应用程序,现在正在进行一些单元测试。现在,当
平台
是它的一个依赖项时,我面临着一个“编译”
服务
的单元测试的问题

以下是
服务的摘要

@Injectable()
export class MyService {
  constructor(public platform: Platform) {}

  myMethod(): Observable<any> {
    if (this.platform.is('android')) {
      return new Observable(android);
    else if (this.platform.is('ios')) {
      return new Observable(iOS);
    }
  }
}
我的测试配置如下所示。我还有其他一些
spec
文件成功运行,除了这个文件引发以下错误:

PS C:\Workspaces\JS-TS\taccess> npm run test

> taccess@0.0.1 test C:\Workspaces\JS-TS\taccess
> karma start ./test-config/karma.conf.js --single-run

(node:17164) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
‼ 「wdm」:
i 「wdm」: Compiled with warnings.
i 「wdm」: Compiling...
‼ 「wdm」:
i 「wdm」: Compiled with warnings.
26 07 2018 07:20:43.963:INFO [karma]: Karma v2.0.5 server started at http://0.0.0.0:9876/
26 07 2018 07:20:44.003:INFO [launcher]: Launching browser Chrome with unlimited concurrency
26 07 2018 07:20:44.806:INFO [launcher]: Starting browser Chrome
26 07 2018 07:20:54.988:INFO [Chrome 68.0.3440 (Windows 10 0.0.0)]: Connected on socket u2KlYyff-qRa_fnHAAAA with id 6508927
Chrome 68.0.3440 (Windows 10 0.0.0) ERROR
  {
    "message": "An error was thrown in afterAll\nUncaught Error: Cannot find module './components/app/app-root'",
    "str": "An error was thrown in afterAll\nUncaught Error: Cannot find module './components/app/app-root'"
  }
Chrome 68.0.3440 (Windows 10 0.0.0) ERROR
  {
    "message": "An error was thrown in afterAll\nUncaught Error: Cannot find module './components/app/app-root'",
    "str": "An error was thrown in afterAll\nUncaught Error: Cannot find module './components/app/app-root'"
  }

Chrome 68.0.3440 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.008 secs / 0 secs)


Chrome 68.0.3440 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.142 secs / 0 secs)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! taccess@0.0.1 test: `karma start ./test-config/karma.conf.js --single-run`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the taccess@0.0.1 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\xxx\AppData\Roaming\npm-cache\_logs\2018-07-26T05_20_56_762Z-debug.log
为什么我需要将
平台
注入服务?因为我使用的是Cordova插件,而且iOS和Android之间存在一些差异,超出了我需要管理的范围

有人知道发生了什么事吗?我做错了什么?


注意1:我已经检查了如何使用
平台删除依赖项,测试(由于缺少
平台
)运行正常。

过了一段时间,我发现了问题所在:Visual Studio代码

由于某些原因,VSCode更改了此导入

import { Platform } from 'ionic-angular';
对另一个

import { Platform } from 'ionic-angular/umd';
当我移除
/umd
的时候,一切都像一个魔咒

希望这对以后的其他人有帮助

import { Platform } from 'ionic-angular/umd';