Joost Schuur
Joost Schuur

Reputation: 4482

How do I use a Chai plugin like chai-date-string with Karma/Mocha/Chai?

Should be able to use any Chai plugin with unit testing with Karma/Mocha and Chai, or does it need to be converted into a special karma compatible plugin?

I'd like to use chai-date-string to do something like expect(requestBody.time).to.be.a.dateString();, but didn't have any luck just installing them as an NPM module and requiring them in my test file.

Then I came across karma-chai-plugins, which I thought was designed to use other chai plugins (even beyond the few that it comes bundled with), but adding that as an NPM module, and then adding the Chai plugin name to the frameworks list, but this didn't work.

My karma.conf.js:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'chai', 'chai-date-string', 'sinon-chai', 'browserify'],
    client: { chai: { includeStack: true } },
    files: [ 'playmob.js', 'test/**/*_test.js' ],
    preprocessors: {
      'test/**/*.js': [ 'browserify' ]
    },
    browserify: {
      debug: true,
    },
    exclude: [ ],
    reporters: ['mocha', 'beep'],
    mochaReporter: { ignoreSkipped: true },
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeHeadless'],
    singleRun: false,
    concurrency: Infinity
  })
}

Versions in package.json (I had to explicitly install a newer version of chai-as-promised to get around a dependency problem):

  "devDependencies": {
    "browserify": "^14.4.0",
    "chai": "^4.1.2",
    "chai-as-promised": "^7.1.1",
    "chai-date-string": "^0.1.0",
    "karma": "^1.7.1",
    "karma-beep-reporter": "^0.1.4",
    "karma-browserify": "^5.1.1",
    "karma-chai-plugins": "^0.9.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "^1.0.1",
    "karma-mocha": "^1.3.0",
    "karma-mocha-reporter": "^2.2.4",
    "karma-sinon-chai": "^1.3.2",
    "mocha": "^3.5.3",
    "sinon": "^2.4.1",
    "sinon-chai": "^2.14.0",
    "uglifyjs": "^2.4.11",
    "url": "^0.11.0",
    "watchify": "^3.9.0"
  }

Results in the following error:

> [email protected] test /Users/jschuur/Code/Playmob/js_api_lib
> karma start

/Users/jschuur/Code/Playmob/js_api_lib/node_modules/di/lib/injector.js:9
      throw error('No provider for "' + name + '"!');
      ^

Error: No provider for "framework:chai-date-string"! (Resolving: framework:chai-date-string)
    at error (/Users/jschuur/Code/Playmob/js_api_lib/node_modules/di/lib/injector.js:22:12)

Upvotes: 1

Views: 774

Answers (1)

Louis
Louis

Reputation: 151380

When I look at the code of karma-chai-plugins I see a series of hardcoded plugin names. It seems to me that karma-chai-plugins recognizes only those Chai plugins that are hardcoded in its source. So I don't think you can use it for chai-date-string.

Moreover, chai-date-string is not distributed in a format that is readily loadable in a browser. You could use Browserify or Webpack to convert it to a file that exports something like chaiDateString into the global space. Then you'd have to:

  1. Include in files your converted chai-date-string script.

  2. Add another script in files that invokes chai.use(chaiDateString). The example code shown in the repo omits the call to chai.use, but it cannot be omitted.

Upvotes: 1

Related Questions