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.

How to create a libdef for something that extend's Jest's 'expect' global?

See original GitHub issue

Jest provides expect.extend, which lets you add custom extensions to its expect global.

A library called expect-puppeteer uses this feature to add new assertions useful for integration testing with Puppeteer, like expect(page).toClick(...).

But this causes Flow to complain that .toClick is not a thing. Flow knows the return value of expect() is a JestExpectType object (because of the Jest libdef), which means you can do things like expect(x).toBeDefined() and Flow understands it. But any custom extensions don’t work.

So I want to write a new libdef for expect-puppeteer (or possibly its parent jest-puppeteer) that somehow augments the expect global that’s originally declared by the Jest libdef.

Is this possible? If the same global (expect) is declared in two different libdefs, is there some way to merge them? Or would I need to redeclare everything that already gets declared by the Jest libdef?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
callumlockecommented, Apr 14, 2018

The enzyme plugin does the same thing (augmenting the global expect), and I’ve just noticed something.

The enzyme type definitions are simply bundled into the Jest definition (along with those of another plugin I haven’t heard of called dom-testing-library).

Are flow-typed maintainers aware of this pattern? The downsides I can see are:

  • These plugin-added types will apply to anyone using Jest, even people who don’t use these plugins. So their Flow autocomplete will suggest methods they can’t use.
  • Two plugins might add expectation methods with the same name but clashing type signatures.
  • This pattern makes some plugins ‘first class’ without any clear criteria
0reactions
pascalduezcommented, Aug 15, 2019

Dupe of #948

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expect - Jest
declare global { namespace jest { interface Expect extends CustomMatchers {} interface Matchers<R> extends CustomMatchers<R> {}
Read more >
How to make Flow aware of Jest's globals (test, expect, etc)?
I've installed the Jest libdef from flow-typed but it doesn't make any difference. Update: it looks like the Jest libdef created by flow-typed ......
Read more >
Jest — How to Use Extend with TypeScript | by Moon
First things first, create the project folder where all related files will be. npm init -y. Install all the packages we need. npm...
Read more >
How To Improve Your Tests with Custom Matchers
Fortunately, test frameworks like Jest make it quite easy and ... Jest allows us to add your own matchers via its expect.extend method....
Read more >
Jest Unit Testing: Extending the framework - Software Wright
Therefore you want the expect function change to happen before the other packages attempt to make modifications to the global expect function. npm...
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