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

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

我无法弄清楚为什么这个测试没有通过。

var expect = require('chai').expect;

describe('HelloComponent', function() {

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

});

产生此错误:

DEBUG [web-server]: serving: /Users/ivan/dev/react-starter/node_modules/karma/static/context.html
DEBUG [web-server]: serving (cached): /Users/ivan/dev/react-starter/node_modules/mocha/mocha.js
DEBUG [web-server]: serving (cached): /Users/ivan/dev/react-starter/node_modules/karma-mocha/lib/adapter.js
DEBUG [web-server]: serving (cached): /Users/ivan/dev/react-starter/test/front-end/tests.webpack.js
Chrome 41.0.2272 (Mac OS X 10.10.2) HelloComponent passes a quite simple test FAILED
        TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
            at new Assertion (/Users/ivan/dev/react-starter/test/front-end/tests.webpack.js:2166:43 <- webpack:///~/chai/lib/chai/assertion.js:33:42)
            at chai.expect (/Users/ivan/dev/react-starter/test/front-end/tests.webpack.js:3592:13 <- webpack:///~/chai/lib/chai/interface/expect.js:9:11)
            at Context.<anonymous> (/Users/ivan/dev/react-starter/test/front-end/tests.webpack.js:89:6 <- webpack:///test/front-end/hello-spec.js:10:4)

可能跟babel在严格模式下包装东西有关?

有谁知道我可以开始采取什么步骤来弄清楚这里发生了什么?

代码是开源的,可在此处获取: https://github.com/UWFosterIT/react-starter/tree/gulp-webpack

安装并重现此错误:

git clone https://github.com/UWFosterIT/react-starter.git
npm install
gulp test:karma

我正在使用 babel 和 ES6 模块。 ES6 代码是隐含的严格模式。 Chai 的断言库与上面设置的严格模式不兼容。

解决方法是在webpack babel loader中忽略/不包含node_modules:

这是我的 karma.conf.js 的相关部分:

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

exclude 是一个正则表达式测试,而不是一个简单的字符串。

我为此苦苦挣扎了大约一天,然后意识到您不需要 import/require chai。它和 jasmine 的 describe.

一样已经可用

只需删除行 var expect = require('chai').expect;,这样就剩下下面的 spec/test。

describe('HelloComponent', function() {

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

});

我看了很多 Webpack 2 + Karma + Chai 的不同示例,以至于我忽略了我不需要导入它的事实

安装 chai 正确地解决了我的问题:

$ 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

为了尝试 chai http 请求,我只安装了 chai-http。而不是 chai 本身。但是同时使用了它们:

'use strict';

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

所以我尝试显式安装 chai,显然它解决了问题。我可以使用 expect 并且不再抛出错误。