Search code examples
reactjsmocha.jschaireactjs-testutils

ReactTestUtils findRenderedComponentWithType throwing error not detecting by chai?


Im using findRenderedComponentWithType to make sure there is a error, and Im using chai's assert.throws, but it is not working.


first of all:

TestUtils.findRenderedComponentWithType documentation:

expects there to be one result and returns that one result, or throws exception if there is any other number of matches besides one.


When I use the function, Im getting an error (as expected and correct). However I cant seems to assert it properly with chai: I tried assert.throws(TestUtils.findRenderedComponentWithType(element, component), /(Error)/). But it is saying the test failed, even though I'm getting an error:

 Error: Did not find exactly one match for componentType:function (props, context, updater) {
 [...]
 }

Solution

  • Check the signature of throws, it expects a function, not an error/object (which is the result of findRenderedComponentWithType. http://chaijs.com/api/assert/#method_throws

    So you will want to do something like

    cons fn = () => TestUtils.findRenderedComponentWithType(element, component)
    assert.throws(fn)