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.

[Bug]: Using @jest/globals expect breaks extensions

See original GitHub issue

Version

27.4.7

Steps to reproduce

git clone git@github.com:johncrim/jest-bugs.git
yarn
yarn test-esm

There are two versions of the same test, one using

import { expect } from '@jest/globals';

the other using the ambient/global expect.

The test using import { expect } from '@jest/globals'; fails with:

 PASS  src/expect-extension-no-jest-globals.spec.ts
 FAIL  src/expect-extension-jest-globals.spec.ts
  ● Test suite failed to run

    src/expect-extension-jest-globals.spec.ts:6:18 - error TS2339: Property 'toIncludeSameMembers' does not exist on type 'Matchers<void, number[]>'.

    6   expect([1, 2]).toIncludeSameMembers([2, 1]);

because the type returned from expect() in ‘@jest/globals’ (Matchers<void, T> in expect module) does not match the type returned from expect() in @types/jest (jest.JestMatchers<T>).

Expected behavior

expect extension typing continues to work whether you’re importing from @jest/globals or not.

Actual behavior

expect extensions fail to compile when importing expect from @jest/globals. If you can get around the typing issue (via casting or any or creating a global type def file to fix the issue) the runtime behavior works as expected.

Additional context

This breaks all expect extensions in libraries I’m aware of, including:

  • jest-extended
  • ngneat/spectator
  • expect-more-jest

It also breaks anyone using private expect extensions using the approach that is widely documented and used - a file like:

// my-jest-matchers.d.ts
declare namespace jest {
  interface Matchers<R> {
    equalsFoo(message: string): R;
  }
}

will no longer enable compilation given expect(x).equalsFoo('message');.

The obvious workaround is “don’t use @jest/globals”, but that is not viable in many codebases - particularly if using ES Modules. In our codebase, we have jasmine types and jest types, and using @jest/globals is necessary now that we’ve changed to ES Modules.

I believe this is the same issue as #10642. #10642 was correctly reported, but it was incorrectly closed. This comment about creating a type def file for expect extensions is helpful, but is not a fix for #10642. Hopefully this issue provides more clarity.

Environment

System:
    OS: Windows 10 10.0.19044
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
  Binaries:
    Node: 16.12.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.15 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 8.1.0 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^27.3.1 => 27.4.7

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Feb 18, 2022

The plan is to make @types/jest import from expect instead of defining everything itself, at which point it should work seamlessly

0reactions
github-actions[bot]commented, Mar 21, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Globals - Jest
In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import...
Read more >
@jest/globals | Yarn - Package Manager
Jest will set process.env.NODE_ENV to 'test' if it's not set to something else. You can use that in your configuration to conditionally setup...
Read more >
vscode jest extension doesn't work properly - Stack Overflow
Highly recommend using the jest runner extension instead. Demo:.
Read more >
Strict mode - JavaScript - MDN Web Docs
"use strict"; let mistypeVariable; // Assuming no global variable mistypeVarible exists // this line throws a ReferenceError due to the ...
Read more >
Wallaby - Immediate JavaScript test feedback in your IDE as ...
Got @wallabyjs to continuously run Jest tests in @WebStormIDE and it's the ... Set up @wallabyjs with typescript for the VS @Code extension...
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