-
Notifications
You must be signed in to change notification settings - Fork 451
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
[Bug]: variable beginning with 'mock' ist NOT hoisted. #3292
Comments
By default, EDITED: reference link jestjs/jest#8148 (comment) this is a bug indeed |
This was discussed previously in 2020: #1088 Essentially, jest doesn't hoist variables starting with @ahnpnl in the linked issue, you indicated that you were not interested in supporting this in ts-jest. Is this no longer the case? If so, where does this fit on your roadmap, and is any help required with this issue? It's currently preventing me from migrating my projects from jest+babel to ts-jest. I'm creating mocks in ways like the following: const mockSSM = {
getParameter: jest.fn().mockReturnThis(),
putParameter: jest.fn().mockReturnThis(),
promise: jest.fn(),
}
jest.mock('aws-sdk', () => ({
SSM: jest.fn().mockImplementation(() => mockSSM),
})) |
It's not about not interested in supporting this in |
Thanks, I've opened a PR #3400 with a failing e2e test, however I'm not entirely sure where to go from here - the tests for the AST transformer seem to already check for this behaviour. |
The following is a decent workaround until The key is to declare the variable using // eslint-disable-next-line no-var
var mockThing: jest.Mock;
jest.mock('./path-to/module', () => {
mockThing = jest.fn();
return {
thing: mockThing,
};
});
// OR more succinctly
jest.mock('./path-to/module', () => ({
thing: (mockThing = jest.fn()),
})); then you can do whatever you want in your beforeEach(() => {
mockThing.mockRejectedValue(new Error('bang!'));
}); |
Version
27.1.3
Steps to reproduce
Expected behavior
Test runs without error.
Actual behavior
throws error.
ReferenceError: Cannot access 'mock' before initialization
Debug log
● Test suite failed to run
Additional context
No response
Environment
The text was updated successfully, but these errors were encountered: