2016-04-01 66 views
3

我試圖學習測試一些JavaScript文件。在webpack中使用mocha和chai。單元測試Javascript - 對象不是構造函數

這是我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 
    } 
    }); 
}; 

這是我的人緣配置使用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 start test/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) 
+0

不應該'global.should = chai.should()'是'global.should = chai.should'? – henry700

+0

@ henry700不應該是正確的 – tyler

+0

錯誤消息指出您的webpack配置有錯誤。你也可以發佈嗎? – Charminbear

回答

1

我有完全相同的問題,而google搜索解決方案跨越此帖一。不幸的是,沒有解決方案,所以花了幾個小時嘗試不同的建議後,我發現正確的配置。

這裏我就是這樣做:

  1. 安裝phantomjs-polyfill-object-assign模塊
 
    npm install --save-dev phantomjs-polyfill-object-assign 
  • 下因緣的files屬性上述填充工具配置:
  •  
        ... 
        files: [ 
         '../node_modules/phantomjs-polyfill-object-assign/object-assign-polyfill.js', 
         'test/variables.js', 
         'test/**/*.spec.js', 
        ], 
        ... 
    

    注意:根據您擁有的karma配置,上面的填充路徑可能需要調整。

  • 更新babel-loader配置與exclude屬性:
  •  
        ... 
        loaders: [{ 
         test: /\.js$/, 
         loader: 'babel-loader', 
         exclude: /node_modules/, 
        }, 
        ... 
    

    就是這樣,測試可以被再次運行。

    2

    而不是

    expect(true).to.be(true); 
    

    使用

    expect(true).to.equal(true); 
    

    這爲我工作

    describe('App', function(){ 
    it('should set the color to red', function(){ 
        // expect(true).to.be(true); 
        expect(true).to.equal(true); 
        }); 
    }); 
    
    +1

    請添加一些解釋,答案會更有幫助。 – BartoszKP

    相關問題