1

I can't get chai working with chai-as-promised in a karma/grunt set up.

I get the following error when I run grunt test or karma 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 looks like a similar problem to this one, but I'm running into the issue with chai-as-promised, not sinon-chai. I've verified that node_modules/chai/chai.js exists.

Here is my package.json file:

{
  "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"
  }
}

And the test/karma.conf.js file:

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,
  });
};
Community
  • 1
  • 1
Isaac
  • 2,173
  • 1
  • 13
  • 15

1 Answers1

6

This is a quirk of Karma, unfortunately. The frameworks key has to be in a kind-of-reverse order, so you have to load every chai plugin before loading chai itself. Right now you have

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

If you change that to

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

Then everything should work well! Some of the karma-chai plugins document this, although it could certainly be more prominent

Keithamus
  • 1,819
  • 17
  • 21
  • Thanks for the response. That's helpful information. Once I get a chance to check this I'll mark your response as the answer. – Isaac Sep 26 '15 at 00:49
  • It definitely could be more prominent. Thanks for that @Keithamus – Zen Oct 21 '15 at 12:48