question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

[Feature]: Replace `@types/jest`'s `expect` implementation with `@jest/expect`

See original GitHub issue

šŸš€ Feature Proposal

People might not want to use the globals (expect, test etc.) Jest provides (either because of ESM or just a preference). And because Jest is written in TypeScript, when doing e.g. import {expect} from '@jest/globals' all built in matchers are typed. However, if you use some matchers from the community (such as jest-extended), they augment the matchers from @types/jest instead of expect (or @jest/expect). If instead all libraries augmented the actual expect types, the imported matcher would be typed correctly.

However, this flips the problem - now people using the globals no longer get custom matchers typed! The solution to this problem is to make @jest/types use @jest/expect instead of shipping its own types for this.

This issue is for tracking the work that needs to be done before releasing Jest 28 stable so thereā€™s a single source of truth - the source code.


I have started: https://github.com/SimenB/DefinitelyTyped/tree/jest-expect

We need to make sure the tests pass. Most breakage is due to the tests using e.g. jest.InverseMatchers etc - I donā€™t think thatā€™s super useful? I might be wrong though, in which case we should expose more of those types from expect and/or @jest/expect.

~(note that running tests is ā€œimpossibleā€ until https://github.com/microsoft/DefinitelyTyped-tools/pull/411 is merged. I ran npm install manually inside types/jest to see errors in the tests in my IDE)~

/cc @mrazauskas

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Oct 26, 2022

If this issue is resolved, yes šŸ™‚ As of now @types/jest doesnā€™t get its matchers from expect

1reaction
SimenBcommented, Oct 24, 2022

For anyone following along, weā€™ve replace our own usage of @types/jest with the types exported from @jest/globals and an extra package sticking Jestā€™s globals into TSā€™s globals: #13344.

Iā€™d still love for @types/jest to work with @jest/expect, but that might just be https://github.com/DefinitelyTyped/DefinitelyTyped/pull/62037 or something like it instead of having steps on the way

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expect - Jest
The expect function is used every time you want to test a value. ... We are going to implement a matcher called toBeDivisibleByExternalValueĀ ......
Read more >
How to expect (jest) a function to be called with an object that ...
mockImplementation () ... first make sure Message is a string expect(PublishCommand). ... import {expect} from '@jest/globals'; function ...
Read more >
Jest: write your own ā€œitā€ and ā€œexpectā€ | by Matt Holland - Medium
Given our above test block example, how would we write our own simple expect() function to replace Jest's? Let's do it! Custom implementation...
Read more >
API Reference | Vitest
It receives the test name, an async function with the tests to collect, ... This function is compatible with Jest's expect.extend , so...
Read more >
Mocking with Jest: Spying on Functions and Changing ...
Spying on Functions and Changing Implementation ... Jest has lots of mocking features. ... callFnWithTheMeaningOfLife(fn) expect(fn).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found