Skip to content

Instantly share code, notes, and snippets.

@adamyanalunas
Created October 21, 2012 01:14
Show Gist options
  • Save adamyanalunas/3925377 to your computer and use it in GitHub Desktop.
Save adamyanalunas/3925377 to your computer and use it in GitHub Desktop.
jasmine.js matcher to test type of object
jasmine.Matchers.prototype.toBeTypeOf = function(expected) {
var actual, notText, objType;
actual = this.actual;
notText = this.isNot ? 'not ' : '';
objType = actual ? Object.prototype.toString.call(actual) : '';
this.message = function() {
return 'Expected ' + actual + notText + ' to be an array';
}
return objType.toLowerCase() === '[object ' + expected.toLowerCase() + ']';
}
@FilipZawada
Copy link

Worth noting, that in Jasmine 2.0 you can use jasmine.any() matcher:

expect(12).toEqual(jasmine.any(Number))

as mentioned here

@wintondeshong
Copy link

+1 for @FilipZawada's suggestion of jasmine#any

@mattixpet
Copy link

I like the idea.

Saves you the trouble of writing e.g. expect(typeof(var)).toEqual('number')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment