Jest: `toBeInstanceOf(String)` not work

Created on 5 Apr 2017  路  6Comments  路  Source: facebook/jest

The following expectation doesn't work as expected.

expect('hello').toBeInstanceOf(String);

Test output:

    expect(value).toBeInstanceOf(constructor)

    Expected value to be an instance of:
      "String"
    Received:
      "hello"
    Constructor:
      "String"

I did not find any docs or issue about this. How do I test whether a value is a String type?

Most helpful comment

'hello' is not an instance of String. new String('hello') is.

You can test types like this:

expect(typeof expected).toBe('string')

All 6 comments

'hello' is not an instance of String. new String('hello') is.

You can test types like this:

expect(typeof expected).toBe('string')

Would anyone be interested in having a discussion around this? It seems like a bit of a trip up that this doesn't work when using toBeInstanceOf. However, I understand the reasoning, if the implementation is using instanceof internally.

Perhaps a way forward is implementing other functions for the primative types, such as toBeAString and toBeANumber, and similar for Undefined, Null, Boolean

@eedrah I'd recommend taking a look at https://github.com/jest-community/jest-extended

FYI: jest-extended typescript declaration doesn't work when executing

$ tsc --pretty -p . --noemit
test/edge.test.ts:6:34 - error TS2339: Property 'toBeString' does not exist on type 'Matchers<any>'.

6         expect(bundled_module()).toBeString();
                                   ~~~~~~~~~~

:disappointed:

You can open up an issue in that repo. I can see the type definition there, so it'll probably be fixed if there's an error 馃檪

How about this: expect("").toMatch(/.*/)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Secretmapper picture Secretmapper  路  3Comments

samzhang111 picture samzhang111  路  3Comments

withinboredom picture withinboredom  路  3Comments

stephenlautier picture stephenlautier  路  3Comments

rosiakr picture rosiakr  路  3Comments