【发布时间】:2017-10-10 23:10:42
【问题描述】:
在我的代码中,我使用 babel-plugin-react-css-modules,它实际上与 react-css-modules 相同
我在 react 组件中有这样的代码。
styleName 用于 CSS 模块,我在 countdownForm.local.scss 中为它们定义样式
className 代表我的 Bootstrap 类
import './countdownForm.local.scss'
class CountdownForm extends Component {
...
render () {
return (
<div styleName="first" className="container">
...
</div>
)
}
}
要在我的测试中测试这个组件,我必须使用 ignore-styles 包,因为 JavaScript 无法处理 CSS 语法,当我们运行单元测试时,测试将尝试在没有 Webpack 的情况下导入 CSS 文件,这将导致错误。
import register from 'ignore-styles'
import React from 'react'
import test from 'tape'
import CountdownForm from 'CountdownForm'
register(undefined, () => ({styleName: 'fake_class_name'}))
test('CountdownForm => should exist', (t) => {
t.ok(CountdownForm)
t.end()
})
通过这样做,一切顺利,测试工作没有问题,但我总是在控制台中收到此警告
警告:标签上的未知道具
styleName。从 元素。
这不是功能问题,因为测试有效,但每次运行测试时都收到此消息很烦人。如何解决,摆脱它?
【问题讨论】:
标签: reactjs testing css-modules