【问题标题】:Grunt, Jasmine, Phantom, React Unit Testing: React throws on ReactElementValidatorGrunt、Jasmine、Phantom、React 单元测试:React 抛出 ReactElementValidator
【发布时间】:2015-06-26 21:08:56
【问题描述】:

我正在使用Grunt 运行JasminePhantom 的单元测试。

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 配置文件

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_modules 文件夹中。我可以 grunt browserify 并且所有内容都按预期捆绑到 test-ouput.js 中,但是当我这样做 grunt karma 时出现错误:

TypeError: 'undefined' is not a function (evaluating 'ReactElementValidator.createElement.bind

如果我检查 test-ouput.js 文件,我可以看到 ReactElementValidator.createElement.bind 函数在包内。有什么想法可能导致这种情况吗?

【问题讨论】:

  • 我认为在 PhantomJS 中使用 React Test Utilities 存在 inown 问题。只是寻找参考。可以肯定这是真正的问题:github.com/ariya/phantomjs/issues/10522.
  • 看起来在 PhantomJS 2.x 中已修复,但我没有尝试过
  • 谢谢,我会调查的。

标签: javascript gruntjs jasmine phantomjs reactjs


【解决方案1】:

这是 phantomJS

npm install --save-dev phantomjs-polyfill

并像这样将其添加到配置中。

files: [
    'node_modules/phantomjs-polyfill/bind-polyfill.js',
    'myDir/*.js'
]

我希望这会有所帮助。

【讨论】:

  • 应该标记为答案 - 确认解决问题
  • 对于卡住的人,请注意添加到“文件”数组中的顺序很重要。应该是第一。花相当多的时间在这上面。 0_0
猜你喜欢
  • 2016-04-26
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
  • 2016-03-21
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多