All Projects → testing-library → Jest Native

testing-library / Jest Native

Licence: mit
🦅 Custom jest matchers to test the state of React Native

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jest Native

Reactconfbr
Public infos and issues about React Conf Brasil organization
Stars: ✭ 156 (-20.41%)
Mutual labels:  jest
Snap Shot
Jest-like snapshot feature for the rest of us, works magically by finding the right caller function
Stars: ✭ 170 (-13.27%)
Mutual labels:  jest
Elasticsearch Jest Example
ElasticSearch Java Rest Client Examples
Stars: ✭ 189 (-3.57%)
Mutual labels:  jest
Mongoose Typescript Example
Stars: ✭ 156 (-20.41%)
Mutual labels:  jest
Chromogen
UI-driven Jest test-generation package for Recoil selectors
Stars: ✭ 164 (-16.33%)
Mutual labels:  jest
Redux Actions Assertions
Simplify testing of redux action and async action creators
Stars: ✭ 177 (-9.69%)
Mutual labels:  jest
Express Typescript Boilerplate
A delightful way to building a RESTful API with NodeJs & TypeScript by @w3tecch
Stars: ✭ 2,293 (+1069.9%)
Mutual labels:  jest
Nxplorerjs Microservice Starter
Node JS , Typescript , Express based reactive microservice starter project for REST and GraphQL APIs
Stars: ✭ 193 (-1.53%)
Mutual labels:  jest
2019 12
🎟 급증하는 트래픽에도 안정적인 예약 서비스, Atomic Pattern을 적용한 재사용 가능한 컴포넌트, 실용적인 Testing을 주제로 하는 이벤트 서비스
Stars: ✭ 169 (-13.78%)
Mutual labels:  jest
Blog Service
blog service @nestjs
Stars: ✭ 188 (-4.08%)
Mutual labels:  jest
Jest Runner Tsc
🃏A Jest runner for the TypeScript compiler
Stars: ✭ 158 (-19.39%)
Mutual labels:  jest
Jest Html Reporter
Jest test results processor for generating a summary in HTML
Stars: ✭ 161 (-17.86%)
Mutual labels:  jest
Vscode Jest
The optimal flow for Jest based testing in VS Code
Stars: ✭ 2,357 (+1102.55%)
Mutual labels:  jest
Express Webpack React Redux Typescript Boilerplate
🎉 A full-stack boilerplate that using express with webpack, react and typescirpt!
Stars: ✭ 156 (-20.41%)
Mutual labels:  jest
Quickshare
Quick and simple file sharing between different devices.
Stars: ✭ 190 (-3.06%)
Mutual labels:  jest
J
微信网页版API 微信桌面机器人
Stars: ✭ 155 (-20.92%)
Mutual labels:  jest
Storybook Addon Jest
REPO/PACKAGE MOVED - React storybook addon that show component jest report
Stars: ✭ 177 (-9.69%)
Mutual labels:  jest
100 Days Of Code Frontend
Curriculum for learning front-end development during #100DaysOfCode.
Stars: ✭ 2,419 (+1134.18%)
Mutual labels:  jest
Jest Canvas Mock
🌗 A module used to mock canvas in Jest.
Stars: ✭ 189 (-3.57%)
Mutual labels:  jest
Nestjs Template
Scaffold quickly your next TypeScript API with this opinionated NestJS template crafted for Docker environments
Stars: ✭ 183 (-6.63%)
Mutual labels:  jest

jest-native

eagle

Custom jest matchers to test the state of React Native.


Build Status Code Coverage version downloads MIT License

All Contributors PRs Welcome Code of Conduct Discord

Watch on GitHub Star on GitHub

Table of Contents

The problem

You want to use jest to write tests that assert various things about the state of a React Native app. As part of that goal, you want to avoid all the repetitive patterns that arise in doing so like checking for a native element's props, its text content, its styles, and more.

This solution

The jest-native library provides a set of custom jest matchers that you can use to extend jest. These will make your tests more declarative, clear to read and to maintain.

Compatibility

These matchers should, for the most part, be agnostic enough to work with any React Native testing utilities, but they are primarily intended to be used with RNTL. Any issues raised with existing matchers or any newly proposed matchers must be viewed through compatibility with that library and its guiding principles first.

Installation

This module should be installed as one of your project's devDependencies:

npm install --save-dev @testing-library/jest-native

You will need react-test-renderer, react, and react-native installed in order to use this package.

Usage

Import @testing-library/jest-native/extend-expect once (for instance in your tests setup file) and you're good to go:

import '@testing-library/jest-native/extend-expect';

Alternatively, you can selectively import only the matchers you intend to use, and extend jest's expect yourself:

import { toBeEmpty, toHaveTextContent } from '@testing-library/jest-native';

expect.extend({ toBeEmpty, toHaveTextContent });

Matchers

jest-native has only been tested to work with RNTL. Keep in mind that these queries will only work on UI elements that bridge to native.

toBeDisabled

toBeDisabled();

Check whether or not an element is disabled from a user perspective.

