嘲笑css模块 [英] Mocking css modules in jest

查看:98
本文介绍了嘲笑css模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试中,我要导入一个 jsx 文件,该文件将导入 css :

In my test I am importing a jsx file which imports css:

import styles from '~/shared/styles/Profile.css';

我正在尝试使用 identity-obj-proxy 模拟所有CSS文件,因为我正在使用 css-modules .但这似乎不起作用.

I am trying to mock all css files with identity-obj-proxy as I am using css-modules. It doesn't seem to work though.

Package.json

"jest": {
    "moduleNameMapper":{
      "\\.(css|less)$": "identity-obj-proxy"
    },

我也尝试过:

"jest": {
    "moduleNameMapper":{
      "^.+\\.(css|less)$": "<rootDir>/config/CSSStub.js"
    },

在创建 CSSStub.css

module.exports = {};

无效-仍然来自Jest的解析CSS错误.

No effect - still the same parsing css error from Jest.

我在做什么错了?

推荐答案

我不知道您是否还在使用此文件,因为问题存在大约6个月了,但是我认为您应该将文件命名为CSSStub.js而不是CSSStub.css.

I don't know if you're still having this since the question is around 6 months old but I think you should name your file CSSStub.js instead of CSSStub.css.

这篇关于嘲笑css模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