0

So I was just trying to use chai-as-promised with karma and webpack and this is what I get. enter image description here I have chai-as-promised, karma-chai-as-promised, sinon-chai, karma-sinon-chai, karma-webpack installed so i should be good to go yet it doesn't work here is my karma.config file

var webpackConfig = require('../../build/webpack.test.conf')

module.exports = function (config) {
  config.set({
    browsers: ['ChromeHeadless'],
    frameworks: ['mocha', 'chai-things', 'sinon-chai', 'chai-as-promised'],
    reporters: ['spec', 'coverage'],
    files: [
      '../../node_modules/babel-polyfill/dist/polyfill.js',
      './index.js'],
    preprocessors: {
      './index.js': ['webpack', 'sourcemap']
    },
    webpack: webpackConfig,
    coverageReporter: {
      dir: './coverage',
      reporters: [
        { type: 'lcov', subdir: '.' },
        { type: 'text-summary' }
      ]
    },
    client: {
      captureConsole: true
    }
  })
}
Amgad Serry
  • 1,595
  • 2
  • 12
  • 20
  • I had a similar problem with es6 import/export and the solution I found was to use [karma-browserify](https://github.com/nikku/karma-browserify) and it works well with my setup. – T4rk1n Jul 14 '17 at 21:16

2 Answers2

0

karma-chai-as-promised includes Chai-as-promised in your test by plopping it into the window as a script element. (See here.) However, this has ceased to be doable since Chai-as-promised 6.0. This release distributes code that depends on the availability of machinery that resolves CommonJS require calls. It could be SystemJS or Webpack, or what-have-you but there has to be something resolving those calls. So it is not possible to just plop the distributed code into a web page.

Possibilities:

  1. Coerce karma-chai-as-promised to use an older version of Chai-as-promised. chai-as-promised is a peer dependency of karma-chai-as-promised so you could have your project set a version older than 6.

  2. Drop karma-chai-as-promised, and have Webpack include it in your test bundle.

Louis
  • 146,715
  • 28
  • 274
  • 320
0

I have added this as a helper file

import chai from 'chai'
chai.should()
import chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)

and then you can use it as

import './chaiAsPromised.helper'
Amgad Serry
  • 1,595
  • 2
  • 12
  • 20