Chai变量未被Karma/Grunt加载

Isa*_*aac 1 node.js gruntjs chai karma-runner

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

我收到以下错误,当我运行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
Run Code Online (Sandbox Code Playgroud)

这对于这个问题看起来像是一个类似的问题,但是我遇到的问题是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"
  }
}
Run Code Online (Sandbox Code Playgroud)

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,
  });
};
Run Code Online (Sandbox Code Playgroud)

Kei*_*mus 6

不幸的是,这是卡玛的怪癖.该frameworks键必须是实物反向的-一个顺序,所以你要装载的装载柴本身之前每柴插件.现在你有

 frameworks: [
  "mocha",
  "chai",
  "sinon-chai",
  "chai-as-promised"
 ],
Run Code Online (Sandbox Code Playgroud)

如果你改变它

 frameworks: [
  "mocha",
  "sinon-chai",
  "chai-as-promised",
  "chai"
 ],
Run Code Online (Sandbox Code Playgroud)

那么一切都应该运作良好!一些karma-chai插件记录了这一点,尽管它肯定会更加突出