简体   繁体   English

TypeError:无法读取未定义的属性“ apply”

[英]TypeError: Cannot read property 'apply' of undefined

Can't testing a react component. 无法测试反应组件。 Below I put code of my setup.js - my configure file and text of error. 在下面,我放置了setup.js的代码-我的配置文件和错误文本。

 require('babel-register')(); var jsdom = require('jsdom').jsdom; var exposedProperties = ['window', 'navigator', 'document']; global.document = jsdom(''); global.window = document.defaultView; Object.keys(document.defaultView).forEach((property) => { if (typeof global[property] === 'undefined') { exposedProperties.push(property); global[property] = document.defaultView[property]; } }); global.navigator = { userAgent: 'node.js' }; // var documentRef = document; // process.env.NODE_ENV = 'test'; function noop() { return null; } require.extensions['.css'] = noop; require.extensions['.scss'] = noop; require.extensions['.md'] = noop; require.extensions['.png'] = noop; require.extensions['.svg'] = noop; require.extensions['.jpg'] = noop; require.extensions['.jpeg'] = noop; require.extensions['.gif'] = noop; 

Test code. 测试代码。 This is very simple test and it fails.( 这是非常简单的测试,但失败了。

Editing. 编辑中。 My component code is below. 我的组件代码如下。

 import React from 'react'; import {mount, shallow} from 'enzyme'; import {expect} from 'chai'; import Header from '../../src/components/Header'; describe ('<Header />', () => { it('should have a logo image', function() { const wrapper = mount(<Header />); expect(wrapper.find('img')).to.have.length(1); }); }); 

at WithStyles.componentWillMount (node_modules\\isomorphic-style-loader\\lib\\withStyles.j at node_modules\\react-dom\\lib\\ReactCompositeComponent.js:348:23 at measureLifeCyclePerf (node_modules\\react-dom\\lib\\ReactCompositeComponent.js:75:12) 在WithStyles.componentWillMount(在node_modules \\ react-dom \\ lib \\ ReactCompositeComponent.js:node:modules \\ isomorphic-style-loader \\ lib \\ withStyles.j在measureLifeCyclePerf((node_modules \\ react-dom \\ lib \\ ReactCompositeComponent.js:75 :12)

 class Header extends React.Component { static propTypes = { intl: intlShape.isRequired, }; componentDidMount() { this.searchbox.refs.queryField.focus(); } render() { return ( <div className={s.root}> <div className={s.container}> <img className={s.alphaRibbon} src={alphaRibbon} alt="alpha" width="50px" /> <Link className={s.brand} to="/"> <img src={logoUrl2x} srcSet={`${logoUrl2x} 2x`} width="67" height="38" alt="8kolo" /> <span className={s.brandTxt}> <FormattedMessage {...messages.brand} /> </span> </Link> <Navigation className={s.nav} /> <div className={s.search}> <SearchBoxRedirect ref={sb => { this.searchbox = sb; }} hitsRoute="/" searchOnChange placeholder={this.props.intl.formatMessage(messages.searchPlaceholder)} prefixQueryFields={['full_name']} /> </div> {/* <LanguageSwitcher /> */} </div> {/* <div className={s.banner}> <div className={s.container}> <FormattedMessage tagName="p" {...messages.bannerDesc} /> </div> </div>*/} </div> ); } } export default withStyles(s)(injectIntl(Header)); 

You're using webpack with isomorphic-style-loader to resolve dependencies to CSS files, but you're not using webpack in your test setup, right? 您正在将webpack与isomorphic-style-loader一起使用来解析对CSS文件的依赖性,但是您没有在测试设置中使用webpack,对吗? To emulate the style loading in your test setup, try using css-modules-require-hook . 要在测试设置中模拟样式加载,请尝试使用css-modules-require-hook

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Redux TypeError:无法读取未定义的属性“应用” - Redux TypeError: Cannot read property 'apply' of undefined “TypeError:无法读取属性'apply'of undefined” - “TypeError: Cannot read property 'apply' of undefined” 类型错误:无法在 EventEmitter 读取未定义的属性“应用” - TypeError: Cannot read property 'apply' of undefined at EventEmitter TypeError:无法在测试中使用javascript应用读取未定义的属性“ apply”? - TypeError: Cannot read property 'apply' of undefined, using javascript apply in tests? D3和Javascript:未捕获的TypeError:无法读取未定义的属性“应用” - D3 and Javascript: Uncaught TypeError: Cannot read property 'apply' of undefined 未捕获(承诺):类型错误:无法读取 ionic 2 应用程序中未定义的属性“应用” - Uncaught (in promise): TypeError: Cannot read property 'apply' of undefined in ionic 2 app 未捕获的TypeError:无法读取未定义的移相器的属性“apply” - Uncaught TypeError: Cannot read property 'apply' of undefined phaser 未捕获的TypeError:无法使用phaser读取未定义的属性“apply” - Uncaught TypeError: Cannot read property 'apply' of undefined with phaser LinkedIn JavaScript SDK-未捕获的TypeError:无法读取未定义的属性“ apply” - LinkedIn JavaScript SDK - Uncaught TypeError: Cannot read property 'apply' of undefined 未捕获的类型错误:无法读取未定义的 react redux 的属性“apply” - Uncaught TypeError: Cannot read property 'apply' of undefined react redux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM