Code Monkey home page Code Monkey logo

jest-prop-type-error's Introduction

jest-prop-type-error

Improves prop-types support in Jest by failing tests on prop-type errors instead of ignoring them

prop-types is a library from Facebook that makes it easy to verify that the props passed to a React component is of the correct type. However, it only allows us runtime type checking, which means some failures can slip by into production.

This is possible to solve with Jest by making prop-type errors throw instead of only logging a warning. In this case your tests would run and whenever a wrong prop is passed to a component, the prop-type validation would pick this up and fail the test. Then we would be able to verify that the props that are passed to our components are good compile-time, provided the tests run when we build our production bundle.

That is what this library does!

Read this blog for a more in-depth explanation.

Before

The test shows a warning and passes

After

The test shows an error and fails

Install

yarn add -D jest-prop-type-error

Usage

In order to enable us to detect prop-type errors and fail the test, you need to add the following to your package.json's jest section.

  "setupFiles": [
    "jest-prop-type-error"
  ]

If there is no jest section in your package.json, add one and use that.

Now simply run your tests like usual, and prop-type errors will throw instead of logging a warning!

Usage with Create React App

Create React App doesn't allow the use of the setupFiles directive. To use with create-react-app, simply require 'jest-prop-type-error' in your setupTests.js file after installing the package:

  import 'jest-prop-type-error'

Requirements

Requires node 6 or later

Acknowledgments

Thanks to Stian Didriksen for the idea in his talk about Jest at the ReactJS Oslo Meetup 9th May 2018.

jest-prop-type-error's People

Contributors

eliihen avatar ypcrumble avatar

Stargazers

Christian Elías avatar Thin White Duke avatar Roie Koper avatar  avatar Harutaka Kawamura avatar Ruan Mer avatar Noman Ali Khan avatar Jalal azimi avatar Juliano Penna avatar Alex Seitsinger avatar Glenn 'devalias' Grant avatar Broughton Software, LLC avatar Syver Enstad avatar Andreas avatar Tonye avatar Xiaoyu Zhang avatar Marty Powell avatar Aleksei Chernov avatar Michał Świtoń avatar Charlike Mike Reagent avatar Jakub Żywiec avatar Matheus Marabesi avatar Alexandre Mouriec avatar larabuli avatar Brian Sayler avatar Pedro avatar Jerome Carty avatar Eric Josue avatar Andris Sīlis avatar Reinis Ivanovs avatar  avatar Renan Garcia Borges avatar John Hunter avatar Leif Olsen avatar Tom Adler avatar Richard Kemp avatar  avatar  avatar Mojtaba Darvishi avatar Alessandro avatar Cody Olsen avatar

Watchers

James Cloos avatar  avatar  avatar

jest-prop-type-error's Issues

UnhandledPromiseRejectionWarning but no test failure on async prop type violations

Describe the bug
Without jest-prop-type-error:

yarn run jest path/to/my/test.jsx
…
  console.error node_modules/prop-types/checkPropTypes.js:20
    Warning: Failed prop type: The prop `group` is marked as required in `Form`, but its value is `undefined`.
        in Form (at app.jsx:79)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total

With jest-prop-type-error:

yarn run jest path/to/my/test.jsx
…
(node:18295) UnhandledPromiseRejectionWarning: Error: Warning: Failed prop type: The prop `group` is marked as required in `Form`, but its value is `undefined`.
    in Form (at app.jsx:79)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total

To Reproduce
The component being tested is relatively normal, though it has this:

state = {
  isLoading: true
}

componentDidMount() {
  Promise.all([]).finally(() => this.setState({ isLoading: false }))
}

render() {
  return <div>
    {this.state.isLoading ? <div>Loading…</div> : <Form group={this.state.group} />}
  </div>
}

The prop-types violation occurs on <Form> when isLoading becomes false.

Expected behavior
With jest-prop-type-error the test suite should fail.

Actual behavior
I see a relevant message about UnhandledPromiseRejectionWarning but the tests pass.

Versions:

  • prop-types: 15.7.2
  • React: 16.8.5
  • Jest: 24.5.0
  • Node: 10.6.0

The test suite passes even if a prop types error is present

Describe the bug
The test suite passes even if a prop type error is present.

To Reproduce
This is my setup files

const error = console.error;

console.error = (message, ...args) => {
  if (/(Invalid prop|Failed prop type)/gi.test(message)) {
    throw new Error(message);
  }

  error.apply(console, [message, ...args]);
};

Expected behavior
The test suite doesn't pass

Actual behavior
The test suite passes and an error is displayed in the console

Screenshots
image

Versions:

  • prop-types: 15.7.2
  • React: 17.0.1
  • Jest: 26.6.3
  • Node: 14.15.3

error highlighting

Is it possible to make a warning output in the same color as the original console.error?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.