You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The conditions around when babel-jest can hoist a function is a bit confusing. If mocks are hoisted, then it's safe to require a module that depends on them. But if they aren't, then the real module will be unintentionally used instead.
If tests consistently use jest.mock for hoistable module mocks, and use jest.doMock for mocks that cannot be hoisted, then its easy to just look at the test and see if its safe to require a module that depends on the mocks at the top level.
jest.mock() must be called at the top-level, and not inside another function.
The first argument to jest.mock() should be a literal value.
The second argument, if provided, should be an inline function.
That function shouldn’t reference any variables defined outside the function.
Examples of incorrect code for this rule:
constmoduleName='./module';jest.mock(moduleName);functionfactory(){returnjest.fn();}jest.mock('./module',factory);jest.mock('./module',()=>{// variable defined outside scopereturnjest.fn().mockReturnValue(moduleName);});
Examples of correct code for this rule:
constmoduleName='./module';jest.doMock(moduleName);functionfactory(){returnjest.fn();}jest.doMock('./module',factory);jest.doMock('./module',()=>{returnjest.fn().mockReturnValue(moduleName);});// can be hoistedjest.mock('random-num');jest.mock('../module',()=>{returnjest.fn().mockReturnValue('foo');});
Auto-fixable: Yes
The text was updated successfully, but these errors were encountered:
I'm not sure how valuable/ accurate we could be on this - in my experience mocking beyond jest.mock and jest.spyOn tend to quickly become pretty advanced & complex so I suspect such a rule could have a lot of caveats.
Two specific things that come to mind is that mocking is often done for modules that are imported within other files (which we can't know about), and that there's a rule about allowing variables that begin with mock as a bail-out (which acknowledges how complex mocking can be).
The conditions around when
babel-jest
can hoist a function is a bit confusing. If mocks are hoisted, then it's safe to require a module that depends on them. But if they aren't, then the real module will be unintentionally used instead.If tests consistently use
jest.mock
for hoistable module mocks, and usejest.doMock
for mocks that cannot be hoisted, then its easy to just look at the test and see if its safe to require a module that depends on the mocks at the top level.Here’s the checks I found from digging through the source code of Jest’s babel plugin.
jest.mock()
must be called at the top-level, and not inside another function.jest.mock()
should be a literal value.Examples of incorrect code for this rule:
Examples of correct code for this rule:
Auto-fixable: Yes
The text was updated successfully, but these errors were encountered: