Grunt,Jasmine,Phantom,React Unit Testing:React抛出ReactElementValidator

woo*_*gie 3 javascript jasmine phantomjs gruntjs 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.readJSON('package.json'),
            browserify: {
                dev: {
                    files: {
                        'test-output.js':['src/methods.js']
                    },
                    options: {
                        browserifyOptions: {
                            debug: true
                        }
                    }
                }
            },
            karma:
            {
                unit:{
                    configFile:"karma.conf.js"
                }
            }
        });
};
Run Code Online (Sandbox Code Playgroud)

使用此Karma配置文件

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
    });
};
Run Code Online (Sandbox Code Playgroud)

React在本地安装为node_modules文件夹中的包.我可以grunt browserify和所有内容test-ouput.js按预期捆绑在一起,但是当我这样做时,grunt karma我得到错误:

TypeError: 'undefined' is not a function (evaluating 'ReactElementValidator.createElement.bind
Run Code Online (Sandbox Code Playgroud)

如果我检查test-ouput.js文件,我可以看到该ReactElementValidator.createElement.bind函数在bundle内.可能导致这种情况的任何想法?

Kev*_*vba 6

这是phantomJS <2.0的已知问题.要解决此问题,只需安装phantomjs polyfill,如下所示:

npm install --save-dev phantomjs-polyfill
Run Code Online (Sandbox Code Playgroud)

并将其添加到配置中.

files: [
    'node_modules/phantomjs-polyfill/bind-polyfill.js',
    'myDir/*.js'
]
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.