Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Grunt、Jasmine、Phantom、React单元测试:React在ReactElementValidator上抛出_Javascript_Gruntjs_Jasmine_Phantomjs_Reactjs - Fatal编程技术网

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