我正在使用 React 构建一个基本的博客应用程序。我正在使用 Jasmine 和 Karma 来运行我的前端测试。我启动并运行了第一个测试,它在 Chrome (Chromium) 和 Firefox 中通过,但是当它在 PhantomJS 中运行时,我收到以下错误:
PhantomJS 1.9.8 (Linux 0.0.0) ERROR
TypeError: 'undefined' is not a function (evaluating 'ReactElementValidator.createElement.bind(
null,
type
)')
at /home/michael/repository/short-stories/test/karma_tests/story_test.js:1742
我的测试文件如下所示:
var React = require('react/addons');
var Story = require('../../app/js/components/story.jsx');
var TestUtils = React.addons.TestUtils;
var testUtilsAdditions = require('react-testutils-additions');
describe('Story component', function () {
var component;
beforeEach(function () {
component = TestUtils.renderIntoDocument(React.createElement('story'));
component.props.storyTitle = 'front end test title';
component.props.author = 'front end author';
component.props.storyText = 'front end story text';
});
it('should display a story', function () {
expect(component.props).toBeDefined();
expect(component.props.storyTitle).toBeDefined();
expect(component.props.storyTitle).toBe('front end test title');
expect(component.props.author).toBe('front end author');
expect(component.props.storyText).toBe('front end story text')
});
});
我尝试删除我的node_modules,并清除npm缓存和npm安装,但它没有修复它。我不确定我的测试如何在 Firefox 和 Chrome 中通过,但在 PhantomJS 中却不行。您可以在这里查看完整的项目:https://github.com/mrbgit/short-stories https://github.com/mrbgit/short-stories。如果有更多信息可以提供帮助,请告诉我。任何帮助表示赞赏。谢谢!
PhantomJS 使用相当旧版本的 Qt-Webkit,不提供Function.prototype.bind https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind。这对于很多图书馆来说都是一个问题,所以polyfill NPM 模块名为“phantomjs-polyfill” https://www.npmjs.com/package/phantomjs-polyfill可用。
如果您不想使用 NPM 模块(如果您正在测试尚未与 browserify/webpack 捆绑在一起的浏览器站点),请使用以下 polyfillbind
MDN页面上提供了,你可以自己附上:
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)