javascript - karma 不执行任何测试

标签 javascript angular typescript karma-jasmine

我在运行任何测试时都遇到了问题,因为我已经毫无问题地启动了 Karma。我可以在命令行中看到确认我的规范文件,但是,没有运行任何测试,看到“Chrome 53.0.2785 (Windows 7 0.0.0): Executed 0 of 0 ERROR (0.005 secs/0 secs)”:

> testingexample@0.0.1 test C:\src\testingexample
> tsc && concurrently "tsc -w" "karma start karma.conf.js"

[1] [WARNING] Looking up paths with require.resolve() is deprecated.
[1] Please add "systemjs" to your SystemJS config paths.
[1] [WARNING] Looking up paths with require.resolve() is deprecated.
[1] Please add "traceur" to your SystemJS config paths.
[1] 27 09 2016 16:06:07.265:WARN [karma]: No captured browser, open http://localhost:9876/
[1] 27 09 2016 16:06:07.307:WARN [karma]: Port 9876 in use
[1] 27 09 2016 16:06:07.309:INFO [karma]: Karma v1.3.0 server started at http://localhost:9877/
[1] 27 09 2016 16:06:07.309:INFO [launcher]: Launching browser Chrome with unlimited concurrency
[1] 27 09 2016 16:06:07.364:INFO [launcher]: Starting browser Chrome
[1] 27 09 2016 16:06:08.151:INFO [Chrome 53.0.2785 (Windows 7 0.0.0)]: Connected on socket /#r4UrKTjwWvF4GtokAAAA with i
d 75686393
    Chrome 53.0.2785 (Windows 7 0.0.0): Executed 0 of 0 ERROR (0.004 secs / 0 secs)
[1] 27 09 2016 16:06:10.172:INFO [watcher]: Changed file "C:/src/testingexample/test/1st.spec.js".
[1] 27 09 2016 16:06:10.194:INFO [watcher]: Changed file "C:/src/testingexample/test/components/header/header.spec.js".
[0] 4:06:10 PM - Compilation complete. Watching for file changes.
    Chrome 53.0.2785 (Windows 7 0.0.0): Executed 0 of 0 ERROR (0.005 secs / 0 secs)

项目结构:

app/
├── common/
├── components/
|   └── header/
|       ├── header.component.css
|       ├── header.component.html
|       └── header.component.ts
test/
├── components/
|   └── header/
|       └── header.spec.ts
├── models/
└── services/
karma-test-shim.js
karma.conf.js
systemjs.config.js

packages.json(可能因试图让一切无误地开始而受到污染):

{
  "name": "testingexample",
  "version": "0.0.1",
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
    "test-once": "tsc && karma start karma.conf.js --single-run",
    "lite": "lite-server",
    "lint": "tslint ./app/**/*.ts -t verbose",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "license": "ISC",
  "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",
    "@angular/upgrade": "2.0.0",
    "systemjs": "0.19.27",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.12",
    "zone.js": "^0.6.23",
    "angular2-in-memory-web-api": "0.0.20",
    "bootstrap": "^3.3.6"
  },
  "devDependencies": {
    "brfs": "^1.4.3",
    "browserify": "^13.1.0",
    "concurrently": "^2.2.0",
    "http-server": "^0.9.0",
    "jasmine": "^2.5.2",
    "jasmine-core": "^2.4.1",
    "karma": "^1.2.0",
    "karma-browserify": "^5.1.0",
    "karma-chrome-launcher": "^0.2.3",
    "karma-cli": "^0.1.2",
    "karma-htmlfile-reporter": "^0.2.2",
    "karma-jasmine": "^0.3.8",
    "karma-systemjs": "^0.15.0",
    "lite-server": "^2.2.0",
    "phantomjs": "^2.1.7",
    "protractor": "^3.3.0",
    "traceur": "0.0.111",
    "tslint": "^3.7.4",
    "typescript": "^2.0.2",
    "typings": "^1.0.4",
    "watchify": "^3.7.0"
  }
}

karma-test-shim.js(这看起来很标准):

// #docregion
// /*global jasmine, __karma__, window*/
Error.stackTraceLimit = 0;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

var builtPath = '/base/app/';

__karma__.loaded = function () { };

function isJsFile(path) {
  return path.slice(-3) == '.js';
}

function isSpecFile(path) {
  return /\.spec\.(.*\.)?js$/.test(path);
}

function isBuiltFile(path) {
  return isJsFile(path) && (path.substr(0, builtPath.length) == builtPath);
}

var allSpecFiles = Object.keys(window.__karma__.files)
  .filter(isSpecFile)
  .filter(isBuiltFile);

System.config({
  baseURL: '/base',
  // Extend usual application package list with test folder
  packages: { 'testing': { main: 'index.js', defaultExtension: 'js' } },

  // Assume npm: is set in `paths` in systemjs.config
  // Map the angular testing umd bundles
  map: {
    '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
    '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
    '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
    '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
    '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
    '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
    '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
    '@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js',
  },
});

System.import('systemjs.config.js')
  .then(importSystemJsExtras)
  .then(initTestBed)
  .then(initTesting);

/** Optional SystemJS configuration extras. Keep going w/o it */
function importSystemJsExtras(){
  return System.import('systemjs.config.extras.js')
  .catch(function(reason) {
    console.log(
      'WARNING: System.import could not load "systemjs.config.extras.js"; continuing without it.'
    );
    console.log(reason);
  });
}

function initTestBed(){
  return Promise.all([
    System.import('@angular/core/testing'),
    System.import('@angular/platform-browser-dynamic/testing')
  ])

  .then(function (providers) {
    var coreTesting    = providers[0];
    var browserTesting = providers[1];

    coreTesting.TestBed.initTestEnvironment(
      browserTesting.BrowserDynamicTestingModule,
      browserTesting.platformBrowserDynamicTesting());
  })
}

// Import all spec files and start karma
function initTesting () {
  return Promise.all(
    allSpecFiles.map(function (moduleName) {
      return System.import(moduleName);
    })
  )
  .then(__karma__.start, __karma__.error);
}

karma .conf.js:

// #docregion
module.exports = function(config) {

  var appBase    = 'app/';       // transpiled app JS and map files
  var appSrcBase = 'app/';       // app source TS files
  var appAssets  = 'app/'; // component assets fetched by Angular's compiler

  var testBase    = 'test/';       // transpiled test JS and map files
  var testSrcBase = 'test/';       // test source TS files

  config.set({
    basePath: '',
    frameworks: ['systemjs','jasmine'],
    plugins: [
      require('karma-systemjs'),
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-htmlfile-reporter'),
      require('karma-browserify'),
      require('browserify')
    ],

    customLaunchers: {
      // From the CLI. Not used here but interesting
      // chrome setup for travis CI using chromium
      Chrome_travis_ci: {
        base: 'Chrome',
        flags: ['--no-sandbox']
      }
    },
    files: [
      // System.js for module loading
      'node_modules/systemjs/dist/system-polyfills.js',
      'node_modules/systemjs/dist/system.src.js',

        // Reflect and Zone.js
      'node_modules/reflect-metadata/Reflect.js',

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

      // RxJs.
      { pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
      { pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },

      // Angular 2 itself and the testing library
      {pattern: 'node_modules/@angular/**/*.js', included: false, watched: false},
      {pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false},

      'karma-test-shim.js',


      'test/**/*spec.js'
    ],

    // Proxied base paths for loading assets
    proxies: {
      // required for component assets fetched by Angular's compiler
      "/app/": appAssets
    },

    exclude: [],
    preprocessors: {

    },

    reporters: ['progress', 'html'],

    // HtmlReporter configuration
    htmlReporter: {
      // Open this file to see results in browser
      outputFile: '_test-output/tests.html',

      // Optional
      pageTitle: 'Unit Tests',
      subPageTitle: __dirname
    },

    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  })
}

最后,systemjs.config.js:

(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs':                       'npm:rxjs',
      'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      'angular2-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

我知道要经历很多事情 - 我希望我做的事情明显是错误的。

最佳答案

我认为这是因为 karma 不会像测试文件那样查看您的规范文件。

当它告诉您它检测到变化时:

[1] 27 09 2016 16:06:10.194:INFO [watcher]: Changed file "C:/src/testingexample/test/components/header/header.spec.js".

这并不意味着它是一个测试文件。

告诉karma哪些文件是测试文件,可以在karma-test-shim.js

中查看
var allSpecFiles = Object.keys(window.__karma__.files)
  .filter(isSpecFile)
  .filter(isBuiltFile);

它告诉 karma 加载所有文件,然后只保留那些文件

  • isSpecFile :以 .spec.js
  • 结尾的东西
  • isBuiltFile :构建路径 /base/app/
  • 下的东西

在您的情况下,您的测试不在 app 目录中,因此您必须删除 .filter(isBuiltFile) 以便 karma 可以处理您的 spec .js 作为测试。

我查看了自己的 karma 测试项目,发现了一些差异。

在我的karma.conf.js

  • 我没有任何 SystemJS 插件。

    frameworks: ['jasmine'],
    plugins   : [
        require('karma-jasmine'),
        require('karma-chrome-launcher')
    ],
    
  • 我已将编译的应用程序和测试 JS 添加到 files 以代替您的 'test/**/*spec.js'

    {pattern: appBase + '**/*.js', included: false, watched: true},
    {pattern: testBase + '**/*.js', included: false, watched: true},
    

    我不知道 karma 是否允许不使用 {pattern: 'test/**/*spec.js', ...} 的模式。

在我的karma-test-shim.js

  • 我不使用 systemjs.config.extras.js,所以我删除了所有相关代码。

关于javascript - karma 不执行任何测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39732570/

相关文章:

javascript - 使用 javascript 记录和检索 html 元素/节点路径

JavaScript 将值发送到另一个函数

javascript - Angular 路由器出现 404 错误?

Angular 12 SSR | Angular 12 SSR |页眉页脚在加载程序到来之前显示

javascript - 将焦点移至 Enter 键上的下一个控件

javascript - jquery 选择器需要从元素中选择下一个表

javascript - Cypress - 等待取消的 XHR 请求

node.js - 使用 ASP.Net Core 应用程序填充 Angular 上的下拉列表时出现错误 TS2717

javascript - Ionic 3 - 应用程序在使用 --prod 构建后显示空白屏幕

具有特定步骤的Javascript for循环