Javascript Jest快照测试错误:您不应在外部使用
我想为我的页脚组件编写快照测试,但它抛出了一个错误:您不应该在外部使用。这是我的密码:Javascript Jest快照测试错误:您不应在外部使用,javascript,reactjs,testing,jestjs,enzyme,Javascript,Reactjs,Testing,Jestjs,Enzyme,我想为我的页脚组件编写快照测试,但它抛出了一个错误:您不应该在外部使用。这是我的密码: import React from 'react' import renderer from 'react-test-renderer' import Footer from '../footer' it('Footer renders correctly', () => { const tree = renderer .create(<Footer />)
import React from 'react'
import renderer from 'react-test-renderer'
import Footer from '../footer'
it('Footer renders correctly', () => {
const tree = renderer
.create(<Footer />)
.toJSON()
expect(tree).toMatchSnapshot()
})
但是现在它抛出了一个错误:浏览器历史需要一个DOM,我用MemoryRouter代替了BrowserRouter,它解决了这个问题
import React from 'react'
import { MemoryRouter } from 'react-router-dom'
import renderer from 'react-test-renderer'
import Footer from '../footer'
it('Footer renders correctly', () => {
const tree = renderer
.create(
<MemoryRouter>
<Footer />
</MemoryRouter>
)
.toJSON()
expect(tree).toMatchSnapshot()
})
我用MemoryRouter代替了BrowserRouter,它解决了这个问题
import React from 'react'
import { MemoryRouter } from 'react-router-dom'
import renderer from 'react-test-renderer'
import Footer from '../footer'
it('Footer renders correctly', () => {
const tree = renderer
.create(
<MemoryRouter>
<Footer />
</MemoryRouter>
)
.toJSON()
expect(tree).toMatchSnapshot()
})