gpt4 book ai didi

unit-testing - Jest with React - 如何渲染整个组件并对其进行测试?

转载 作者:行者123 更新时间:2023-11-28 20:28:45 25 4
gpt4 key购买 nike

假设我有一个简单的 App 组件,我正在对其运行测试以查看它是否只是返回一个将 Hello World 打印到屏幕上的 div。为了在我的测试文件中呈现应用程序,函数助手是什么?此外,为了测试正在呈现的 HTML,在 expect 调用中调用什么函数?

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'

describe("App", () => {
it('prints "HELLO WORLD" to the screen', () => {
expect(**App??**).**toRENDER??**("HELLO WORLD")
}
}

谢谢

最佳答案

最好使用 enzyme来自 airbnb 的图书馆。

有一个example两者一起使用

你的代码看起来像下一个:

import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import {shallow} from 'enzyme'

describe("App", () => {
it('prints "HELLO WORLD" to the screen', () => {
const wrapper = shallow(<App />)

expect(wrapper.text()).toBe("HELLO WORLD")
}
}

关于unit-testing - Jest with React - 如何渲染整个组件并对其进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40187451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com