Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-native

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-native

Custom jest matchers to test the state of React Native

  • 1.2.0
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

jest-native

goat

Custom jest matchers to test the state of React Native.


Build Status Code Coverage version downloads MIT License PRs Welcome

Watch on GitHub Star on GitHub

The problem

You want to use jest to write tests that assert various things about the state of a React Native tree. 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.

Installation

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

npm install --save-dev jest-native

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

Usage

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

import '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 'jest-native';

expect.extend({ toBeEmpty, toHaveTextContent });

Matchers

jest-native has only been tested to work with native-testing-library. 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 accessibilityStates={['disabled']}.

toBeEnabled

toBeEnabled();

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

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

toBeEmpty

toBeEmpty();

Check that the given element has no content.

toContainElement(element)

toContainElement();

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

toHaveProp(prop, value)

toHaveProp(prop, value);

Check that an element has a given prop. Only works for native elements, so this is similar to checking for attributes in the DOM.

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

toHaveTextContent(text)

toHaveTextContent(text);

Check if an element has 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.

Todo list

  • toBeVisible() {?}
  • toHaveStyle(any) {?}

Inspiration

This library was made to be a companion for native-testing-library.

It was inspired by jest-dom, the companion library for dom-testing-library. 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!

Keywords

FAQs

Package last updated on 06 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc