【问题标题】:Jest with Styled Components error: Syntax error parsing expected css: missing '}'Jest with Styled Components 错误:解析预期 css 的语法错误:缺少“}”
【发布时间】:2021-03-07 17:00:45
【问题描述】:

我正在尝试将 jest 与样式化组件一起使用来测试组件的样式,但我遇到了错误:

Syntax error parsing expected css: missing '}' here
Failing css:
[object Object]

我的组件是一个带有 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' })
}

我正在运行 npm react-scripts 测试。

我尝试导入“jest-styled-components”和“@testing-library/jest-dom”

【问题讨论】:

    标签: reactjs jestjs styled-components


    【解决方案1】:

    可能是因为你使用的@testing-library/jest-dom包的版本。

    对于v4.2.4,toHaveStyle(css: string),可以看到,匹配器只支持string参数。因此,您应该使用

    expect(link).toHaveStyle(`color: #fff`);
    

    v5.1.0起支持JS对象参数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 2018-12-11
      • 2021-11-05
      • 2014-11-08
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多