1 Star 3 Fork 0

Thoughtworks / taro-testing-library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

taro-testing-library

Simple and complete taro.js 2.x testing utilities that encourage good testing practices.

Actions Status NPM license codecov

Install

// use yarn
yarn add taro-testing-library -D
// use npm
npm install taro-testing-library -D

For Taro 3.x

For Taro 3.x and React, can use react-testing-library directly.

the only need is to configure moduleNameMapper in jest.config.js like this.

{
  moduleNameMapper: {
    '@tarojs/components': '@tarojs/components/dist-h5/react',
    ...
  },
}

Usage

set preset in your jest config file

{
  "preset": "taro-testing-library"
}

API

API

render

  • render(Component, { container, target }) => { container, unmount, rerender }: render method to mount a component

    • container: The HTML element the component is mounted into.

      default : document.body

    • target: The HTML element the component is mounted.

      default : container.appendChild(document.createElement('div'))

Result

  • container: container
  • component: created Taro.js component
  • rerender(Component): method of rerender component
  • unmount(): method of unmount component
  • debug(): method of log current dom
  • ...queries: Returns all query functions that are binded to the target.

renderToString

renderToString(Component) => string: export from nerv-server, you can use it to match snapshot

cleanup

Unmounts the component from the container and destroys the container.

cleanup() is called after each test automatically by default if the testing framework you're using supports the afterEach global (like mocha, Jest, and Jasmine).

However, you may choose to skip the auto cleanup by setting the TTL_SKIP_AUTO_CLEANUP env variable to 'true'.

To make this even easier, you can also simply import taro-testing-library/dont-cleanup-after-each which will do the same thing.

Demo

Component

import 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>
  );
};

Test

import 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();
})
MIT License Copyright (c) 2020 ThoughtWorks China Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Simple Taro.js 2.x testing utilities that encourage good testing practices. 展开 收起
TypeScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/thoughtworks/taro-testing-library.git
git@gitee.com:thoughtworks/taro-testing-library.git
thoughtworks
taro-testing-library
taro-testing-library
master

搜索帮助