【问题标题】:Karma+Jasmine+RequireJS not running specsKarma+Jasmine+RequireJS 未运行规范
【发布时间】:2014-12-02 21:30:55
【问题描述】:

我一整天都在努力让 Karma 工作。出于某种原因,我无法让 Karma 实际运行测试。相反,它似乎只是无限期地闲置,没有任何成功或失败的迹象。

我注意到,当我在 Chrome 中执行 Karma 时单击“调试”按钮时,打开的 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
  });
};

test-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
});

【问题讨论】:

    标签: javascript requirejs jasmine karma-runner


    【解决方案1】:

    原来我犯了一个小错误:我忘记在文件下包含test-main.js。在尝试修复与“缺少时间戳”相关的另一个错误时,我无意中删除了对 test-main 的引用。

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

    希望这将在未来对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 2014-09-08
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多