Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toBeInstanceOf(String) not work #3257

Closed
xareelee opened this issue Apr 5, 2017 · 7 comments
Closed

toBeInstanceOf(String) not work #3257

xareelee opened this issue Apr 5, 2017 · 7 comments

Comments

@xareelee
Copy link

xareelee commented Apr 5, 2017

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?

@thymikee
Copy link
Collaborator

thymikee commented Apr 5, 2017

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

You can test types like this:

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

@thymikee thymikee closed this as completed Apr 5, 2017
@eedrah
Copy link

eedrah commented Jul 18, 2018

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

@SimenB
Copy link
Member

SimenB commented Jul 18, 2018

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

@DrSensor
Copy link

DrSensor commented Sep 17, 2018

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();
                                   ~~~~~~~~~~

😞

@SimenB
Copy link
Member

SimenB commented Sep 17, 2018

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 🙂

@dadiorchen
Copy link

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

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants