Chai: AssertionError: expected 'happy' to be an instance of String

Created on 8 Dec 2015  Â·  2Comments  Â·  Source: chaijs/chai

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?

Most helpful comment

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.

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zzzgit picture zzzgit  Â·  3Comments

liborbus picture liborbus  Â·  4Comments

domenic picture domenic  Â·  4Comments

basherr picture basherr  Â·  4Comments

andipavllo picture andipavllo  Â·  3Comments