Add hooks into the warning output for unit tests and other use cases
See original GitHub issueI’d like to configure my testsuite to fail if there are any react warnings triggered.
I’m currently using a variant on this SO answer http://stackoverflow.com/questions/29651950/karma-and-react-have-warnings-to-cause-errors
console.warn = (function(warn) {
return function(msg) {
// Detect react warnings & error
if (/^Warning: /.test(msg)) {
throw new Error("React " + msg);
}
return warn.apply(this, arguments);
};
})(console.warn);
This basically works, but has a problem because there’s some global-state memoisation in the code which triggers warning to try not to trigger too often: https://github.com/facebook/react/blob/500d4c3f8779bce7c7dde129eb2fec9e901cf5ae/src/isomorphic/classic/element/ReactElementValidator.js#L46
Could we provide a way to clear this state, or even better a supported API for opt-in erroring on warnings?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:6
- Comments:26 (17 by maintainers)
Top Results From Across the Web
Testing custom react hooks that use fetch (or other async ...
I'm using useRef(true) to make sure it doesn't make a call on it's first render and useState to update the error/data state which...
Read more >How to test React Hooks - LogRocket Blog
The goal of this article is to provide a practical guide on testing React Hooks using tools such as React Testing Library, Jest,...
Read more >How to test custom React hooks - Kent C. Dodds
Get confidence your custom React hooks work properly with solid tests.
Read more >Prevent `act` warning on async hook · Issue #14 - GitHub
I changed asyncHook.test.js to my use case, add a test case illustrating this and skip other tests. This test case doesn't generate warning....
Read more >Advanced Specflow Shared & Scoped Bindings, Hooks and ...
Advanced Specflow Tutorial on Shared & Scoped Bindings, Hooks and Step Reuse: In this Complete Guide on Specflow Training, we had a look...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Never mind my last comment, it’s wrong.
checkPropTypes()
doesn’t return the failures (and it also deduplicates them). Maybe we should expose something there that doesn’t.I realize I’m replying to a comment that’s over a year old, but… 😄
I just did a quick test (below) and Dan’s hack-around does still seem to work so far as I can see, although I realize it’s far from ideal. For example:
The above test will result in the following warnings: