Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 警告:失败的道具类型:道具打开在Snackbar中标记为所需,但其值未定义_Javascript_Reactjs_Unit Testing_Material Ui_Jestjs - Fatal编程技术网

Javascript 警告:失败的道具类型:道具打开在Snackbar中标记为所需,但其值未定义

Javascript 警告:失败的道具类型:道具打开在Snackbar中标记为所需,但其值未定义,javascript,reactjs,unit-testing,material-ui,jestjs,Javascript,Reactjs,Unit Testing,Material Ui,Jestjs,我试图在我的应用程序中引入jest快照测试 LoginForm组件 render() { return ( ... <DynamicSnack dialogOpen={this.props.dialogOpen} snackOpen={this.props.snackOpen} snackTimer={this.props.snackTimer} snackMessage={this.props.sna

我试图在我的应用程序中引入
jest
快照测试

LoginForm组件

render() {
    return (
    ...
    <DynamicSnack
        dialogOpen={this.props.dialogOpen}
        snackOpen={this.props.snackOpen}
        snackTimer={this.props.snackTimer}
        snackMessage={this.props.snackMessage}
    />
    )
}
import Snackbar from 'material-ui/Snackbar';

render() {
    let { snackOpen, snackTimer, snackMessage } = this.props

    return (
        <Snackbar
            open={snackOpen}
            message={snackMessage}
            autoHideDuration={snackTimer}
            onRequestClose={this.closeSnack}
        />
    )
}
render(){
返回(
...
)
}
动态NACK组件

render() {
    return (
    ...
    <DynamicSnack
        dialogOpen={this.props.dialogOpen}
        snackOpen={this.props.snackOpen}
        snackTimer={this.props.snackTimer}
        snackMessage={this.props.snackMessage}
    />
    )
}
import Snackbar from 'material-ui/Snackbar';

render() {
    let { snackOpen, snackTimer, snackMessage } = this.props

    return (
        <Snackbar
            open={snackOpen}
            message={snackMessage}
            autoHideDuration={snackTimer}
            onRequestClose={this.closeSnack}
        />
    )
}
从“物料ui/Snackbar”导入Snackbar;
render(){
让{snackOpen,snackTimer,snackMessage}=this.props
返回(
)
}
LoginForm.spec.js

import React from 'react'
import renderer from 'react-test-renderer'
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';

import LoginForm from '../../app/components/loginComponents/loginForm'

describe('LoginForm', () => {
    it('should render snapshot', () => {
        const component = renderer.create(
            <MuiThemeProvider>
                <LoginForm />
            </MuiThemeProvider>
        )
        const tree = component.toJSON()
        expect(tree).toMatchSnapshot()
    })
})
从“React”导入React
从“反应测试渲染器”导入渲染器
从“材质ui/styles/MuiThemeProvider”导入MuiThemeProvider;
从“../../app/components/loginComponents/LoginForm”导入LoginForm
描述('LoginForm',()=>{
它('应该呈现快照',()=>{
const component=renderer.create(
)
const tree=component.toJSON()
expect(tree.toMatchSnapshot())
})
})
警告

警告:失败的道具类型:道具
消息
Snackbar
中标记为所需,但其值为
未定义

警告:失败的道具类型:道具
打开
Snackbar
中标记为所需,但其值为
未定义

我已经尝试直接导入DynamicSnack组件,甚至Snackbar,并手动添加属性
open={false}message={'w00f'}
,但没有任何变化

我不熟悉单元测试,并尝试从学习
jest
开始


如何消除这些警告?

解决方案非常简单,在测试
LoginForm
时,您没有通过
Snackbar
所需的道具。将它们作为

 const component = renderer.create(
        <MuiThemeProvider>
            <LoginForm snackOpen={true}
    snackMessage={'Wrong info'}/>
        </MuiThemeProvider>
    )
const component=renderer.create(
)

我发誓我以前至少试过一次,但可能还有其他不同之处。现在它工作了:-)谢谢!也许你添加了,
open
message
而不是
snackOpen
snackMessage
我肯定我两个都试过了,但可能当时它没有包装在
MuiThemeProvider
中。在未来,我将尝试以一种不那么混乱的方式使用绝望的尝试:-我们如何测试当没有道具通过时是否抛出警告?