question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add hooks into the warning output for unit tests and other use cases

See original GitHub issue

I’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:closed
  • Created 8 years ago
  • Reactions:6
  • Comments:26 (17 by maintainers)

github_iconTop GitHub Comments

4reactions
gaearoncommented, Jan 2, 2018

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.

4reactions
bvaughncommented, Oct 11, 2017

I realize I’m replying to a comment that’s over a year old, but… 😄

Does anybody know of a workaround to get the --watch scenario mentioned by @scottnonnenberg working? Unfortunately @gaearon’s workaround is not applicable as the warnings are still only triggered once.

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:

import PropTypes from 'prop-types';
import React from 'react';
import ReactDOM from 'react-dom';

const CustomComponent = () => null;
CustomComponent.propTypes = {
  requiredBool: PropTypes.bool.isRequired
};

let counter = 0;

beforeEach(() => {
  CustomComponent.displayName = `CustomComponent-${++counter}`
})

it('test 1', () => {
  const div = document.createElement('div');
  ReactDOM.render(<CustomComponent />, div);
});

it('test 2', () => {
  const div = document.createElement('div');
  ReactDOM.render(<CustomComponent />, div);
});

The above test will result in the following warnings:

  console.error node_modules/fbjs/lib/warning.js:33
    Warning: Failed prop type: The prop `requiredBool` is marked as required in `CustomComponent-1`, but its value is `undefined`.
        in CustomComponent-1 (at App.test.js:20)

  console.error node_modules/fbjs/lib/warning.js:33
    Warning: Failed prop type: The prop `requiredBool` is marked as required in `CustomComponent-2`, but its value is `undefined`.
        in CustomComponent-2 (at App.test.js:25)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found