Chai变量未被Karma / Grunt加载

时间:2015-08-05 07:12:32

标签: node.js gruntjs karma-runner chai

我不能让chai在业力/咕噜声中设置chai-as-promised。

运行grunt testkarma test/karma.conf.js时出现以下错误:

PhantomJS 1.9.8 (Mac OS X 0.0.0) ERROR
  ReferenceError: Can't find variable: chai
  at /Users/isaac/Sites/ChessGame/node_modules/chai-as-promised/lib/chai-as-promised.js:19

这看起来像this one的类似问题,但我遇到的问题是chai-as-promise,而不是sinon-chai。我已经确认node_modules/chai/chai.js存在。

这是我的package.json文件:

{
  "name": "chessgame",
  "private": true,
  "devDependencies": {
    "angular-mocks": "^1.4.3",
    "chai": "^3.2.0",
    "chai-as-promised": "^5.1.0",
    "chai-things": "^0.2.0",
    "grunt": "^0.4.5",
    "grunt-angular-templates": "^0.5.7",
    "grunt-autoprefixer": "^2.0.0",
    "grunt-concurrent": "^1.0.0",
    "grunt-contrib-clean": "^0.6.0",
    "grunt-contrib-compass": "^1.0.0",
    "grunt-contrib-concat": "^0.5.0",
    "grunt-contrib-connect": "^0.9.0",
    "grunt-contrib-copy": "^0.7.0",
    "grunt-contrib-cssmin": "^0.12.0",
    "grunt-contrib-htmlmin": "^0.4.0",
    "grunt-contrib-imagemin": "^0.9.2",
    "grunt-contrib-jshint": "^0.11.0",
    "grunt-contrib-uglify": "^0.7.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-filerev": "^2.1.2",
    "grunt-google-cdn": "^0.4.3",
    "grunt-karma": "*",
    "grunt-mocha": "^0.4.13",
    "grunt-newer": "^1.1.0",
    "grunt-ng-annotate": "^0.9.2",
    "grunt-svgmin": "^2.0.0",
    "grunt-usemin": "^3.0.0",
    "grunt-wiredep": "^2.0.0",
    "jit-grunt": "^0.9.1",
    "jshint-stylish": "^1.0.0",
    "karma-browserify": "^4.2.1",
    "karma-chai": "^0.1.0",
    "karma-chai-as-promised": "^0.1.2",
    "karma-chai-things": "^0.1.4",
    "karma-coverage": "^0.4.2",
    "karma-jasmine": "*",
    "karma-mocha": "^0.2.0",
    "karma-mocha-reporter": "^1.0.3",
    "karma-phantomjs-launcher": "*",
    "karma-requirejs": "^0.2.2",
    "karma-sinon": "^1.0.4",
    "karma-sinon-chai": "^1.0.0",
    "mocha": "^2.2.5",
    "mockfirebase": "^0.11.0",
    "requirejs": "^2.1.19",
    "sinon": "^1.15.4",
    "sinon-chai": "^2.8.0",
    "time-grunt": "^1.0.0"
  },
  "engines": {
    "node": ">=0.10.0"
  },
  "scripts": {
    "test": "grunt test"
  }
}

test/karma.conf.js文件:

module.exports = function(config) {
  'use strict';

  config.set({
    autoWatch: true,
    basePath: '../',
    frameworks: [
      "mocha",
      "chai",
      "sinon-chai",
      "chai-as-promised"
     ],
    files: [
      // bower:js
      'bower_components/jquery/dist/jquery.js',
      'bower_components/angular/angular.js',
      'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap.js',
      'bower_components/angular-animate/angular-animate.js',
      'bower_components/angular-cookies/angular-cookies.js',
      'bower_components/angular-messages/angular-messages.js',
      'bower_components/angular-resource/angular-resource.js',
      'bower_components/angular-route/angular-route.js',
      'bower_components/angular-sanitize/angular-sanitize.js',
      'bower_components/angular-touch/angular-touch.js',
      'bower_components/firebase/firebase.js',
      'bower_components/angularfire/dist/angularfire.js',
      'bower_components/angular-mocks/angular-mocks.js',
      'bower_components/mocha/mocha.js',
      'bower_components/chai/chai.js',
      'bower_components/mockfirebase/browser/mockfirebase.js',
      // endbower
      'node_modules/sinon/pkg/sinon.js',
      'node_modules/chai/chai.js',
      'node_modules/sinon-chai/lib/sinon-chai.js',
      "app/scripts/**/*.js",
      "test/mock/**/*.js",
      "test/spec/**/*.js"
    ],
    exclude: [
    ],
    port: 8080,
    browsers: [
      "PhantomJS"
    ],
    reporters: ['mocha','coverage'], //
    preprocessors: {
      'app/scripts/controllers/*.js': ['coverage'],
      'app/scripts/directives/*.js': ['coverage'],
      'app/scripts/app.js': ['coverage']
    },
    coverageReporter: {
      type : 'text',
      dir: 'test-results/coverage/'
    },
    singleRun: false,
    colors: true,
    logLevel: config.LOG_INFO,
  });
};

1 个答案:

答案 0 :(得分:6)

不幸的是,这是卡玛的一个怪癖。 frameworks键必须采用相反的顺序,因此您必须在加载chai之前加载每个chai插件。现在你有

 frameworks: [
  "mocha",
  "chai",
  "sinon-chai",
  "chai-as-promised"
 ],

如果您将其更改为

 frameworks: [
  "mocha",
  "sinon-chai",
  "chai-as-promised",
  "chai"
 ],

然后一切都应该运作良好!一些karma-chai plugins document this,虽然它肯定会更加突出