Karma + Jasmine + RequireJS没有运行规格

时间:2014-12-02 21:30:56

标签: javascript requirejs jasmine karma-runner

我一直试图让Karma整天工作。出于某种原因,我无法让Karma实际运行测试。相反,它似乎无限期地闲置,没有表明成功或失败。

我注意到当我在Chrome中执行Karma时单击Debug按钮时,打开的Debug Runner不会呈现任何内容。我还注意到调试确实加载了项目库,但是非调试模式只加载了karma.js和socketio。

以下是运行karma start后的调试日志:

DEBUG [plugin]: Loading karma-* from /usr/local/lib/node_modules
DEBUG [plugin]: Loading plugin /usr/local/lib/node_modules/karma-chrome-launcher.
DEBUG [plugin]: Loading plugin /usr/local/lib/node_modules/karma-jasmine.
DEBUG [plugin]: Loading plugin /usr/local/lib/node_modules/karma-requirejs.
INFO [karma]: Karma v0.12.28 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
DEBUG [temp-dir]: Creating temp dir at /var/folders/1y/g2l4xc656ds_0_grl0pz3fnm0000gn/T/karma-14051726
DEBUG [launcher]: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --user-data-dir=/var/folders/1y/g2l4xc656ds_0_grl0pz3fnm0000gn/T/karma-14051726 --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate http://localhost:9876/?id=14051726
DEBUG [watcher]: Resolved files:
/usr/local/lib/node_modules/requirejs/require.js
/usr/local/lib/node_modules/karma-requirejs/lib/adapter.js
/usr/local/lib/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
/usr/local/lib/node_modules/karma-jasmine/lib/boot.js
/usr/local/lib/node_modules/karma-jasmine/lib/adapter.js
/Users/christopherdrane/chrome/js/lib/jquery/dist/jquery.js
/Users/christopherdrane/chrome/js/lib/dynatable/jquery.dynatable.js
/Users/csd/chrome/js/app/main.js
/Users/csd/chrome/js/test/appSpec.js
DEBUG [watcher]: Watching "/Users/csd/chrome/js/lib/jquery/dist/jquery.js"
DEBUG [watcher]: Watching "/Users/csd/chrome/js/lib/dynatable/jquery.dynatable.js"
DEBUG [watcher]: Watching "/Users/csd/chrome/js/app"
DEBUG [watcher]: Watching "/Users/csd/chrome/js/test"
DEBUG [watcher]: Add file "/Users/csd/chrome/js/lib/dynatable/jquery.dynatable.js" ignored. Already in the list.
DEBUG [watcher]: Add file "/Users/csd/chrome/js/app/main.js" ignored. Already in the list.
DEBUG [watcher]: Add file "/Users/csd/chrome/js/test/appSpec.js" ignored. Already in the list.
DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/client.html
DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/karma.js
DEBUG [web-server]: upgrade /socket.io/1/websocket/PuC65cs3e7EVlDAU1p00
DEBUG [karma]: A browser has connected on socket PuC65cs3e7EVlDAU1p00
INFO [Chrome 39.0.2171 (Mac OS X 10.8.5)]: Connected on socket PuC65cs3e7EVlDAU1p00 with id 14051726
DEBUG [launcher]: Chrome (id 14051726) captured in 4.614 secs
DEBUG [karma]: All browsers are ready, executing
DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/context.html
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/requirejs/require.js
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-requirejs/lib/adapter.js
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-jasmine/lib/boot.js
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-jasmine/lib/adapter.js
DEBUG [Chrome 39.0.2171 (Mac OS X 10.8.5)]: Disconnected during run, waiting 2000ms for reconnecting.
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma/static/client.html
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma/static/karma.js
DEBUG [web-server]: upgrade /socket.io/1/websocket/BSX21Oc05-qDhfgn1p01
DEBUG [karma]: A browser has connected on socket BSX21Oc05-qDhfgn1p01
DEBUG [Chrome 39.0.2171 (Mac OS X 10.8.5)]: Reconnected on BSX21Oc05-qDhfgn1p01.
DEBUG [web-server]: serving: /usr/local/lib/node_modules/karma/static/debug.html
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/requirejs/require.js
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-requirejs/lib/adapter.js
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-jasmine/lib/adapter.js
DEBUG [web-server]: serving (cached): /usr/local/lib/node_modules/karma-jasmine/lib/boot.js
WARN [Chrome 39.0.2171 (Mac OS X 10.8.5)]: Disconnected (1 times), because no message in 10000 ms.

项目结构为

/js
-> app.js (require.config)
-> karma.conf.js
-> /app
---> main.js
-> /lib
---> /dynatable
---> /jasmine
---> /jquery
---> /requirejs
-> /test
-> appSpec.js
-> test-main.js

也相关:

karma.conf.js

// Karma configuration
// Generated on Tue Dec 02 2014 11:07:40 GMT-0500 (EST)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'requirejs'],


    // list of files / patterns to load in the browser
    files: [
      {pattern: 'lib/jquery/dist/jquery.js', included: false},
      {pattern: 'lib/dynatable/jquery.dynatable.js', included: false},
      {pattern: 'app/**/*.js', included: false},
      {pattern: 'test/**/*Spec.js', included: false},
    ],


    // list of files to exclude
    exclude: ['app.js'
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    //logLevel: config.LOG_INFO,
      logLevel: config.LOG_DEBUG,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};

测试main.js

var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;

var pathToModule = function(path) {
  return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

Object.keys(window.__karma__.files).forEach(function(file) {
  if (TEST_REGEXP.test(file)) {
    // Normalize paths to RequireJS module names.
    allTestFiles.push(pathToModule(file));
  }
});

require.config({
  // Karma serves files under /base, which is the basePath from your config file
  baseUrl: '/base',

  paths: {
    jquery: 'lib/jquery/dist/jquery',
    dynatable: 'lib/dynatable/jquery.dynatable'},

  shim: {
    dynatable: {
      deps: ['jquery']
      }
    },

  // dynamically load all test files
  deps: allTestFiles,

  // we have to kickoff jasmine, as it is asynchronous
  callback: window.__karma__.start
});

1 个答案:

答案 0 :(得分:0)

事实证明我犯了一个小错误:我忘了将test-main.js包含在文件中。我无意中删除了对test-main的引用,同时尝试修复与"缺少时间戳相关的另一个错误"。

我的/ lib /目录包含使用git clone <jquery>获得的全部内容,最初我写了files以包含{pattern: 'lib/**/*.js', included: false}。 Karma不喜欢这一点,但不幸的是,由于错误信息不佳,我花了一段时间来诊断问题。

希望将来可以帮助其他人。