首页 > 解决方案 > “toHaveStyleRule”在 Jest(React Typescript)中不起作用,但是 toHaveStyle 可以

问题描述

我正在做一些测试并且 toHaveStyle 没有返回任何错误,但是当我在 toHaveStyleRule 中应用相同的语法时,它最终给了我以下错误:

  No style rules found on passed Component

      21 |
      22 |     // error
    > 23 |     expect(screen.getByTestId('icone')).toHaveStyleRule('width', '5rem')

但是测试是一样的!有人可以告诉我错误的原因吗?

测试:

// Ok
    expect(screen.getByTestId('icone')).toHaveStyle({ width: '5rem' })

// Error
expect(screen.getByTestId('icone')).toHaveStyleRule('width', '5rem')

.Babelrc:

{
  "presets": ["next/babel", "@babel/preset-typescript"],
  "plugins": [
    [
      "babel-plugin-styled-components",
      {
        "ssr": true,
        "displayName": true
        
      }
    ]
  ],
  "env": {
    "test": {
      "plugins": [
        [
          "babel-plugin-styled-components",
          {
            "ssr": false,
            "displayName": false
          }
        ]
      ]
    }
  }
}

标签: jestjsts-jest

解决方案


推荐阅读