-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
ReferenceError: Cannot access 'myMock' before initialization
even though the rule is respected
#10996
Comments
As I can see the error message tells you about variable
|
There is nothing called |
This is happening to me, too:
Jest output:
|
You may use this: import { X } from 'path/to/module'
jest.mock('path/to/module', () => ({
X: jest.fn()
})); in your test block:: expect(X).toHaveBeenCalled() |
@amitnsky any suggestion then for TypeScript complaining that |
Instead of saying "+1", please provide reproductions. Questions about TS is out of scope for this issue (and this repo if you use |
@kylegoetz can you share code pls so that i can check. |
@amitnsky thank you 🙇 |
No reproduction has been provided, so I'll close this. Feel free to open up a new issue with a minimal reproduction if anybody has one |
Bump, need to reopen this. I have a function, which returns another function(s) that I'd like to mock. With the solution provided, it's not really possible. re-mocking is not possible as well. Here's the minimal code to reproduce: const mock = jest.fn();
jest.mock('module', () => ({
fn: mock,
})); output:
the module looks like this: export default (...args) => {
// some code
return {
foo: () => ...,
bar: () => ..., // <-- I want to mock this.
};
}; I tried remocking it in beforeEach, but does not seem to work, because module is imported this way: import module from 'module';
const { bar } = module(...); EDIT: this is only affected when the destructuration happens in a global scope (root level), not affected if it's inside a function. |
Note it should also be complete not snippets. https://stackoverflow.com/help/minimal-reproducible-example is a great text explaining what goes into a good reproduction case for a bug report 🙂 |
🐛 Bug Report
I'm getting the error:
ReferenceError: Cannot access 'myMock' before initialization
Even though i respected jest documentation about the hoisting: A limitation with the factory parameter is that, since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'.
what i'm doing:
As you can see, i'm mocking a singleton class.
The static method (here:
staticMethod
) is the entry point of my class.To Reproduce
Steps to reproduce the behavior:
Having a singleton class like so:
About the test file, you can see what i'm doing above.
Expected behavior
Not throwing
ReferenceError: Cannot access 'myMock' before initialization
when the rule about mock variable starting withmock
is respected.Link to repl or repo (highly encouraged)
envinfo
Thanks in advance for any help !
The text was updated successfully, but these errors were encountered: