【问题标题】:How to mock Styled JSX 'global' property in Jest如何在 Jest 中模拟 Styled JSX 的“全局”属性
【发布时间】:2021-11-30 21:55:25
【问题描述】:

当尝试在 NextJS 项目上运行测试时,我遇到了以下错误:

TypeError: _css.default.global is not a function

为了模拟 StyledJSX,我添加了一个包含以下代码的 __mocks__/styled-jsx/css.js 文件:

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

export default css;

这会处理包含来自 styled-jsx 的普通 css 声明的文件,但不适用于我的 App.js 文件,其中包含

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

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

          body {
          ...

任何指针将不胜感激!

【问题讨论】:

标签: reactjs typescript jestjs next.js styled-jsx


【解决方案1】:

解决方案

我已将App.tsx 中的导入从import css from "styled-jsx/css"; 更改为import { global } from "styled-jsx/css";,并将我的css 声明从css.global 更改为global,并将我的模拟css.js 文件修改为:

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

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

export { css as default, global };

【讨论】:

    猜你喜欢
    • 2020-05-13
    • 2017-03-19
    • 2016-05-20
    • 2018-12-15
    • 2019-11-22
    • 2022-10-21
    • 2019-08-12
    • 2019-05-12
    • 2020-06-08
    相关资源
    最近更新 更多