I'm trying to learn to test some javascript files. Using mocha and chai with webpack.
This is my 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
}
});
};
And this the test/test.webpack.js
which I use on the karma config:
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);
my test is very basic, just in order to test if it works src/index.spec.js
:
describe("A test suite", function() {
beforeEach(function() { });
afterEach(function() { });
it('should fail', function() { expect(true).to.be.true; });
});
When I try to run the tests, I got this error
./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-ponents/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)
I'm trying to learn to test some javascript files. Using mocha and chai with webpack.
This is my 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
}
});
};
And this the test/test.webpack.js
which I use on the karma config:
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);
my test is very basic, just in order to test if it works src/index.spec.js
:
describe("A test suite", function() {
beforeEach(function() { });
afterEach(function() { });
it('should fail', function() { expect(true).to.be.true; });
});
When I try to run the tests, I got this error
./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-ponents/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)
Share
Improve this question
asked Apr 1, 2016 at 17:42
PabloPablo
10.7k18 gold badges59 silver badges80 bronze badges
3
-
Shouldn't
global.should = chai.should()
beglobal.should = chai.should
? – henry700 Commented Apr 1, 2016 at 17:45 - @henry700 no that should be correct as is – Tyler Commented Apr 1, 2016 at 17:50
- The error message states that your webpack-configuration has an error. Can you also post that? – David Losert Commented Apr 2, 2016 at 17:59
2 Answers
Reset to default 3I had exactly same issue and while googling for solution came across this post. Unfortunately there was no solution, so after spending several hours trying different suggestions I found right config.
Here's what I did:
- Installed
phantomjs-polyfill-object-assign
module
npm install --save-dev phantomjs-polyfill-object-assign
- Configured above polyfill under
files
property of karma:
... files: [ '../node_modules/phantomjs-polyfill-object-assign/object-assign-polyfill.js', 'test/variables.js', 'test/**/*.spec.js', ], ...
Note: depending on karma config you have, it's possible that above path to polyfill needs adjustment.
- Updated
babel-loader
config withexclude
property:
... loaders: [{ test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/, }, ...
That's it, tests can be run again.
Instead of
expect(true).to.be(true);
use
expect(true).to.equal(true);
This worked for me
describe('App', function(){
it('should set the color to red', function(){
// expect(true).to.be(true);
expect(true).to.equal(true);
});
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745260358a4619183.html
评论列表(0条)