Babel / Karma / Chai gives TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions

浪尽此生 提交于 2019-12-01 16:15:36

I'm using babel and ES6 modules. ES6 code is implictly strict mode. Chai's assertion library is not compatible with strict mode as it's set up above.

The solution is to ignore / not include node_modules in the webpack babel loader:

This is the relevant section of my karma.conf.js:

webpack: {
  // any necessary webpack configuration
  devtool: 'inline-source-map',
  module: {
    loaders: [
      { test: /\.js$/, loader: 'babel', exclude: /node_modules/ }
    ]
  }
},

The exclude is a regex test instead of a simple string.

I struggled with this for about a day then realised you don't need to import/require chai. It's already available as is jasmine's describe.

Simply remove the line var expect = require('chai').expect; so you are left with the spec/test below.

describe('HelloComponent', function() {

  it('passes a quite simple test', function() {
    expect(1 + 4).to.equal(5);
  });

});

I had looked at so many different examples of Webpack 2 + Karma + Chai that I missed the fact I didn't need to import it

Installing chai properly solved the issue for me:

$ npm i -D chai
+-- chai@4.0.2
| +-- check-error@1.0.2
| +-- deep-eql@2.0.2
| | `-- type-detect@3.0.0
| +-- get-func-name@2.0.0
| +-- pathval@1.1.0
| `-- type-detect@4.0.3
`-- nock@9.0.13
  `-- chai@3.5.0
    +-- deep-eql@0.1.3
    | `-- type-detect@0.1.1
    `-- type-detect@1.0.0

In order to try chai out with httprequests, I installed only chai-http. And not chai itself. But were using both of them:

'use strict';

const chaiHttp = require('chai-http');
const chai = require('chai');
const expect = chai.expect;
chai.use(chaiHttp);

So I've tried to installed chai explicitly and apparently it solved the issue. I can use expect and the error isn't thrown any more.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!