Javascript Grunt、Jasmine、Phantom、React单元测试:React在ReactElementValidator上抛出
我正在使用Javascript Grunt、Jasmine、Phantom、React单元测试:React在ReactElementValidator上抛出,javascript,gruntjs,jasmine,phantomjs,reactjs,Javascript,Gruntjs,Jasmine,Phantomjs,Reactjs,我正在使用Grunt运行Jasmine单元测试和Phantom Grunfile.js module.exports = function (grunt) { "use strict"; grunt.loadNpmTasks('grunt-browserify'); grunt.loadNpmTasks('grunt-karma'); grunt.initConfig( { pkg: grunt.file.readJSO
Grunt
运行Jasmine
单元测试和Phantom
Grunfile.js
module.exports = function (grunt)
{
"use strict";
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-karma');
grunt.initConfig(
{
pkg: grunt.file.readJSON('package.json'),
browserify: {
dev: {
files: {
'test-output.js':['src/methods.js']
},
options: {
browserifyOptions: {
debug: true
}
}
}
},
karma:
{
unit:{
configFile:"karma.conf.js"
}
}
});
};
使用此Karma
config文件
module.exports = function(config)
{
config.set({
basePath: '',
frameworks: ['browserify', 'jasmine'],
files: [
'myDir/*.js'
],
exclude: [
],
preprocessors: {
'myDir/*.js':['browserify','reactify']
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['PhantomJS'],
browserify: {
debug: true,
transform: []
},
plugins: [
'karma-phantomjs-launcher',
'karma-jasmine','karma-bro'],
singleRun: true
});
};
React作为包本地安装在node\u modules
文件夹中。我可以grunt-browserify
并按预期将所有内容捆绑到test-output.js
中,但当我执行grunt-karma
时,我会得到错误:
TypeError: 'undefined' is not a function (evaluating 'ReactElementValidator.createElement.bind
如果我检查
test output.js
文件,我可以看到ReactElementValidator.createElement.bind
函数在包中。你知道这是什么原因吗?这是phantomJS<2.0的已知问题。要解决此问题,只需安装phantomjs polyfill,如下所示:
npm install --save-dev phantomjs-polyfill
然后像这样将其添加到配置中
files: [
'node_modules/phantomjs-polyfill/bind-polyfill.js',
'myDir/*.js'
]
我希望这能有所帮助。我认为在PhantomJS中使用React测试实用程序有一个不足之处。只是想找个推荐人。相当确定这是真正的问题:。在PhantomJS 2.x中看起来是固定的,但我还没有尝试过它。Hanks,我会研究它。应该标记为答案-已确认解决问题。对于陷入困境的人,请注意将其添加到“文件”数组中的顺序。它应该是第一个。在那上面花相当多的时间。0_0