This matcher will check if the element or its parent has a disabled prop, or if it has `accessibilityState={{disabled: true]}.

It also works with accessibilityStates={['disabled']} for now. However, this prop is deprecated in React Native 0.62

Examples

const { getByTestId } = render(
  <View>
    <Button disabled testID="button" title="submit" onPress={(e) => e} />
    <TextInput accessibilityState={{ disabled: true }} testID="input" value="text" />
  </View>,
);

expect(getByTestId('button')).toBeDisabled();
expect(getByTestId('input')).toBeDisabled();

toBeEnabled

toBeEnabled();

Check whether or not an element is enabled from a user perspective.

Works similarly to expect().not.toBeDisabled().

Examples

const { getByTestId } = render(
  <View>
    <Button testID="button" title="submit" onPress={(e) => e} />
    <TextInput testID="input" value="text" />
  </View>,
);

expect(getByTestId('button')).toBeEnabled();
expect(getByTestId('input')).toBeEnabled();

toBeEmpty

toBeEmpty();

Check that the given element has no content.

Examples

const { getByTestId } = render(<View testID="empty" />);

expect(getByTestId('empty')).toBeEmpty();

toContainElement

toContainElement(element: ReactTestInstance | null);

Check if an element contains another element as a descendant. Again, will only work for native elements.

Examples

const { queryByTestId } = render(
  <View testID="grandparent">
    <View testID="parent">
      <View testID="child" />
    </View>
    <Text testID="text-element" />
  </View>,
);

const grandparent = queryByTestId('grandparent');
const parent = queryByTestId('parent');
const child = queryByTestId('child');
const textElement = queryByTestId('text-element');

expect(grandparent).toContainElement(parent);
expect(grandparent).toContainElement(child);
expect(grandparent).toContainElement(textElement);
expect(parent).toContainElement(child);
expect(parent).not.toContainElement(grandparent);

toHaveProp

toHaveProp(prop: string, value?: any);

Check that an element has a given prop.

You can optionally check that the attribute has a specific expected value.

Examples

const { queryByTestId } = render(
  <View>
    <Text allowFontScaling={false} testID="text">
      text
    </Text>
    <Button disabled testID="button" title="ok" />
  </View>,
);

expect(queryByTestId('button')).toHaveProp('accessibilityStates', ['disabled']);
expect(queryByTestId('button')).toHaveProp('accessible');
expect(queryByTestId('button')).not.toHaveProp('disabled');
expect(queryByTestId('button')).not.toHaveProp('title', 'ok');

toHaveTextContent

toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean });

Check if an element or its children have the supplied text.

This will perform a partial, case-sensitive match when a string match is provided. To perform a case-insensitive match, you can use a RegExp with the /i modifier.

To enforce matching the complete text content, pass a RegExp.

Examples

const { queryByTestId } = render(<Text testID="count-value">2</Text>);

expect(queryByTestId('count-value')).toHaveTextContent('2');
expect(queryByTestId('count-value')).toHaveTextContent(2);
expect(queryByTestId('count-value')).toHaveTextContent(/2/);
expect(queryByTestId('count-value')).not.toHaveTextContent('21');

toHaveStyle

toHaveStyle(style: object[] | object);

Check if an element has the supplied styles.

You can pass either an object of React Native style properties, or an array of objects with style properties. You cannot pass properties from a React Native stylesheet.

Examples

const styles = StyleSheet.create({ text: { fontSize: 16 } });

const { queryByText } = render(
  <Text
    style={[
      { color: 'black', fontWeight: '600', transform: [{ scale: 2 }, { rotate: '45deg' }] },
      styles.text,
    ]}
  >
    Hello World
  </Text>,
);

expect(queryByText('Hello World')).toHaveStyle({ color: 'black', fontWeight: '600', fontSize: 16 });
expect(queryByText('Hello World')).toHaveStyle({ color: 'black' });
expect(queryByText('Hello World')).toHaveStyle({ fontWeight: '600' });
expect(queryByText('Hello World')).toHaveStyle({ fontSize: 16 });
expect(queryByText('Hello World')).toHaveStyle({ transform: [{ scale: 2 }, { rotate: '45deg' }] });
expect(queryByText('Hello World')).toHaveStyle({ transform: [{ rotate: '45deg' }] });
expect(queryByText('Hello World')).toHaveStyle([{ color: 'black' }, { fontWeight: '600' }]);
expect(queryByText('Hello World')).not.toHaveStyle({ color: 'white' });

Inspiration

This library was made to be a companion for RNTL.

It was inspired by jest-dom, the companion library for DTL. We emulated as many of those helpers as we could while keeping in mind the guiding principles.

Other solutions

None known, you can add the first!

Contributors

Thanks goes to these wonderful people (emoji key):


Brandon Carroll

💻 📖 🚇 ⚠️

Santi

💻

Marnus Weststrate

💻

Matthieu Harlé

💻

Alvaro Catalina

💻

ilker Yılmaz

📖

Donovan Hiland

💻 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].