juliemr / minijasminenode

A very bare-bones version of jasmine-node
MIT License
32 stars 21 forks source link

Better error message when matching null/undefined with toContain. #28

Open rehmsen opened 9 years ago

rehmsen commented 9 years ago

I was recently thrown off by the error message

minijasminenode/lib/jasmine-1.3.1.js:1142
  return haystack.indexOf(needle) >= 0;
                  ^
TypeError: Cannot call method 'indexOf' of null

Since this happened when running a pretty large test suite, I was not immediately sure what had happened, nor which test case had actually failed. After some debugging it turned out that I had called expect(foo).toContain(bar), where foo was null. This pull request provides a better error message in this case:

Failures:

  1) toContain should print helpful error message when matching null
   Message:
     Expected null to contain 'needle'.
   Stacktrace:
     Error: Expected null to contain 'needle'.
    at null.<anonymous> (.../minijasminenode/spec/tocontain_spec.js:3:18)

I am not sure this is the correct repo to fix this in. I also did not find any unit tests for the matchers, so I was not sure if I should add one for this change. Please advice.