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

Typing failure on expect().toThrow(Error) with explicit imports #10087

Closed
momocow opened this issue May 26, 2020 · 3 comments · Fixed by #11929
Closed

Typing failure on expect().toThrow(Error) with explicit imports #10087

momocow opened this issue May 26, 2020 · 3 comments · Fixed by #11929

Comments

@momocow
Copy link

momocow commented May 26, 2020

🐛 Bug Report

Here's the minimum reproducible snippet.

My project is written in Typescript and I'm using jest along with ts-jest.

// test.ts
import { test, expect } from '@jest/globals'

test('error', () => {
  function throwError (): never { throw new Error() }
  expect(throwError).toThrow(Error)
})

After running the test file, an error is shown.

error TS2345: Argument of type 'ErrorConstructor' is not assignable to parameter of type 'string | RegExp | Error | Constructable | undefined'.
      Type 'ErrorConstructor' is not assignable to type 'Constructable'.
        Types of parameters 'message' and 'args' are incompatible.
          Type 'unknown' is not assignable to type 'string | undefined'.
            Type 'unknown' is not assignable to type 'string'.

However, if I remove the first line which makes the test using jest globals, everything works fine.

That is,

// test.ts
test('error', () => {
  function throwError (): never { throw new Error() }
  expect(throwError).toThrow(Error)
})

I found that source definition of expect is different between two tests.

With explicit imports, the definition is coming from node_modules\expect\build\types.d.ts, while with implicit imports, the definition is coming from node_modules\@types\jest\index.d.ts.

To Reproduce

Steps to reproduce the behavior:

  • Create a test file with the snippet above.
  • Run the test.

Expected behavior

No typing errors.

envinfo

  System:
    OS: Windows 10 10.0.18362
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  Binaries:
    Node: 12.16.0 - <Program Files>\nodejs\node.EXE
    Yarn: 1.9.4 - <Program Files>\Yarn\bin\yarn.CMD
    npm: 6.13.4 - <Program Files>\nodejs\npm.CMD
  npmPackages:
    jest: ^26.0.1 => 26.0.1
@josh-berry
Copy link

This is reproducible with just the expect package by itself. Here's how to setup a minimal test:

$ mkdir repro
$ cd repro
$ npm init
$ npm i --save-dev typescript expect
$ vi test.ts

Write the following into the test.ts file:

import expect from 'expect';

function oops() {
    throw new Error('oops');
}

expect(oops).toThrow(Error);

Then try to compile it with --strict --esModuleInterop (the latter option is needed to allow the use of import):

$ ./node_modules/.bin/tsc --strict --esModuleInterop test.ts

test.ts:7:22 - error TS2345: Argument of type 'ErrorConstructor' is not assignable to parameter of type 'string | RegExp | Error | Constructable | undefined'.
  Type 'ErrorConstructor' is not assignable to type 'Constructable'.
    Types of parameters 'message' and 'args' are incompatible.
      Type 'unknown' is not assignable to type 'string | undefined'.
        Type 'unknown' is not assignable to type 'string'.

7 expect(oops).toThrow(Error);
                       ~~~~~

  test.ts:7:22
    7 expect(oops).toThrow(Error);
                           ~~~~~
    Did you mean to use 'new' with this expression?


Found 1 error.

@SimenB
Copy link
Member

SimenB commented Sep 30, 2021

PR welcome 🙂

@github-actions
Copy link

github-actions bot commented Nov 5, 2021

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 Nov 5, 2021
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