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

@jest/globals doesn't seem to work with jest.mock #10060

Closed
Vinnl opened this issue May 18, 2020 · 3 comments
Closed

@jest/globals doesn't seem to work with jest.mock #10060

Vinnl opened this issue May 18, 2020 · 3 comments

Comments

@Vinnl
Copy link
Contributor

Vinnl commented May 18, 2020

πŸ› Bug Report

When importing jest from @jest/globals, and running jest.mock, I get:

  ● Test suite failed to run

    TypeError: Cannot read property 'jest' of undefined

      2 | import { jest, it, expect } from "@jest/globals";
      3 | // import { it, expect } from "@jest/globals";
    > 4 | jest.mock('./mocked.ts');
        | ^

That's what I get in the minimal reproduction repo. In my regular code, I instead get this error:

ReferenceError: Cannot access 'globals_1' before initialization

To Reproduce

Steps to reproduce the behavior:

  1. git clone git@github.com:Vinnl/ts-jest-repro.git --branch globals-mock
    (Note that this is not on the master branch.)
  2. npm install
  3. npm test

This is a repository with Jest and ts-jest setup. The problematic code is:

// COMMENT OUT THE (jest) IMPORT TO MAKE THIS WORK AGAIN
import { jest, it, expect } from "@jest/globals";
// import { it, expect } from "@jest/globals";
jest.mock('./mocked.ts');

import { add } from './add';

it('should properly add', () => {
	expect(add(1, 2)).toBe(3);
});

Expected behavior

The test to run successfully.

Link to repl or repo (highly encouraged)

git clone git@github.com:Vinnl/ts-jest-repro.git --branch globals-mock

envinfo

$  npx envinfo --preset jest 
npx: installed 1 in 0.889s

  System:
    OS: Linux 5.4 Ubuntu 20.04 LTS (Focal Fossa)
    CPU: (4) x64 Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
  Binaries:
    Node: 12.16.1 - ~/.nvm/versions/node/v12.16.1/bin/node
    Yarn: 1.22.4 - /usr/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.16.1/bin/npm
  npmPackages:
    jest: ^26.0.1 => 26.0.1 

Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 18, 2020
This makes it easier for TypeScript to typecheck our tests, rather
relying on `any`-typed globals.

Unfortunately, it was not possible to
`import { jest } from "@jest/globals"` in files that also call
jest.mock(). This was reported here:
jestjs/jest#10060
Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 18, 2020
This makes it easier for TypeScript to typecheck our tests, rather
relying on `any`-typed globals.

For more details, see
https://jestjs.io/blog/2020/05/05/jest-26#a-new-way-to-consume-jest---jestglobals

Unfortunately, it was not possible to
`import { jest } from "@jest/globals"` in files that also call
jest.mock(). This was reported here:
jestjs/jest#10060
@ahnpnl
Copy link
Contributor

ahnpnl commented May 19, 2020

Possibly related to the fact that ts-jest doesn’t have the hoisting implementation yet like Babel-jest, see kulshekhar/ts-jest#1593

Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 19, 2020
This makes it easier for TypeScript to typecheck our tests, rather
relying on `any`-typed globals.

For more details, see
https://jestjs.io/blog/2020/05/05/jest-26#a-new-way-to-consume-jest---jestglobals

Unfortunately, it was not possible to
`import { jest } from "@jest/globals"` in files that also call
jest.mock(). This was reported here:
jestjs/jest#10060
@SimenB
Copy link
Member

SimenB commented May 19, 2020

Yep, this will only work properly with babel-plugin-jest-hoist (included in babel-jest config). If you use another transformer, that will need to support it

@SimenB SimenB closed this as completed May 19, 2020
Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 25, 2020
This makes it easier for TypeScript to typecheck our tests, rather
relying on `any`-typed globals.

For more details, see
https://jestjs.io/blog/2020/05/05/jest-26#a-new-way-to-consume-jest---jestglobals

Unfortunately, it was not possible to
`import { jest } from "@jest/globals"` in files that also call
jest.mock(). This was reported here:
jestjs/jest#10060
Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 25, 2020
This makes it easier for TypeScript to typecheck our tests, rather
relying on `any`-typed globals.

For more details, see
https://jestjs.io/blog/2020/05/05/jest-26#a-new-way-to-consume-jest---jestglobals

Unfortunately, it was not possible to
`import { jest } from "@jest/globals"` in files that also call
jest.mock(). This was reported here:
jestjs/jest#10060
@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 May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants