简体   繁体   English

如何在 Jest 中模拟 Styled JSX 'global' 属性

[英]How to mock Styled JSX 'global' property in Jest

When trying to run tests on a NextJS project i'm coming up against the following error:当尝试在 NextJS 项目上运行测试时,我遇到了以下错误:

TypeError: _css.default.global is not a function

I've added a __mocks__/styled-jsx/css.js file that contains the following code in order to mock StyledJSX:为了模拟 StyledJSX,我添加了一个包含以下代码的__mocks__/styled-jsx/css.js文件:

const css = () => {
  return "";
};

export default css;

This takes care of the files that contain normal css decalrations from styled-jsx, but not for my App.js file which contains这会处理包含来自 styled-jsx 的普通 css 声明的文件,但不适用于我的 App.js 文件,其中包含

// CSS reset
const styles = css.global`
  html,
  body,
  div,
  span,
  object,
  iframe,
  ...

and

<style jsx global>{`
          @import "${mockHeadImport}";
          ${mockBodyImport === mockHeadImport
            ? ""
            : `@import "${mockBodyImport}";`}

          body {
          ...

Any pointers would be appreciated!任何指针将不胜感激!

SOLUTION解决方案

I've changed my import inside App.tsx from import css from "styled-jsx/css";我已经改变了我在App.tsximport css from "styled-jsx/css"; from import css from "styled-jsx/css"; to import { global } from "styled-jsx/css"; import { global } from "styled-jsx/css"; , and my css declaration from css.global to global , and modifed my mock css.js file to: ,以及我的 css 声明从css.globalglobal ,并将我的模拟css.js文件修改为:

const css = () => {
  return "";
};

const global = () => {
  return "";
};

export { css as default, global };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM