{"record":{"id":"df8932a00793f007","repo":"emotion-js/emotion","slug":"tohavestylerule-expects-to-receive-a-single-elem","errorCode":null,"errorMessage":"`toHaveStyleRule` expects to receive a single element but it received an array.","messagePattern":"`toHaveStyleRule` expects to receive a single element but it received an array\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest/src/matchers.js","lineNumber":48,"sourceCode":"  if (value instanceof RegExp) {\n    return value.test(declaration.children)\n  }\n\n  if (isAsymmetric(value)) {\n    return value.asymmetricMatch(declaration.children)\n  }\n\n  return value === declaration.children\n}\n\nfunction toHaveStyleRule(\n  received,\n  property,\n  value,\n  options /* ?: { target?: string | RegExp, media?: string } */ = {}\n) {\n  if (Array.isArray(received)) {\n    throw new Error(\n      '`toHaveStyleRule` expects to receive a single element but it received an array.'\n    )\n  }\n  const { target, media } = options\n  const classNames = getClassNamesFromNodes([received])\n  const cssString = getStylesFromClassNames(classNames, getStyleElements())\n  let preparedRules = stylis.compile(cssString)\n  if (media) {\n    preparedRules = getMediaRules(preparedRules, media)\n  }\n  const result = preparedRules\n    .filter(\n      rule =>\n        rule.type === 'rule' && hasClassNames(classNames, rule.props, target)\n    )\n    .reduce((acc, rule) => {\n      const lastMatchingDeclaration = findLast(\n        rule.children,","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/emotion-js/emotion/blob/b882bcba85132554992e4bd49e94c95939bbf810/packages/jest/src/matchers.js#L30-L66","documentation":"toHaveStyleRule matches style rules against a single element's class names. Arrays are not supported, so the matcher throws immediately when the received value is an array. Callers must select one element (e.g. wrapper.get(0) or find(...)) before asserting.","triggerScenarios":"Calling expect(nodes).toHaveStyleRule(...) where nodes is an array — e.g. ReactTestingLibrary's screen.getAllByRole(...), wrapper.find(...).nodes, or querySelectorAll results.","commonSituations":"Using getAllBy* query variants, passing wrapper.children() (which returns an array in some Enzyme versions), or destructuring a find() result array into the matcher.","solutions":["Select a single element: expect(getAllByRole('button')[0]).toHaveStyleRule(...)","Use wrapper.find() (returns a wrapper) or .getDOMNode() / .at(0) instead of arrays","Loop over the array and assert each element individually","Use toHaveStyleRule on an element's first child via children().at(0)"],"exampleFix":"// before\nexpect(screen.getAllByRole('button')).toHaveStyleRule('color', 'red')\n\n// after\nscreen.getAllByRole('button').forEach(button => {\n  expect(button).toHaveStyleRule('color', 'red')\n})","handlingStrategy":"type-guard","validationCode":"if (Array.isArray(received)) {\n  throw new TypeError('toHaveStyleRule expects a single element, got an array')\n}","typeGuard":"function isSingleElement(x) {\n  return !Array.isArray(x) && x != null && typeof x === 'object'\n}","tryCatchPattern":null,"preventionTips":["Avoid getAllBy* for style assertions; assert per element","Use .at(0)/[0]/getDOMNode() to unwrap wrappers before asserting","Add a test util that unwraps arrays automatically"],"tags":["jest","matcher","assertion","enzyme"],"backgroundTag":"matcher-received-array","analyzedSha":"b882bcba85132554992e4bd49e94c95939bbf810","analyzedAt":"2026-09-02T22:27:13.739Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}