在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
taro-testing-librarySimple and complete taro.js 2.x testing utilities that encourage good testing practices. Install// use yarnyarn add taro-testing-library -D// use npmnpm install taro-testing-library -D For Taro 3.xFor Taro 3.x and React, can use react-testing-library directly. the only need is to configure { moduleNameMapper: { '@tarojs/components': '@tarojs/components/dist-h5/react', ... },} Usageset { "preset": "taro-testing-library"} APIAPIrender
Result
renderToString
cleanupUnmounts the component from the container and destroys the container.
However, you may choose to skip the auto cleanup by setting the To make this even easier, you can also simply import DemoComponentimport Taro, { useState } from '@tarojs/taro';import { Text } from '@tarojs/components';const Counter = (props) => { const { initial = 1 } = props; const [count, setCount] = useState(initial) return ( <Text onClick={() => {setCount(count+1)}} className="number"> {count} </Text> );}; Testimport Taro from '@tarojs/taro';import { act, render } from 'taro-testing-library';test('should render component', () => { const { container } = render(<Counter />); const $number = container.querySelector('.number'); expect($number.innerHTML).toEqual('1');});test('should rerender when trigger setState hooks', () => { const { container } = render(<Counter />); const $number = container.querySelector('.number'); act(() => { $number.click() }) expect($number.innerHTML).toEqual(`2`);});it('should support snapshot', () => { const component = renderToString(<div>component without state</div>); expect(component).toMatchSnapshot();}) |
请发表评论