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

expect.extend: not possible to correctly extend corresponding TypeScript types #11487

Closed
NaridaL opened this issue May 30, 2021 · 4 comments
Closed

Comments

@NaridaL
Copy link

NaridaL commented May 30, 2021

🐛 Bug Report

When extending expect

import expect from "expect"
expect.extend({
  toWorkProperly(received: number, actual: number) {
    return {pass:true}
  }
})

it should be possible to extend the corresponding types:

declare module "expect" {
  interface Matchers<R> {
    toWorkProperly(actual: number): void
  }
}

instead you get
image

To Reproduce

see above

Expected behavior

There should be no error and proper typechecking.

The reason for the issue, is that expect doesn't actually export the Matchers interface which expect() returns, but rather a sub-interface. See https://github.com/facebook/jest/blob/master/packages/expect/src/index.ts#L429 .

Unfortunately, it isn't possible to do the right thing directly here, i.e., you can't re-export an interface in a namespace. The quick option would be to move the Matchers declaration directly into the namespace instead of importing it from './types'. The proper solution would probably be to use a proper ES export for expect.

Link to repl or repo (highly encouraged)

repl.it link clicky clicky

envinfo

 npx envinfo --preset jest
npx: installed 1 in 4.202s

  System:
    OS: Linux 5.4 Debian GNU/Linux 9 (stretch) 9 (stretch)
    CPU: (4) x64 Intel(R) Xeon(R) CPU @ 2.30GHz
  Binaries:
    Node: 12.18.3 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.6 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.5.3 => 26.5.3 

 
@NaridaL
Copy link
Author

NaridaL commented May 30, 2021

Unfortunately, #10642 was closed by #11211, which only concerns the "using globals" case, not the issue described by #10642.

@SimenB I noted possible solutions both in the issue description an in #10642 (comment), but didn't get an answer. It would be appreciated if you could take another look.

NaridaL added a commit to NaridaL/jest that referenced this issue May 30, 2021
This allows types to be exported as well.
Fixes jestjs#11487
Strictly speaking a breaking change, as expect = require('expect') not longer works, but as this is usually used as a global, this shouldn't be much of an issue.
NaridaL added a commit to NaridaL/jest that referenced this issue May 30, 2021
This allows types to be exported as well.
Fixes jestjs#11487
Strictly speaking a breaking change, as expect = require('expect') not longer works, but as this is usually used as a global, this shouldn't be much of an issue.
NaridaL added a commit to NaridaL/jest that referenced this issue May 30, 2021
This allows types to be exported as well.
Fixes jestjs#11487
Strictly speaking a breaking change, as expect = require('expect') not longer works, but as this is usually used as a global, this shouldn't be much of an issue.
NaridaL added a commit to NaridaL/jest that referenced this issue May 30, 2021
This allows types to be exported as well.
Fixes jestjs#11487
Strictly speaking a breaking change, as expect = require('expect') not longer works, but as this is usually used as a global, this shouldn't be much of an issue.
@PaperStrike
Copy link

For anyone looking for a workaround, here's a dirty one:

// Use module augmentation on the internal 'expect/build/types'
declare module 'expect/build/types' {
  interface Matchers<R> {
    // ...
  }
}

@SimenB
Copy link
Member

SimenB commented Feb 22, 2022

This will be supported in Jest 28 (currently available as jest@next), see example: https://github.com/facebook/jest/blob/eafc350f648ec75d7fb691b4ac45e02de8d68ada/examples/expect-extend/toBeWithinRange.ts#L49-L56

@SimenB SimenB closed this as completed Feb 22, 2022
@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 Mar 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants