简体   繁体   English

如何在 Next.js styled-jsx 中使用 jest 覆盖?

[英]How can I use jest coverage in Next.js styled-jsx?

I'm trying to use test framework Jest with Next.js and styled-jsx.我正在尝试将测试框架 Jest 与 Next.js 和 styled-jsx 一起使用。

This is my external css style.这是我的外部 css 样式。

import css from 'styled-jsx/css';

export const TextStyle = css`
     p {
         font-size: 14px;
         color: #a8a8a8;
     }
`;

and this is my package.json script.这是我的 package.json 脚本。 It works well.它运作良好。

"test": "jest --verbose",

I want to use coverage option.我想使用覆盖选项。 so I tried this "test": "jest --coverage --verbose" , but it is not working.所以我尝试了这个"test": "jest --coverage --verbose" ,但它不起作用。

ReferenceError: css is not defined参考错误:未定义 css

I have read this https://github.com/zeit/styled-jsx/issues/436 .我已经阅读了这个https://github.com/zeit/styled-jsx/issues/436 However, the issue is still open and doesn't help me.但是,问题仍然存在并且对我没有帮助。

How can I fix it?我该如何解决?

styled-jsx docs mention how to solve it but that didn't work well for me. styled-jsx文档提到了如何解决它,但这对我来说效果不佳。

I made it work by doing the following: Set the node env in your package.json test scripts to "test" like so (can add the --verbose flag as well if you want):我通过执行以下操作使其工作:将package.json测试脚本中的节点 env 设置为“测试”,就像这样(如果需要,也可以添加--verbose标志):

    "test": "NODE_ENV=test jest",
    "test:coverage": "NODE_ENV=test jest --coverage"

In your babel configuration ( .babelrc in my case) add babel-test: true to the test env config like so (refer to the Next docs for more details):在您的 babel 配置(在我的情况下为.babelrc )中,像这样将babel-test: true添加到test配置中(有关更多详细信息,请参阅Next 文档):

{
  // this is your original config, it's required if you don't have a babel config yet and are using `Next` since `Next` normally does it under the hood for you:
  "presets": [
    [
      "next/babel",
    ]
  ],
  // this is what you're adding:
  "env": {
    "test": {
      "presets": [
        [
          "next/babel",
          {
            "styled-jsx": {
              "babel-test": true
            }
          }
        ]
      ]
    }
  }
}

Your tests should now pass but may show a warning saying: styled-jsx/css: if you are getting this error it means that your css tagged template literals were not transpiled.您的测试现在应该通过,但可能会显示一条警告: styled-jsx/css: if you are getting this error it means that your css tagged template literals were not transpiled.

In that case you should add a jest auto mock for styled-jsx/css by adding a new file with this exact path from the root of your project (the __mocks__ folder has to be a sibling of your node_modules folder) /__mocks__/styled-jsx/css.js :在这种情况下,你应该添加一个jest自动模拟的styled-jsx/css通过添加新的文件,从项目的根这个确切路径__mocks__文件夹必须是你的兄弟node_modules文件夹) /__mocks__/styled-jsx/css.js

function css() {
  return ""
}

module.exports = css

*Note: what this whole setup does is disable styled-jsx transpilation when you run your tests which means that the generated classes will not be generated in your test components. *注意:整个设置的作用是在您运行测试时禁用 styled-jsx 转换,这意味着生成的类不会在您的测试组件中生成。 In my case for example that breaks my tests because I'm relying on the generated classes for hiding some elements and my tests rely on those elements being hidden.例如,在我的情况下,这会破坏我的测试,因为我依赖于生成的类来隐藏某些元素,而我的测试依赖于那些被隐藏的元素。 I'm now trying to figure out a way around that but it may not be an issue in your case.我现在正想办法解决这个问题,但在您的情况下可能不是问题。

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

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