specit
specit copied to clipboard
Asserts on "undefined" always fail due to JSON.stringify(window)
Either of the following assertions throws a TypeError due to SpecIt calling JSON.stringify(window)
:
assert(undefined).shouldNot(be);
assert(undefined).should(eql, undefined);
The problem originates in objectToSpecIt()
which passes expectation
as the first argument to Function.apply
– however when expectation
is undefined
, then this
is the global object in the called function (as expected: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply). In a browser the global context is window
, so later on SpecIt calls JSON.stringify(actual)
when actual === window
.
I've implemented a kludgy workaround, which specially maps undefined
to a UndefinedWrapper
singleton.
Also noticed this affects asserts on null
; used a wrapper class instead of a singleton in final fix above.