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?
'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(/.*/)
Most helpful comment
'hello'
is not an instance of String.new String('hello')
is.You can test types like this: