javascript - 单元测试 Javascript - 对象不是构造函数

标签 javascript unit-testing mocha.js karma-runner chai

我正在尝试学习测试一些 javascript 文件。将 mocha 和 chai 与 webpack 结合使用。

这是我的test/karma.conf.js

var webpack = require('webpack');

module.exports = function (config) {
  config.set({
    browsers: [ 'PhantomJS' ],
    singleRun: true,
    frameworks: [ 'mocha' ],
    files: [
      'tests.webpack.js'
    ],
    preprocessors: {
      'tests.webpack.js': [ 'webpack', 'sourcemap' ]
    },
    reporters: [ 'spec' ],
    webpack: {
      devtool: 'inline-source-map',
      module: {
        loaders: [
          { test: /\.js$/, loader: 'babel-loader' }
        ]
      }
    },
    webpackServer: {
      noInfo: true
    }
  });
};

这是我在 karma 配置上使用的 test/test.webpack.js:

import chai from 'chai'

global.chai = chai
global.expect = chai.expect
global.should = chai.should()

var context = require.context('../src', true, /spec\.js$/);
context.keys().forEach(context);

我的测试非常基础,只是为了测试它是否有效 src/index.spec.js:

describe("A test suite", function() {
   beforeEach(function() { });
   afterEach(function() { });
   it('should fail', function() { expect(true).to.be.true; });
});

当我尝试运行测试时,出现此错误

./node_modules/karma/bin/karma 开始测试/karma.conf.js

01 04 2016 14:15:08.191:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
01 04 2016 14:15:08.255:INFO [launcher]: Starting browser PhantomJS
01 04 2016 14:15:12.443:INFO [PhantomJS 2.1.1 (Windows 8 0.0.0)]: Connected on socket /#GivXNjnm0g5H9DBXAAAA with id 29314805

PhantomJS 2.1.1 (Windows 8 0.0.0) ERROR
  TypeError: Object is not a constructor (evaluating '$export($export.S + $export.F * !__webpack_require__(17), 'Object', { defineProperty: __webpack_require__(13).f })')
  at C:/Users/pablo.feldman/Documents/Projects/jpmc-components/test/tests.webpack.js:535 <- webpack:///~/babel-runtime/~/core-js/library/modules/es6.object.define-property.js:3:0

PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.101 secs / 0 secs)

最佳答案

我遇到了完全相同的问题,在谷歌搜索解决方案时遇到了这篇文章。不幸的是没有解决方案,所以在花了几个小时尝试不同的建议后我找到了正确的配置。

这是我做的:

  1. 已安装 phantomjs-polyfill-object-assign 模块
    npm install --save-dev phantomjs-polyfill-object-assign
  1. 在 karma 的 files 属性下配置上述 polyfill:
    ...
    files: [
        '../node_modules/phantomjs-polyfill-object-assign/object-assign-polyfill.js',
        'test/variables.js',
        'test/**/*.spec.js',
    ],
    ...

注意:根据您的 karma 配置,上面的 polyfill 路径可能需要调整。

  1. 使用 exclude 属性更新了 babel-loader 配置:
    ...
    loaders: [{
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
    },
    ...

就是这样,可以再次运行测试。

关于javascript - 单元测试 Javascript - 对象不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36362606/

相关文章:

javascript - 如果我使用 Javascript 和 CSS 为数据库动态获取文本,如何将文本分成几列?

unit-testing - 我如何获得常规 MockFor/StubFor 忽略方法以使用需求方法

C++测试框架: recommendation sought

node.js - sails.js + mocha + supertest + sinon : how to stub sails. js Controller 函数

javascript - 如何在 JavaScript 中组合不同的条件?

javascript - 从标签返回文本,其中最外层 HTML 标签适用于 jquery 中的所有文本节点

c++ - 有没有办法显式分离隐式链接的 DLL?

node.js - Mocha 和超测有什么区别?

reactjs - 模拟 onClick 在 enzyme 中不起作用

javascript - 按钮传递 Onclick 事件