【发布时间】:2021-03-07 08:19:36
【问题描述】:
我正在尝试使用 jest-dom 来测试组件的样式,但我遇到了错误:
"TypeError: expect(...).toHaveStyle is not a function"
我的组件是一个带有 styled-components 的简单链接:
import styled from 'styled-components'
export const Link = styled.a`
color: #fff;
`
在我正在做的测试中:
describe('Link', () => {
it('should display color on the link', () => {
render(<Link href="/x">Test</Link>)
}
expect(screen.getByRole('link', { name: /test/i })).toHaveStyle({ color: '#fff' })
}
我创建了一个设置文件 (jest.config.js):
module.exports = {
setupFilesAfterEnv: ['<rootDir>/.jest/setup.js'],
}
我在项目的根目录下创建了 .jest / setup.js 文件并导入了 js-dom:
import '@testing-library/jest-dom'
【问题讨论】:
标签: reactjs jestjs styled-components react-testing-library jest-dom