specit icon indicating copy to clipboard operation
specit copied to clipboard

Asserts on "undefined" always fail due to JSON.stringify(window)

Open niallsmart opened this issue 13 years ago • 1 comments

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.

niallsmart avatar Jun 14 '11 21:06 niallsmart

Also noticed this affects asserts on null; used a wrapper class instead of a singleton in final fix above.

niallsmart avatar Jun 14 '11 22:06 niallsmart