在 Cordova 项目上执行 运行 Karma 时出现 0 个错误,共 0 个错误

Executed 0 of 0 ERROR when running Karma on Cordova project

我有一个 apache cordova 项目设置,我是第一次尝试使用 Karma。我是这样开始这个项目的:

karma start test/karma.conf.js --verbose

但我得到的只是:

INFO [karma]: Karma v0.12.36 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [web-server]: 404: /favicon.ico
INFO [Chrome 43.0.2357 (Windows 8.1 0.0.0)]: Connected on socket 0_IDnS6qvPg4OhGd7oq4 with id 66015767
Chrome 43.0.2357 (Windows 8.1 0.0.0): Executed 0 of 0 ERROR (0.002 secs / 0 secs)

我已经仔细检查了我对 the docs for configuring requirejs for karma 所做的事情。

karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '..',
    frameworks: ['jasmine', 'requirejs'],
    files: [
      {pattern: 'www/lib/ionic/js/*.min.js', included: false},
      {pattern: 'www/lib/ionic/js/angular/*.min.js', included: false},
      {pattern: 'www/lib/ionic/js/angular-ui/*.min.js', included: false},
      {pattern: 'www/js/**/*.js', included: false},
      {pattern: 'test/specs/*Spec.js', included: false},
      {pattern: 'test/test-app.js', included: true}
    ],
    exclude: [
      'www/js/app.js'
    ],
    preprocessors: {
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

测试-app.js

var tests = [];
for(var file in window.__karma__.files) {
    if(window.__karma__.files.hasOwnProperty(file)) {
        if(/Spec\.js$/.test(file)) {
            tests.push(file);
        }
    }
}

requirejs.config({
    baseUrl: 'www',
    paths: {
        'angular': 'lib/ionic/js/angular'
        ,'angular-ui': 'lib/ionic/js/angular-ui'
    },
    shim: {
        'angular': {exports: 'angular'},
        'www/js/controllers': { deps: ['angular']}
    },
    deps: tests,
    callback: window.__karma__.start
})

helloSpec.js

define('helloTests', ['angular'], function() {
        describe('UnitTest: Hello', function() {
            it('is defined', function() {
                expect([1,2,3].length).toEqual(3);
            });
        });
    });

结果如下 debug.html:

<!doctype html>
<html>
<head>
  <title>Karma DEBUG RUNNER</title>
  <link href="favicon.ico" rel="icon" type="image/x-icon" />
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
  <script type="text/javascript">
    window.__karma__ = {
      info: function(info) {
        if (info.dump && window.console) window.console.log(info.dump);
      },
      complete: function() {
        if (window.console) window.console.log('Skipped ' + this.skipped + ' tests');
      },
      store: function() {},
      skipped: 0,
      result: window.console ? function(result) {
        if (result.skipped) {
          this.skipped++;
          return;
        }
        var msg = result.success ? 'SUCCESS ' : 'FAILED ';
        window.console.log(msg + result.suite.join(' ') + ' ' + result.description);

        for (var i = 0; i < result.log.length; i++) {
          window.console.error(result.log[i]);
        }
      } : function() {},
      loaded: function() {
        this.start();
      }
    };

    window.__karma__.config = {"args":[],"useIframe":true,"captureConsole":true};


    // All served files with the latest timestamps
    window.__karma__.files = {
  '/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/requirejs/require.js': 'f9d7ac1ba78c53a51fd346e3901a5f406f060f44',
  '/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-requirejs/lib/adapter.js': 'f2d8d5976c2bbe89ebe046ac51f393f5547bbc3b',
  '/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/jasmine-core/lib/jasmine-core/jasmine.js': '578a1e5ff14db21b04e2d6db7fd0eda37042440c',
  '/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-jasmine/lib/boot.js': '997181251903c5bcc9659d92edc872a2a4abfa7b',
  '/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js': '0a69ad1fa10cc8be0a12b241227d3b834f82db4f',
  '/base/www/lib/ionic/js/ionic-angular.min.js': '44040e6eeb17077cc79b0985118b15bf1420217d',
  '/base/www/lib/ionic/js/ionic.bundle.min.js': '4a4bfece5434ae5c4cebab055aaae274f82050d9',
  '/base/www/lib/ionic/js/ionic.min.js': '3e42a6e6700ba9982583b6b5da48861d202d8cfe',
  '/base/www/lib/ionic/js/angular/angular-animate.min.js': '6df7f913a45a779638fd60cce18eee17268460d2',
  '/base/www/lib/ionic/js/angular/angular-resource.min.js': 'eca9d7e3b12d62b9da13216f1d3b39fcd411f860',
  '/base/www/lib/ionic/js/angular/angular-sanitize.min.js': 'b8cf1b04b40df803c16edda80c431ec422b63ad8',
  '/base/www/lib/ionic/js/angular/angular.min.js': '09028d3553206017f5ced7249be5641baaea2020',
  '/base/www/lib/ionic/js/angular-ui/angular-ui-router.min.js': '0973f9c46d3be867276e941e2e6af9c662a92333',
  '/base/www/js/controllers/game.js': '4b67bf783d76ae9f67ac5d569e78319be4a85321',
  '/base/www/js/controllers/setup.js': 'b466bd52a326d35700517383a6ceb5b25b3ab44c',
  '/base/www/js/directives.js': 'fbc832e4aa0c55a597a57cd4474aa9435bc17bf1',
  '/base/www/js/services.js': 'df5339fa9aa37d92974fe59c392b8fb53a71497d',
  '/base/test/specs/helloSpec.js': 'b8f2b498922be08d7ff36bafd094132b9c75d357',
  '/base/test/test-app.js': '432128c0bcd0257679fb9f4f31ff75a1060f64db'
};

  </script>
  <!-- Dynamically replaced with <script> tags -->
  <script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/requirejs/require.js"></script>
<script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-requirejs/lib/adapter.js"></script>
<script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-jasmine/lib/boot.js"></script>
<script type="text/javascript" src="/absolutec:/Users/Mark/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js"></script>
<script type="text/javascript" src="/base/test/test-app.js"></script>
  <script type="text/javascript">
    window.__karma__.loaded();
  </script>
</body>
</html>

我假设服务文档列表是 requirejs 提取的所有内容,脚本标签是 karma 添加的内容。

控制台输出为:

Skipped 0 tests

所以看起来它只是没有看到测试定义。但是当我逐步调试并在 requirejs.config 处设置断点时,我可以看到 "tests" 数组包含我的测试文件:

/base/test/specs/helloSpec.js

我还可以通过查看 Chrome 的网络面板看到它已加载。其中列出:

debug.html
boot.js
jasmine.js
adapter.js
require.js
test-app.js
adapter.js
helloSpec.js

早些时候我遇到了错误:

Mismatched anonymous define() module: ...

所以我更改了

的定义函数调用
define(['angular'], function(angular) {
    ....
});

define('helloTests', ['angular'], function(angular) {
    ....
});

删除了错误详细信息,但没有解决任何问题。事实证明它也无济于事,因为我再也看不到错误消息的变化。发布后,我一时兴起尝试删除它,因为我看不到它在其他任何地方使用。然后报错信息变成:

Chrome 43.0.2357 (Windows 8.1 0.0.0) ERROR: 'There is no timestamp for www/lib/ionic/js/angular.js!'

WARN [web-server]: 404: /www/lib/ionic/js/angular.js
Chrome 43.0.2357 (Windows 8.1 0.0.0) ERROR
  Uncaught Error: Script error for: angular
  http://requirejs.org/docs/errors.html#scripterror
  at c:/Users/Mark/AppData/Roaming/npm/node_modules/requirejs/require.js:141

因为我使用的是 min 文件(通过在文件模式中包含“*.min.js”更容易过滤掉调试版本)我更改了我的 requirejs.config 路径并附加了“.min “像这样:

requirejs.config({
    baseUrl: 'www',
    paths: {
        'angular': 'lib/ionic/js/angular/angular.min'
        ,'angular-ui': 'lib/ionic/js/angular-ui/angular-ui.min'
    },
    ...
})

此时,错误消息仅更改为说 angular.min.js 没有时间戳。在这一点上,我注意到路径是相对的,并以 "base/wwww" 开头。那是我终于注意到我错过的东西 in the docs 的时候。所以我将 requirejs.config 中的 "baseUrl" 参数更改为添加 "base/":

requirejs.config({
    baseUrl: 'base/www',
    ...
})

现在错误消失了,测试是 运行:

Chrome 43.0.2357 (Windows 8.1 0.0.0): Executed 1 of 1 SUCCESS (0.015 secs / 0 secs)

编辑: 还有一件事,测试开始了 运行 但我没有注意到我仍然收到 "There is no timestamp" 消息。将我的 requirejs.config baseUrl 参数从相对更改为绝对(在开头添加“/”)修复了它。

requirejs.config({
    baseUrl: '/base/www',
    ...
})

现在我起来 运行。