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
oracle
windows-8.1
couchdb
activemq
loopbackjs
plpgsql
initialization
histogram
mathematical-optimization
fogbugz
openflow
jint
visualforce
mutation-testing
bellman-ford
chromecast
cname
smartgwt
sonata
trace32
xbox360
web-deployment
tomcat8
exe
gravity
boomi
codefluent
yii2-basic-app
backendless
react-dnd
jstree
managed-c++
prompt
windows2012
delayed-job
plantuml
sca
cloveretl
image-optimization
xll
text-decorations
rdp
task-parallel-library
grails3.2.0
pyttsx
vungle-ads
google-prediction
virtual-memory
latexml
git-tfs
section508
uiautomatorviewer
beeline
freshdesk
android-mediarecorder
dynamics-crm-4
openerp-6
gapi
ternary-operator
portal
researchkit
thread-exceptions
msbuild-4.0
fiber
qpixmap
static-methods
deferred-rendering
highlighting
jsr363
myfaces
phpquery
perceptron
mousehover
fanotify
search-form
explain
azure-xplat-cli
prism-4
nssortdescriptor
operands
dos2unix
adhoc-polymorphism
configurable-product
windows-rt
back
maven-central
prism.js
sql-function
spring-social-linkedin
lsa
spring-security-acl
smips
ctp
jmenubar
libxml-js
chartfx
days
jbox2d
itunes-sdk
cyrillic
jsplitpane
clrprofiler
border-box
image-formats
fitch-proofs
type-safety
glassfish-esb
resharper-5.1
yahoo-maps

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App