jest


Error: Invariant Violation: Element type is invalid


I caught this error when I used Jest.
header-test.js
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import Header from '../app/components/layouts/header.js';
describe('<Header/>', () => {
const Header = TestUtils.renderIntoDocument(<Header />);
const span = TestUtils.scryRenderedDOMComponentsWithTag(Header, "span");
it('header context (This is the header)', () => {
const header_context_node = ReactDOM.findDOMNode(span[0]);
expect(header_context_node).toEqual('This is the header');
});
});
And here's what in header.js
import React from 'react';
class Header extends React.Component {
render() {
return (
<div className="main-header">
<ul>
<li className="main-header-left">
<span>This is the header</span>
</li>
</ul>
</div>
)
}
}
export default Header
I read some related questions, they all say this error is caused by import {Component} from './Component.js'. But in my case, I did not use { } but still have that error.
Here's the detail of the error
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.
at invariant (node_modules/react-dom/node_modules/fbjs/lib/invariant.js:44:15)
at ReactCompositeComponentWrapper.instantiateReactComponent [as _instantiateReactComponent] (node_modules/react-dom/lib/instantiateReactComponent.js:77:56)
at ReactCompositeComponentWrapper.performInitialMount (node_modules/react-dom/lib/ReactCompositeComponent.js:367:22)
at ReactCompositeComponentWrapper.mountComponent (node_modules/react-dom/lib/ReactCompositeComponent.js:258:21)
at Object.mountComponent (node_modules/react-dom/lib/ReactReconciler.js:46:35)
at mountComponentIntoNode (node_modules/react-dom/lib/ReactMount.js:104:32)
at ReactReconcileTransaction.perform (node_modules/react-dom/lib/Transaction.js:140:20)
at batchedMountComponentIntoNode (node_modules/react-dom/lib/ReactMount.js:126:15)
at ReactDefaultBatchingStrategyTransaction.perform (node_modules/react-dom/lib/Transaction.js:140:20)
at Object.batchedUpdates (node_modules/react-dom/lib/ReactDefaultBatchingStrategy.js:62:26)
at Object.batchedUpdates (node_modules/react-dom/lib/ReactUpdates.js:97:27)
at Object._renderNewRootComponent (node_modules/react-dom/lib/ReactMount.js:320:18)
at Object._renderSubtreeIntoContainer (node_modules/react-dom/lib/ReactMount.js:401:32)
at Object.render (node_modules/react-dom/lib/ReactMount.js:422:23)
at Object.renderIntoDocument (node_modules/react-dom/lib/ReactTestUtils.js:79:21)
at Suite.<anonymous> (__tests__/header-test.js:8:47)
at Object.<anonymous> (__tests__/header-test.js:6:1)
at process._tickCallback (internal/process/next_tick.js:109:7)
<Header/>
✕ encountered a declaration exception (3ms)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 1.704s
Ran all test suites.
console.error node_modules/react/node_modules/fbjs/lib/warning.js:36
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.
You are importing correctly - you only need to export and import { Header } if you're wrapping the component (with redux's connect, for example).
If you're using Jest, why not use the built in tools?
import React from 'react';
import { shallow } from 'enzyme';
import Header from '../app/components/layouts/header.js';
describe('<Header />', () => {
it('header context (This is the header)', () => {
const component = shallow(<Header />);
expect(component.find('span').text()).toEqual('This is the header');
});
});

Related Links

Is there a good report for jest test case?
Jest is hung there
Error: Invariant Violation: Element type is invalid
maxTotalConnection and defaultMaxTotalConnectionPerRoute
Calling CreateIndex with settings without using ImmutableSettings

Categories

HOME
memory
applescript
biztalk
portable-class-library
mathematical-optimization
google-sheets-query
visualforce
video-streaming
firebase-database
onvif
u-boot
spring-security-oauth2
yeoman-generator-angular
mpu6050
mahout-recommender
global-variables
closures
word2vec
home
data-type-conversion
intel
firemonkey-style
router
tortoisemerge
tracing
clipboard
bobo-browse.net
basic-authentication
copy-protection
source-insight
snap-framework
xcrun
landscape
unification
extbase
python-jira
system-on-chip
opal-framework
word-cloud
web-inspector
virtual-memory
audio-converter
axis
wysihtml5
rm
ptvs
network-protocols
polymaps
msxml
instafeedjs
openerp-6
nrf51
bpms
spring-mongo
conan
android-syncadapter
simian
urbit
verisign
vim-plugin
django-validation
outlook.com
python-cffi
html.actionlink
extend
dbmigrate
deque
ssmtp
mojo
directorysearcher
operands
datagridcomboboxcolumn
producer
lumia-imaging-sdk
jchartfx
lru
.net-remoting
donut-chart
proc-open
icenium
system-requirements
onconfigurationchanged
zend-pdf
system-information
pageheap
infopath-2007
jquery-1.4
mongrel2
resharper-5.1
3-tier
tacit-programming
gtktextview
downcasting
version-control-migration
server-load
code-camp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App