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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add typeof assertion #762

Merged
merged 4 commits into from Feb 16, 2022
Merged

Conversation

Shinigami92
Copy link
Contributor

@Shinigami92 Shinigami92 commented Feb 15, 2022

This adds a new assertion that replaces expect(typeof actual).toBe('string') with expect(actual).toBeTypeOf('string')

Please let me know where I need to add docs and/or tests, I'm very unfamiliar with the structure of the repo 馃槄

Edit:
Somehow there is a chai typeOf https://www.chaijs.com/api/assert/#method_typeof
But not sure how or if this is in any way accessible from vitest

@netlify
Copy link

netlify bot commented Feb 15, 2022

鉁旓笍 Deploy Preview for vitest-dev ready!

馃敤 Explore the source changes: f6a09f1

馃攳 Inspect the deploy log: https://app.netlify.com/sites/vitest-dev/deploys/620c0c5ce4c3060008576559

馃槑 Browse the preview: https://deploy-preview-762--vitest-dev.netlify.app

Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would need to add docs to api/index.md i think
And test should be somewere in the test/core/test 馃憖

packages/vitest/src/integrations/chai/jest-expect.ts Outdated Show resolved Hide resolved
@sheremet-va
Copy link
Member

sheremet-va commented Feb 15, 2022

Somehow there is a chai typeOf https://www.chaijs.com/api/assert/#method_typeof

I think it is accesible with:

import { assert } from 'chai'
assert.typeOf()

Co-authored-by: Vladimir <sheremet-va@users.noreply.github.com>
@Shinigami92 Shinigami92 marked this pull request as draft February 15, 2022 20:07
@Shinigami92
Copy link
Contributor Author

I think it is accesible with:

import { assert } from 'chai'
assert.typeOf()

Tested this, but it's not so accurate and the negation with .not.toBeTypeOf shows the wrong message.
So I will leave it as I implemented it 馃檪

@Shinigami92 Shinigami92 marked this pull request as ready for review February 15, 2022 20:26
@Shinigami92
Copy link
Contributor Author

@sheremet-va Ready for a second review 馃殌
Thanks for your review

Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thank you for PR!

@patak-dev
Copy link
Member

patak-dev commented Feb 15, 2022

Nice! About the name, shouldn't it be toBeOfType instead of toBeTypeOf

expect(actual).toBeOfType('string')

because it is

typeof actual === 'string'

So, the type of actual is string
And we want to say now that we expect actual to be of type string

It is different than for toBeInstanceOf, as with it the construct is actual instanceof expected. So the API for it in Jest sounds right.

@Shinigami92
Copy link
Contributor Author

Nice! About the name, shouldn't it be toBeOfType instead of toBeTypeOf

expect(actual).toBeOfType('string')

because it is

typeof actual === 'string'

So, the type of actual is string And we want to say now that we expect actual to be of type string

It is different than for toBeInstanceOf, as with it the construct is actual instanceof expected. So the API for it in Jest sounds right.

Yes and no, cause there is the real typeof actual === 'string', it's named after this typeof where folks are familiar with

@patak-dev
Copy link
Member

I think that every other API in expect reads well. IMO, even if some users may initially reach out toBeTypeOf('string'), the correct naming here is toBeOfType('string') and with TS and autocomplete people shouldn't have issues finding/using it. @sheremet-va ?

@Shinigami92
Copy link
Contributor Author

I think that every other API in expect reads well. IMO, even if some users may initially reach out toBeTypeOf('string'), the correct naming here is toBeOfType('string') and with TS and autocomplete people shouldn't have issues finding/using it. @sheremet-va ?

Would you be at least satisfied with an alias?
I also already thought about aliases like typeOf and notTypeOf and such.
Like currently you can use e.g. expect(...).toMatch() or expect(...).match()

@patak-dev
Copy link
Member

For reference, regarding the naming in chai, there it is

assert.typeOf('tea', 'string', 'we have a string');

but that reads well: type of 'tea' is 'string'

@sheremet-va
Copy link
Member

sheremet-va commented Feb 16, 2022

I think that every other API in expect reads well. IMO, even if some users may initially reach out toBeTypeOf('string'), the correct naming here is toBeOfType('string') and with TS and autocomplete people shouldn't have issues finding/using it. @sheremet-va ?

I like the current version because it is consistent with toBeInstanceOf

@patak-dev
Copy link
Member

patak-dev commented Feb 16, 2022

I like the current version because it is consistent with toBeInctanceOf

But it isnt consistent to me 馃
It is different than for toBeInstanceOf, as with it the construct is actual instanceof expected. So the API for it in Jest sounds right.

typeof actual === expected
actual instanceof expected

the of refers to actual in the first, and to expected in the second

Grammarly complains for "Expect something to be type of string". And the casing, TypeOf instead of typeof also affects here.

Imagine that instanceof was called classof and used in JS as classof actual === expected. Should the method be called expect(actual).toBeClassOf(expected) then? Here it is easier to see why that wouldnt work and the method should be toBeOfClass or toBeInstanceOf

@Aslemammad
Copy link
Member

toBeTypeOf is more accessible, but toBeOfType suits well here, and I guess it can be easily found with TS! so no problem on that side, and even if some users have trouble with that naming, we can mention it in the docs why we prefer toBeOfType!

@antfu
Copy link
Member

antfu commented Feb 16, 2022

I'd vote for toBeTypeOf as it matches the wording order of the keyword typeof, which to me will be more intuitive and easier to find.

@antfu antfu merged commit 5e2324f into vitest-dev:main Feb 16, 2022
@Shinigami92 Shinigami92 deleted the feat-assert-typeof branch February 16, 2022 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants