it('instanceof', function(){
var foo = 'happy';
expect(foo).to.be.instanceof(String);
})
when i run the code above, the below err show.
AssertionError: expected 'happy' to be an instance of String
‘happy’ is not an instance of a String?
Hey @lizhengnacl thanks for the issue.
This is actually JavaScript's behaviour. instanceof does not work for primitive values:
'happy' instanceof String
// => false
true instanceof Boolean
// => false
1 instanceof Number
// => false
Read more about the instanceof operator on mdn.
Our to.be.instanceof assertion just calls instanceof, and so behaves the same way.
You probably want to assert using our to.be.a/to.be.an assertion. For example:
// This one passes fine
it('instanceof', function(){
var foo = 'happy';
expect(foo).to.be.a('string');
})
Because of the above, I'm going to close this issue. Feel free to continue discussing it though.
@keithamus
i know it now, thanks for your answer
Most helpful comment
Hey @lizhengnacl thanks for the issue.
This is actually JavaScript's behaviour.
instanceofdoes not work for primitive values:Read more about the instanceof operator on mdn.
Our
to.be.instanceofassertion just calls instanceof, and so behaves the same way.You probably want to assert using our
to.be.a/to.be.anassertion. For example:Because of the above, I'm going to close this issue. Feel free to continue discussing it though.