FIX : "this" refers to "window" object instead of current class during test (which causes errors)

16 views
Skip to first unread message

Kevin Wilson

unread,
May 6, 2016, 5:35:25 PM5/6/16
to Jasmine Dev
Hello everyone,

I've been using Jasmine 2.4.1 and noticed that if you are trying to test Javascript class methods that use the "this" keyword to indicate the context is the class, during Jasmine tests, the "this" keyword is set to the global "window" object... which causes unexpected behavior, and errors, and tests incorrectly fail.

I've fixed this by allowing the "this" context to be set via the optional 2nd parameter of the "expect" call during the test setup (args can optionally be passed as the 3rd parameter of the "expect" call).  If it's not specified, "expect" behaves as before.

I've made sure that this new version passes all of the existing unit tests for v2.4.1, so this is a non-breaking change that maintains existing functionality while adding new functionality (which was required in my case).  I've attached the updated JS file.  If you'd like to add it to your source code to help your user base, go for it.  If not, I'll maintain a copy for my use in the future.

Oh, I've also attached an updated CSS file so the output looks better / cleaner / easier to read.  Feel free to use it... or not.

Thanks for the very useful library!


~ Cheers

Kevin Wilson

jasmine.js.txt
jasmine.css.txt
Reply all
Reply to author
Forward
0 new messages