{"record":{"id":"76b69e67fba5e691","repo":"emotion-js/emotion","slug":"jest-emotion-requires-jsdom-see-https-jestjs-io","errorCode":null,"errorMessage":"jest-emotion requires jsdom. See https://jestjs.io/docs/en/configuration#testenvironment-string for more information.","messagePattern":"jest-emotion requires jsdom\\. See https://jestjs\\.io/docs/en/configuration#testenvironment-string for more information\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest/src/utils.js","lineNumber":290,"sourceCode":"    styles = styles.replace(keyframesNamePattern, name => {\n      if (keyframesNameCache[name] === undefined) {\n        keyframesNameCache[name] = `animation-${index++}`\n        keyframesStyles += keyframesMap[name]\n      }\n      return keyframesNameCache[name]\n    })\n\n    keyframesStyles = keyframesStyles.replace(keyframesNamePattern, value => {\n      return keyframesNameCache[value]\n    })\n  }\n\n  return (keyframesStyles + styles).replace(removeCommentPattern, '')\n}\n\nexport function getStyleElements() /*: Array<HTMLStyleElement> */ {\n  if (!isBrowser) {\n    throw new Error(\n      'jest-emotion requires jsdom. See https://jestjs.io/docs/en/configuration#testenvironment-string for more information.'\n    )\n  }\n  const elements = Array.from(document.querySelectorAll('style[data-emotion]'))\n  return elements\n}\n\nconst unique = arr => Array.from(new Set(arr))\n\nexport function getKeys(elements /*: Array<HTMLStyleElement> */) {\n  const keys = unique(\n    elements.map(element => element.getAttribute('data-emotion'))\n  ).filter(Boolean)\n  return keys\n}\n\nexport function hasClassNames(\n  classNames /*: Array<string> */,","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/emotion-js/emotion/blob/b882bcba85132554992e4bd49e94c95939bbf810/packages/jest/src/utils.js#L272-L308","documentation":"jest-emotion's matchers read rendered <style data-emotion> elements from the document, which only exists under a DOM implementation like jsdom. When getStyleElements runs in a non-browser (node) test environment, it throws and points to Jest's testEnvironment docs. Without jsdom there is no document to inspect, so assertions are impossible.","triggerScenarios":"Running tests with testEnvironment: 'node' (or 'jest-environment-node') and calling toHaveStyleRule, getStyleElements, or emotion snapshot serializers.","commonSituations":"A repo-wide jest config defaulted to node environment; a monorepo where jest-emotion tests inherit a node env; newly added test files not covered by a jsdom docblock.","solutions":["Set testEnvironment: 'jsdom' in jest.config (or per-file `@jest-environment jsdom` docblock)","Install/verify jest-environment-jsdom is present (Jest 28+ ships it separately)","Add `/** @jest-environment jsdom */` at the top of files using emotion matchers","Split style-assertion tests into a jsdom project via jest projects config"],"exampleFix":"// before (jest.config.js)\nmodule.exports = { testEnvironment: 'node' }\n\n// after\nmodule.exports = { testEnvironment: 'jsdom' }","handlingStrategy":"validation","validationCode":"// guard before using emotion matchers\nif (typeof document === 'undefined') {\n  throw new Error('jest-emotion tests require jsdom: set testEnvironment: \"jsdom\"')\n}","typeGuard":"const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined'","tryCatchPattern":null,"preventionTips":["Set testEnvironment: 'jsdom' in jest config","Add @jest-environment jsdom docblocks to DOM-dependent test files","Install jest-environment-jsdom explicitly on Jest 28+"],"tags":["jest","jsdom","environment","configuration"],"backgroundTag":"missing-jsdom-environment","analyzedSha":"b882bcba85132554992e4bd49e94c95939bbf810","analyzedAt":"2026-09-02T22:27:13.739Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}