npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

riteway-jest

v2.0.2

Published

Unit tests that always supply a good bug report when they fail for Jest.

Downloads

677

Readme

RITEway-Jest

Inspired by Eric Elliott's RITEway.

Why?

TLDR: I wanted RITEway's assert for Jest.

I love RITEway's API because it forces you to write good unit tests by it's given-should API and only exposing the equals assertion.

Only problem is RITEway is build using tape. You can't use it with Jest, which in turn has some advantages and disadvantages.

Disadvantages

Advantages

You might want to check out RITEway because you can learn these advantages first hand. I prefer RITEway for React apps and use RITEway-Jest for React Native apps.

Installation

npm i --save-dev riteway-jest

or

yarn add --dev riteway-jest

Then import it in your src/setupTests.js for React with CRA.

import 'riteway-jest/src/riteway-jest.js';

For React Native you need to add a key in your package.json to the jest key.

"jest": {
  "preset": "react-native",
  "setupFilesAfterEnv": ["node_modules/riteway-jest/src/riteway-jest.js"]
}

If you have a jest.config.js.

module.exports = {
  setupFilesAfterEnv: [
    'node_modules/riteway-jest/src/riteway-jest.js',
    // ... other setup files ...
  ],
  // ... other options ...
};

If ESLint yells at you, add a global key to your .eslintrc.json.

{
  "_comment": "<Your other settings here>",
  "globals": {
    "assert": true
  },
  "rules": {
    "_comment": "<Your rules here>"
  }
}

Usage

With pure functions.

const sum = (a = 0, b = 0) => a + b;

describe('sum()', () => {
  const should = 'return the correct sum';

  assert({
    given: 'no arguments',
    should: 'return 0',
    actual: sum(),
    expected: 0,
  });

  assert({
    given: 'zero',
    should,
    actual: sum(2, 0),
    expected: 2,
  });

  assert({
    given: 'negative numbers',
    should,
    actual: sum(1, -4),
    expected: -3,
  });
});

Using React Native Testing Library.

import React from 'react';
import { Text } from 'react-native';
import { render } from 'react-native-testing-library';

function MyText({ title }) {
  return <Text>{title}</Text>;
}

describe('Text component', () => {
  const createText = (props = {}) => render(<MyText {...props} />);

  {
    const props = { title: 'Foo' };
    const $ = createText(props).getByType;

    assert({
      given: 'a title',
      should: 'display the title',
      actual: $('Text').props.children,
      expected: props.title,
    });
  }
});

Using React Testing Library.

import PropTypes from 'prop-types';
import React from 'react';
import { cleanup, render } from 'react-testing-library';

function Button({ disabled, onClick, text }) {
  return (
    <button data-testid="foo" disabled={disabled} onClick={onClick}>
      {text}
    </button>
  );
}

Button.propTypes = {
  disabled: PropTypes.bool.isRequired,
  onClick: PropTypes.func.isRequired,
  text: PropTypes.string,
};

Button.defaultProps = {
  disabled: false,
  text: '',
};

describe('button component', () => {
  const createButton = (props = {}) =>
    render(<Button onClick={() => {}} {...props} />);

  {
    const props = { text: 'foo' };
    const $ = createButton(props).getByTestId;

    assert({
      given: 'a text',
      should: "render 'foo'",
      actual: $('foo').textContent,
      expected: props.text,
    });
    cleanup();
  }

  {
    const props = { disabled: true, text: 'foo' };
    const $ = createButton(props).getByText;

    assert({
      given: 'disabled',
      should: 'be disabled',
      actual: $('foo').hasAttribute('disabled'),
      expected: props.disabled,
    });
    cleanup();
  }
});

skip & only & todo

assert supports Jest's skip, only and todo functions.

// This test is explicitly skipped
assert.skip({
  given: 'something',
  should: 'be equal to something',
  actual: 'nothing',
  expected: 'something',
});

// This test gets executed
assert.only({
  given: 'something',
  should: 'be equal to something',
  actual: 'nothing',
  expected: 'something',
});

// This test is implicitly skipped because the .only() above
assert({
  given: 'something',
  should: 'be equal to something',
  actual: 'nothing',
  expected: 'something',
});

each is currently not supported.