Angular 2业力设置问题 - 业力开始,但没有运行测试

时间:2017-01-15 01:36:37

标签: unit-testing angular typescript karma-jasmine

简而言之,

  • 我创建了一个以Angular的Plunker代码开头的项目 2"路由"示例(通过the Angular 2 Advanced Tutorial for Routing链接访问)。该来源没有Karma或Jasmine 配置烘焙。

  • 我一直在尝试将其添加到现有内容中 项目。我当前的设置运行业力,但没有看到任何测试,.js或.ts。

14 01 2017 19:16:44.080:WARN [karma]: No captured browser, open http://localhost:9876/
14 01 2017 19:16:44.112:INFO [karma]: Karma v1.4.0 server started at http://0.0.0.0:9876/
14 01 2017 19:16:44.112:INFO [launcher]: Launching browser Chrome with unlimited concurrency
14 01 2017 19:16:44.268:INFO [launcher]: Starting browser Chrome
14 01 2017 19:16:46.895:INFO [Chrome 55.0.2883 (Windows 10 0.0.0)]: Connected on socket [SNIP]

首先,我开始使用the Angular 2 Advanced Tutorial for Testing链接。那说我应该创建/app/1st.spec.ts:

describe('lst tests', () => {
    it('true is true', () => expect(false).toBe(true));
});

然后我运行了npm测试,但发现我没有Karma功能。从那以后,我根据advice from a programmer's blog安装了各种软件包。我的装置已成为:

npm install karma –save-dev
npm install karma-jasmine jasmine-core –save-dev
npm install karma-chrome-launcher –save-dev 
npm install karma-cli
npm install angular-mocks
npm install requirejs karma-requirejs

我跑了" karma init"我尽力回答问题。我目前的karma.conf.js是(浓缩):

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'requirejs'],
    files: [
      {pattern: 'app/*.js', included: false},
      {pattern: 'app/*.spec.js', included: false},
      {pattern: 'app/**/*.js', included: false}
    ],
    exclude: [ ],
    preprocessors: { },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    concurrency: Infinity
  })
}

我的package.json是:

{
  "name": "MyProgram",
  "version": "0.1.0",
  "license": "MIT",
  "description": "MyProgram description",
    "scripts": {
      "start": "gulp copy-jit && concurrently \"npm run tsc:w\" \"npm run lite\" ",
      "lite": "lite-server",
      "tsc": "tsc",
      "tsc:w": "tsc -w",
      "aot": "ngc -p tsconfig-aot.json",
      "rollup": "rollup -c rollup-config.js",
      "start-aot": "npm run aot && npm run rollup && gulp copy-aot && npm run lite",
      "test": "karma start karma.conf.js"
  },
  "dependencies": {
    "@angular/common": "~2.2.4",
    "@angular/compiler": "~2.2.4",
    "@angular/compiler-cli": "~2.2.4",
    "@angular/core": "~2.2.4",
    "@angular/forms": "~2.2.4",
    "@angular/http": "~2.2.4",
    "@angular/platform-browser": "~2.2.4",
    "@angular/platform-browser-dynamic": "~2.2.4",
    "@angular/platform-server": "^2.2.4",
    "@angular/router": "~3.2.4",
    "@angular/upgrade": "~2.2.4",
    "angular-in-memory-web-api": "~0.1.5",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "zone.js": "^0.6.25"
  },
  "devDependencies": {
    "@types/core-js": "^0.9.34",
    "@types/jasmine": "^2.5.35",
    "@types/node": "^6.0.45",
    "@types/selenium-webdriver": "^2.53.32",
    "concurrently": "^3.0.0",
    "del": "^2.2.2",
    "gulp": "^3.9.1",
    "gulp-gzip": "^1.4.0",
    "gulp-load-plugins": "^1.3.0",
    "gulp-rename": "^1.2.2",
    "gulp-task-listing": "^1.0.1",
    "gulp-util": "^3.0.7",
    "jasmine-core": "^2.5.2",
    "karma": "^1.4.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-jasmine": "^1.1.0",
    "karma-requirejs": "^1.1.0",
    "lite-server": "^2.2.2",
    "rollup": "^0.36.1",
    "rollup-plugin-commonjs": "^5.0.4",
    "rollup-plugin-node-resolve": "^2.0.0",
    "rollup-plugin-uglify": "^1.0.1",
    "typescript": "^2.0.3"
  }
}

我希望通过您的回复解决的问题是:

  • 适用于Karma和Jasmine的Angular 2项目配置 测试
  • 能够创建Typescript源代码测试并在Karma中检测并运行它们。

谢谢,

杰罗姆。

1 个答案:

答案 0 :(得分:0)

  • 我也试图通过这个过程。我知道如果您使用Angular CLI执行 ng new ,它将为您创建一个项目,并正确设置所有Karma内容。
  • ng测试类似于 npm测试,但似乎也快得多。
  • package.json“scripts”部分控制“test”命令行选项。
  • 我试图将它复制到Tour of Heroes第6部分并设法成功完成1st.spec.ts并让Karma启动并将true = true转换为true = false。
  • 但是在它说你可以在Chrome中加载1st.spec.ts时,事情对我来说也不起作用(第5点here)。
  • 我记录了在here之间复制文件的方法。
  • 您还有一个额外的麻烦,即Karma使用了一个使用LTS发布周期落后于时代的Node版本。所以设置nvm是明智的。请参阅here