Karma/Grunt 未加载 Chai 变量

Chai variable not loaded by Karma/Grunt

我无法让 chai 在 karma/grunt 设置中与 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

这看起来像是与 类似的问题,但我 运行 正在处理 chai-as-promised 的问题,而不是 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,
  });
};

不幸的是,这是 Karma 的一个怪癖。 frameworks 键必须以一种相反的顺序,所以你必须在加载 chai 本身之前加载每个 chai 插件。现在你有

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

如果您将其更改为

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

那么一切都应该运行良好!一些karma-chai plugins document this,虽然它肯定可以更突出