-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
babel-plugin-jest-hoist: The module factory of jest.mock()
is not allowed to reference any out-of-scope variables.
#9730
Comments
Alright, it seems all mocked functions need to be prefixed with
const foo = jest.fn();
jest.mock('./foo', () => ({
// throws: babel-plugin-jest-hoist: The module factory of jest.mock() is not allowed to reference any out-of-scope variables.
foo,
})) Prefixing with // prefixing with mock avoids out-of-scope error to be thrown
// but value of foo will be undefined
const mockFoo = jest.fn();
jest.mock('./foo', () => ({
foo: mockFoo,
})) |
Only way I got it working is by importing my mock within my testfile, like: // only here mockFoo will be deifned!
import { foo as mockFoo } from './foo';
jest.mock('./foo', () => ({
foo: jest.fn();,
})) |
Yeah, this is the correct behavior. You get the warning since You can import the mock function again as you note, that's usually the cleanest solution. This is all expected, and documented, behavior. If you have further questions, please use StackOverflow or our discord channel. If the docs can be improved, a PR is always welcome! |
@SimenB Please check the docs, I think especially this page and the coding example is misleading, in addition it does not mention the Also the Readme of But the source code has a comment about it:
... and an implementation 🤔 |
… & babel-plugin-jest-hoist. See: jestjs/jest#9730 I think it's trying to be helpful but just being an annoying PITA instead.
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. |
🐛 Bug Report
Spying on mocked methods as described in docs, throws
babel-plugin-jest-hoist: The module factory of
jest.mock()is not allowed to reference any out-of-scope variables.
:https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class
To Reproduce
Steps to reproduce the behavior:
Follow the docs here https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class
Expected behavior
What is documented should work:
https://jestjs.io/docs/en/es6-class-mocks#spying-on-methods-of-our-class
Link to repl or repo (highly encouraged)
Similiar issue #2567
envinfo
The text was updated successfully, but these errors were encountered: