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

ReferenceError: Cannot access 'myMock' before initialization if test itself and 'myMock' are in different files #11455

Closed
dbtrnl opened this issue May 26, 2021 · 7 comments

Comments

@dbtrnl
Copy link

dbtrnl commented May 26, 2021

🐛 Bug Report

Same problem as issue #10996, encountering ReferenceError: Cannot access 'myMock' before initialization while trying to run test on a mock imported from another file.
If the mock is in the same file as the test, it runs correctly.

These two test cases are included in the repo (see below)

To Reproduce

  1. Clone the repo https://github.com/dnbtr/jest-test-bug;
  2. Run $ npm install to install dependencies;
  3. Run tests with $ npm test.

Expected behavior

Two tests are provided, in the following folders:
src/tests/workingTest
src/tests/buggyTest

Files on src/adapters and src/integrations are not relevant to the issue.
I hadn't time to cut the code to the bare minimum.

The workingTest should pass, the buggyTest should fail with ReferenceError: Cannot access 'mockDynamoRepository_1' before initialization.

Both tests are identical, the difference is that the mock (DynamoRepositoryMock) was moved to a different file in the buggy test.
Uncomment lines on src/tests/buggyTest/buggyTest.spec.ts to see other implementations that also don't work and result in the same error.

Expected output:

 PASS  src/tests/workingTest/workingTest.spec.ts (6.316 s)
 FAIL  src/tests/buggyTest/buggyTest.spec.ts
  ● Test suite failed to run

    ReferenceError: Cannot access 'mockDynamoRepository_1' before initialization

      17 | // jest.mock('aws-sdk', () => { mockDynamoRepository; });
      18 |
    > 19 | jest.mock('aws-sdk', () => mockDynamoRepository<TestType>([]));
         |                                                ^
      20 |
      21 | describe('Test DynamoRepository (buggy test, SHOULD FAIL)', () => {
      22 |   const repo: IRepositoryAdapter = DynamoRepository;

      at src/tests/buggyTest/buggyTest.spec.ts:19:48
      at Object.<anonymous> (src/integrations/DynamoRepository.ts:5:1)
      at Object.<anonymous> (src/tests/buggyTest/buggyTest.spec.ts:3:1)

Link to repo

https://github.com/dnbtr/jest-test-bug

envinfo

There is no env. Repo can be used as-is.

@ahnpnl
Copy link
Contributor

ahnpnl commented May 27, 2021

Does it happen if you use babel-jest to transform TypeScript (I see you are using ts-jest)?

@dbtrnl
Copy link
Author

dbtrnl commented May 27, 2021

Hello,

I tried babel-jest using the instructions in Jest's README, but the error persists. I created a new branch for reproduction.

Tried calling jest with different parameters as well (see 'package.json`) but always the same result.

@SimenB
Copy link
Member

SimenB commented May 27, 2021

This is a runtime error, the babel plugin would have warned you if you didn't name it mock*. You need to do require or something inside the jest.mock callback. jest.mock calls are hoisted, so they run before you import the helper, and thus the helper is not yet defined when you try to use it

@SimenB SimenB closed this as completed May 27, 2021
@dbtrnl
Copy link
Author

dbtrnl commented May 27, 2021

Hi Simen,

Strangely, the mock had a different name (without mock*), but there were no warnings, which led me to opening this issue.
I changed it to the current name because of the issue referenced in the beginning.

I still can't quite make it function, but now it's a Typescript thing. Thanks for the help.

@SimenB
Copy link
Member

SimenB commented May 27, 2021

Ah, probably because of ts-jest's implementation of the hoisting.

Using babel-jest throws this:
image

@dbtrnl
Copy link
Author

dbtrnl commented May 28, 2021

I see, thanks for the insight! I still get lost on the internals of Jest :(

@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 Jun 28, 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