0

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) {
 [...]
 }
user308553
  • 1,238
  • 4
  • 18
  • 32

1 Answers1

1

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)
ken4z
  • 1,340
  • 1
  • 11
  • 18
  • first of all it works thanks. I actually read that before too, but I didn't realize method and function is different by definition, but then I still dont really see how this made a diff. Doesnt chai just run whatever in the first parameter then check what it output? does it have something to do with callback? – user308553 May 09 '16 at 21:38
  • 1
    Your initial implementation wasn't passing in a function (or method). It has do with the order things are executed in. The `TestUtils.findRenderedComponentWithType(element, component)` gets executed first and returns either a Component object or throws an error. So really you are passing in one of those types. Think of how it might be implemented. It takes a function, then wraps it in a try/catch block THEN executes it. Whereas you were executing it THEN behind the scenes it was getting wrapped, but by then it is too late; the error has been thrown – ken4z May 10 '16 at 13:51
  • 1
    As a side note, you might want to check out https://github.com/airbnb/enzyme. I found the ReactTestUtils lib to be a bit of a pain to work with (especially those method names) so ended up writing my own before Enzyme became available. – ken4z May 10 '16 at 14:01
  • oh yeah, wow that's embarassing, thanks. I've been seeing people talking about enzyme too, I'll take a look thanks! – user308553 May 10 '16 at 14:52