【发布时间】:2017-08-21 18:00:32
【问题描述】:
我正在使用 React v15.4、babel-jest v18 和酶 v2.5.1
我有一个简单的 React 组件:
import React, {Component} from 'react'
import {FormattedRelative} from 'react-intl'
import pageWithIntl from '../components/PageWithIntl'
import Layout from '../components/Layout'
class About extends Component {
static async getInitialProps ({req}) {
return {someDate: Date.now()}
}
render () {
return (
<Layout>
<h1>About</h1>
<p>
<FormattedRelative
value={this.props.someDate}
updateInterval={1000}
/>
</p>
</Layout>
)
}
}
export default pageWithIntl(About)
还有一个简单的笑话/酶测试:
/* 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 />
)
expect(about.find('h1').text()).toEqual('About')
})
})
Jest 测试应该通过,但我收到了一个错误:
方法“text”仅用于在单个节点上运行。 0 找到 而是。
我错过了什么?
=== 更新
快照测试通过:
describe('With Snapshot Testing', () => {
it('About shows "About"', () => {
const component = renderer.create(<About />)
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
})
})
有没有办法将酶期望测试集成到快照测试中?怎么做?
【问题讨论】: