草庐IT

javascript - Babel/Karma/Chai 给出 TypeError : 'caller' , 'callee' 和 'arguments' properties may not be accessed on strict mode functions

coder 2024-07-19 原文

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

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 加载器中忽略/不包含 node_modules:

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

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

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

关于javascript - Babel/Karma/Chai 给出 TypeError : 'caller' , 'callee' 和 'arguments' properties may not be accessed on strict mode functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29105947/

有关javascript - Babel/Karma/Chai 给出 TypeError : 'caller' , 'callee' 和 'arguments' properties may not be accessed on strict mode functions的更多相关文章

随机推荐