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

Make fail() to break the typescript's control flow #312

Merged
merged 1 commit into from
Oct 11, 2021

Conversation

xamgore
Copy link
Contributor

@xamgore xamgore commented Apr 14, 2021

Imagine we have a code like this. If expect.fail() returns R, we won't be able to call val.slice as val may be null, thus we get a compiler error.

    test('example', async () => {
      const val = foo() // string | null
      if (!val) expect.fail('string expected, got null')
      
      val.slice(5) // some operations with the string
      // ...
    })

Though, if we change expect.fail() return type to never, typescript will start understanding the further execution won't be possible with val === null, so the val.slice is exactly string::slice call. And there won't be any compilation errors.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@xamgore
Copy link
Contributor Author

xamgore commented Apr 15, 2021

@mattphillips are you ok with this?

@WORMSS
Copy link

WORMSS commented Jun 28, 2021

would you need not.pass() also?

@WORMSS
Copy link

WORMSS commented Jun 28, 2021

I thought this sounded familiar

#204

#204 (comment)

https://github.com/keeganwitt/jest-extended/pull/26

Seems fail is currently a no-op according to the code?

@xamgore
Copy link
Contributor Author

xamgore commented Jun 28, 2021

would you need not.pass() also?

I would consider only fail to increase the probability of this PR getting merged.

@SimenB
Copy link
Member

SimenB commented Oct 6, 2021

Hmm, this isn't implemented according to the docs. Should we just remove it from the types?

https://github.com/jest-community/jest-extended#failmessage

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

Landed #204, so this makes sense now. Thanks!

@SimenB SimenB merged commit c4961cd into jest-community:main Oct 11, 2021
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

3 participants