Skip to content

Instantly share code, notes, and snippets.

@Torthu
Created October 18, 2022 10:14
Show Gist options
  • Save Torthu/6dfcd2fcb2be48309105e1da2d90758b to your computer and use it in GitHub Desktop.
Save Torthu/6dfcd2fcb2be48309105e1da2d90758b to your computer and use it in GitHub Desktop.
Mock styled-components with jest
jest.mock('styled-components', () => {
const domElements = [
'a',
'abbr',
'address',
'area',
'article',
'aside',
'audio',
'b',
'base',
'bdi',
'bdo',
'big',
'blockquote',
'body',
'br',
'button',
'canvas',
'caption',
'cite',
'code',
'col',
'colgroup',
'data',
'datalist',
'dd',
'del',
'details',
'dfn',
'dialog',
'div',
'dl',
'dt',
'em',
'embed',
'fieldset',
'figcaption',
'figure',
'footer',
'form',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'head',
'header',
'hgroup',
'hr',
'html',
'i',
'iframe',
'img',
'input',
'ins',
'kbd',
'keygen',
'label',
'legend',
'li',
'link',
'main',
'map',
'mark',
'menu',
'menuitem',
'meta',
'meter',
'nav',
'noscript',
'object',
'ol',
'optgroup',
'option',
'output',
'p',
'param',
'picture',
'pre',
'progress',
'q',
'rp',
'rt',
'ruby',
's',
'samp',
'script',
'section',
'select',
'small',
'source',
'span',
'strong',
'style',
'sub',
'summary',
'sup',
'table',
'tbody',
'td',
'textarea',
'tfoot',
'th',
'thead',
'time',
'title',
'tr',
'track',
'u',
'ul',
'var',
'video',
'wbr', // SVG
'circle',
'clipPath',
'defs',
'ellipse',
'foreignObject',
'g',
'image',
'line',
'linearGradient',
'marker',
'mask',
'path',
'pattern',
'polygon',
'polyline',
'radialGradient',
'rect',
'stop',
'svg',
'text',
'tspan',
];
const props = ['css', 'keyframes', 'attrs', 'withComponent'].concat(domElements);
const returnFn = () => returnFn;
let returnObj = {
__esModule: true,
default: returnFn,
};
props.forEach((prop) => {
// @ts-expect-error someday they'll handle imperative assignment properly
returnFn[prop] = returnFn;
// @ts-expect-error someday they'll handle imperative assignment properly
returnObj[prop] = returnFn;
});
return returnObj;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment