Skip to content

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

Closed
@glenjamin

Description

@glenjamin

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:

var ownerHasKeyUseWarning = {};

Could we provide a way to clear this state, or even better a supported API for opt-in erroring on warnings?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions