├── assets ├── proptype-test-error.png └── proptype-test-warning.png ├── index.js ├── package.json ├── .github └── ISSUE_TEMPLATE │ └── Bug_report.md ├── CHANGELOG.md ├── LICENSE ├── README.md └── CODE_OF_CONDUCT.md /assets/proptype-test-error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eliihen/jest-prop-type-error/HEAD/assets/proptype-test-error.png -------------------------------------------------------------------------------- /assets/proptype-test-warning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eliihen/jest-prop-type-error/HEAD/assets/proptype-test-warning.png -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const error = console.error; 2 | 3 | console.error = (message, ...args) => { 4 | if (/(Invalid prop|Failed prop type)/gi.test(message)) { 5 | throw new Error(message); 6 | } 7 | 8 | error.apply(console, [message, ...args]); 9 | }; 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-prop-type-error", 3 | "description": "Makes Jest throw on React prop-type failures", 4 | "version": "1.1.0", 5 | "main": "index.js", 6 | "author": "Espen Henriksen", 7 | "license": "MIT", 8 | "homepage": "https://github.com/esphen/jest-prop-type-error", 9 | "repository": "esphen/jest-prop-type-error", 10 | "bugs": { 11 | "url": "https://github.com/esphen/jest-prop-type-error/issues" 12 | }, 13 | "keywords": [ 14 | "jest", 15 | "react", 16 | "test", 17 | "testing", 18 | "prop-type", 19 | "prop-types", 20 | "error", 21 | "fail", 22 | "throw" 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | **Describe the bug** 8 | A clear and concise description of what the bug is. 9 | 10 | **To Reproduce** 11 | Describe some way to reproduce the problem, preferably with a link to a GitHub repo which displays the problem on an `npm test`. 12 | 13 | **Expected behavior** 14 | A clear and concise description of what you expected to happen. 15 | 16 | **Actual behavior** 17 | A clear and concise description of what actually happens. 18 | 19 | **Screenshots** 20 | If applicable, add screenshots to help explain your problem. 21 | 22 | **Versions:** 23 | - prop-types: [e.g. 15.6.1] 24 | - React: [e.g. 16.3.2] 25 | - Jest: [e.g. 22.4.3] 26 | - Node: [e.g. v8.11.0] 27 | 28 | **Additional context** 29 | Add any other context about the problem here. 30 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [Unreleased] 8 | 9 | ## [1.1.0] - 2018-05-11 10 | ## Added 11 | - Support for "Failed prop type" in addition to legacy "Invalid prop" 12 | - Support for multiple arguments to console.error 13 | - MIT license file 14 | - Contributor Covenant Code of Conduct 15 | - Changelog 16 | 17 | ## [1.0.0] - 2018-05-10 18 | ### Added 19 | - The initial release 20 | 21 | [Unreleased]: https://github.com/esphen/jest-prop-type-error/compare/v1.1.0...HEAD 22 | [1.1.0]: https://github.com/esphen/jest-prop-type-error/compare/v1.0.0...v1.1.0 23 | [1.0.0]: https://github.com/esphen/jest-prop-type-error/commit/967cdc36cd9a1d64f2c887bc52b48dd4b87c2ea9 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Espen Henriksen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jest-prop-type-error 2 | 3 | > Improves prop-types support in Jest by failing tests on prop-type errors 4 | > instead of ignoring them 5 | 6 | [prop-types][pt] is a library from Facebook that makes it easy to verify that 7 | the props passed to a [React][react] component is of the correct type. However, 8 | it only allows us runtime type checking, which means some failures can slip by 9 | into production. 10 | 11 | This is possible to solve with [Jest][jest] by making prop-type errors throw 12 | instead of only logging a warning. In this case your tests would run and 13 | whenever a wrong prop is passed to a component, the prop-type validation would 14 | pick this up and fail the test. Then we would be able to verify that the props 15 | that are passed to our components are good compile-time, provided the tests 16 | run when we build our production bundle. 17 | 18 | That is what this library does! 19 | 20 | [Read this blog][blog] for a more in-depth explanation. 21 | 22 | ## Before 23 | ![The test shows a warning and passes](assets/proptype-test-warning.png) 24 | 25 | ## After 26 | ![The test shows an error and fails](assets/proptype-test-error.png) 27 | 28 | # Install 29 | 30 | yarn add -D jest-prop-type-error 31 | 32 | # Usage 33 | 34 | In order to enable us to detect prop-type errors and fail the test, you need to 35 | add the following to your `package.json`'s jest section. 36 | 37 | "setupFiles": [ 38 | "jest-prop-type-error" 39 | ] 40 | 41 | If there is no jest section in your `package.json`, add one and use that. 42 | 43 | Now simply run your tests like usual, and prop-type errors will throw instead 44 | of logging a warning! 45 | 46 | # Usage with Create React App 47 | 48 | Create React App doesn't allow the use of the `setupFiles` directive. To use with 49 | create-react-app, simply require 'jest-prop-type-error' in your `setupTests.js` file 50 | after installing the package: 51 | 52 | import 'jest-prop-type-error' 53 | 54 | # Requirements 55 | 56 | Requires node 6 or later 57 | 58 | # Acknowledgments 59 | 60 | Thanks to [Stian Didriksen][stian] for the idea in his talk about Jest at the 61 | ReactJS Oslo Meetup 9th May 2018. 62 | 63 | [pt]: https://facebook.github.io/react/docs/typechecking-with-proptypes.html 64 | [blog]: https://medium.com/shark-bytes/type-checking-with-prop-types-in-jest-e0cd0dc92d5 65 | [react]: https://reactjs.org/ 66 | [jest]: https://facebook.github.io/jest/ 67 | [stian]: https://twitter.com/stipsan 68 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at dev+githubcoc@henriksen.is. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | --------------------------------------------------------------------------------