Enzyme: Method “text” is only meant to be run on a single node. 0 found instead
Use .first()
example const wrapper = shallow()
wrapper.find('h1 or p or .ClassName or #id').first();
import React from 'react'
import { shallow } from 'enzyme'
import renderer from 'react-test-renderer'
import About from '../pages/about.js'
describe('With Enzyme', () => {
it('App shows "About"', () => {
const about = shallow(
<About />
)
expect(about.find('h1').first().text()).toEqual('About')
})
})
Its caused by the fact that shallow does not render child componnets and your component been wrapped by a function. So shallow only returns a representation of the function not of the component.
You can use dive()
to reach the real component
/* global it, expect, describe */
import React from 'react'
import { shallow } from 'enzyme'
import renderer from 'react-test-renderer'
import About from '../pages/about.js'
describe('With Enzyme', () => {
it('App shows "About"', () => {
const about = shallow(
<About />
).dive()
expect(about.find('h1').text()).toEqual('About')
})
})