在Cordova项目上运行Karma时执行0 0 ERROR

时间:2015-06-05 12:33:01

标签: cordova requirejs karma-runner

我有一个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

1 个答案:

答案 0 :(得分:24)

早些时候我收到了错误:

Mismatched anonymous define() module: ...

所以我改变了

的define函数调用
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)

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

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

现在我已经开始运作了。