Angular 2 Test未执行

时间:2017-01-17 03:13:05

标签: unit-testing angular karma-jasmine

任何人都可以帮助我为什么不执行测试。

我添加了spec文件:' app / 1st.spec.ts'根据angular.io测试web site

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

业力没有问题。 结果如下:

Chrome 55.0.2883 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.002 secs / 0 secs)

我不知道为什么我的项目不起作用,但我在另一个新项目中测试并且执行了单元测试。请指教!

这是我的package.json。我也在使用Angular Universal。

{
  "name": "my-app",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "backend": "nodemon bin/server/index.js",
    "compile": "node_modules/.bin/webpack",
    "dump-task-list": "npm run compile && node bin/server/index.js dump-task-list",
    "server": "npm run compile && npm run backend",
    "server-host": "ng build && ts-node server/server.ts",
    "start": "npm run start-with-server",
    "start-with-server": "npm run dump-task-list && ng serve",
    "lint": "tslint \"src/**/*.ts\" \"log-server/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.0.0",
    "@angular/compiler": "2.0.0",
    "@angular/core": "2.0.0",
    "@angular/forms": "2.0.0",
    "@angular/http": "2.0.0",
    "@angular/platform-browser": "2.0.0",
    "@angular/platform-browser-dynamic": "2.0.0",
    "@angular/router": "3.0.0",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.13",
    "@ngrx/core": "^1.2.0",
    "@ngrx/effects": "^2.0.0",
    "@ngrx/store": "^2.2.1",
    "@ngrx/store-devtools": "^3.1.0",
    "@ngrx/store-log-monitor": "^3.0.2",
    "@progress/kendo-angular-buttons": "^0.13.4",
    "@progress/kendo-angular-grid": "^0.6.3",
    "@progress/kendo-angular-upload": "^0.10.4",
    "@progress/kendo-data-query": "^0.1.7",
    "@telerik/kendo-theme-bootstrap": "^0.10.0",
    "@types/winston": "0.0.32",
    "angular2-express-engine": "^2.0.11",
    "angular2-platform-node": "~2.0.11",
    "angular2-universal": "^2.0.11",
    "angular2-universal-polyfills": "^2.0.11",
    "body-parser": "^1.15.2",
    "bootstrap": "^4.0.0-alpha.5",
    "cheerio": "^0.22.0",
    "cookie-parser": "^1.4.3",
    "core-js": "^2.4.1",
    "express": "^4.14.0",
    "jsnlog": "^2.20.1",
    "node-uuid": "^1.4.7",
    "pouchdb": "^6.0.7",
    "preboot": "^4.5.2",
    "rxjs": "5.0.0-beta.12",
    "signalr": "^2.2.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "@angularclass/resolve-angular-routes": "^1.0.9",
    "@types/body-parser": "0.0.29",
    "@types/chai": "^3.4.34",
    "@types/cheerio": "^0.17.31",
    "@types/compression": "0.0.29",
    "@types/cookie-parser": "^1.3.30",
    "@types/express": "^4.0.32",
    "@types/express-serve-static-core": "^4.0.33",
    "@types/hammerjs": "^2.0.32",
    "@types/jasmine": "^2.2.30",
    "@types/mime": "0.0.28",
    "@types/mkdirp": "^0.3.29",
    "@types/morgan": "^1.7.32",
    "@types/node": "^6.0.38",
    "@types/pouchdb": "^5.4.28",
    "@types/serve-static": "^1.7.27",
    "@types/uuid": "^2.0.29",
    "angular-cli": "1.0.0-beta.16",
    "angular2-schema-form": "1.0.0-alpha.32",
    "angular2-template-loader": "^0.4.0",
    "codelyzer": "~0.0.26",
    "concurrently": "^3.1.0",
    "imports-loader": "^0.6.5",
    "jasmine-core": "2.4.1",
    "jasmine-spec-reporter": "2.5.0",
    "json-loader": "^0.5.4",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "nodemon": "^1.10.0",
    "protractor": "4.0.9",
    "raw-loader": "^0.5.1",
    "rimraf": "^2.5.4",
    "string-replace-loader": "github:gdi2290/string-replace-loader",
    "ts-loader": "^0.8.2",
    "ts-node": "1.2.1",
    "tslint": "3.13.0",
    "typescript": "2.0.2",
    "webpack": "2.1.0-beta.22",
    "winston": "^2.3.0",
    "winston-daily-rotate-file": "^1.3.1"
  }
}

这是我的karma.config

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'angular-cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-remap-istanbul'),
      require('angular-cli/plugins/karma')
    ],
    files: [
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['angular-cli']
    },
    remapIstanbulReporter: {
      reports: {
        html: 'coverage',
        lcovonly: './coverage/coverage.lcov'
      }
    },
    angularCli: {
      config: './angular-cli.json',
      environment: 'dev'
    },
    reporters: ['progress', 'karma-remap-istanbul'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

这里是test.ts

import './polyfills.ts';

import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';

// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare var __karma__: any;
declare var require: any;

// Prevent Karma from running prematurely.
__karma__.loaded = function () {};


Promise.all([
  System.import('@angular/core/testing'),
  System.import('@angular/platform-browser-dynamic/testing'),
])
  // First, initialize the Angular testing environment.
  .then(([testing, testingBrowser]) => {
    testing.getTestBed().initTestEnvironment(
      testingBrowser.BrowserDynamicTestingModule,
      testingBrowser.platformBrowserDynamicTesting()
    );
  })
  // Then we find all the tests.
  .then(() => require.context('./', true, /\.spec\.ts/))
  // And load the modules.
  .then(context => context.keys().map(context))
  // Finally, start Karma to run the tests.
  .then(__karma__.start, __karma__.error);

3 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。

在karma.conf.js上试试这个:

mime: {
  'text/x-typescript': ['ts','tsx'],
  'text/javascript': ['js']
},

答案 1 :(得分:0)

在你的karma.config中进行以下更改 -

files: [
      { pattern: './src/[path_to_your_app_folder]/*.spec.ts', watched: false }
    ]
preprocessors: {
      './tests/**/*.spec.js': ['angular-cli']
    }

它将解决您的问题。

答案 2 :(得分:0)

就我而言:

添加了:

    mime: {
        'text/x-typescript': ['ts', 'tsx']
    },

编辑:

reporters: config.angularCli && config.angularCli.codeCoverage ?
            ['progress', 'karma-remap-istanbul'] :
            ['progress'],

现在测试工作正常。