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() + ']';
}
@prantlf
Copy link

prantlf commented Jan 26, 2014

Thanks for the idea! Shouldn't the space in notText be on the other side to separate the actual value and not do double the space in front of the following text at the line 9? i fixed it im my fork and added toBeInstanceOf to check other than primitive types.

@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