I create a simple test with Jest's default automatic mocking. I'm trying to test one of my app's modules, so I need to make sure the module I am testing is not mocked. However, using jest.unmock('../moduleName') did not seem to work.
I also tried using require.requireActual('../moduleName'), which also did not work.
https://facebook.github.io/jest/docs/api.html#require-requireactual-modulename
Context: Using latest TypeScript and React Native. All source and test files are written in TypeScript. Using a tsprocessor for Jest.
Printing out the module that I intend to test seems to indicate that it is mocked, even when I explicitly told it not to mock this module:
{ MyClass:
{ [Function: MyClass]
_isMockFunction: true,
_getMockImplementation:
{ [Function]
_isMockFunction: true,
_getMockImplementation: [Function],
mock: [Object],
mockClear: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImpl: [Function],
mockImplementation: [Function],
mockReturnThis: [Function] },
mock: { calls: [], instances: [] },
mockClear: [Function],
mockReturnValueOnce: [Function],
mockReturnValue: [Function],
mockImplementationOnce: [Function],
mockImpl: [Function],
mockImplementation: [Function],
mockReturnThis: [Function] } }
jest.unmock('../moduleName') should work as expected if the path to the module is the same as the import done in the code you are testing
@vvo Here's a more specific example that did not work:
My test file (I'm trying to test TwitterProtocol module):
jest.unmock('../twitterProtocol')
import { TwitterProtocol } from '../twitterProtocol'
I believe the above code is correct?
I believe the above code is correct?
Yes this should work, can you create a simple repository test folder on github so we can investigate?
Thanks for opening an issue. imports in TypeScript work similarly to ES2015 – they are hoisted above all other statements. When using babel-jest, we actually hoist unmock calls above imports. If you are using TypeScript, we don't have this custom transform available unfortunately.
Here are a few ways to work around this issue:
import, use require inside of your test files.babel-jest in your typescript preprocessor after processing your typescript code into JS. (basically: return babelJest.process(tsOutput, filename); ).Of course, the right way to fix this would be a plugin for typescript that hoists jest.unmock calls.
@cpojer Would such a plugin for TypeScript be difficult to make? If it's not too difficult, I can give it a shot.
I don't know, does TypeScript have a plugin system like babel does or is the compiler still completely closed?
@cpojer would it make sense to run babel-preset-jest transformation on top of anything that is not babel-jest?
this would solve the coverage problem too
so basically.. if you're using babel-jest you get babel-plugin-jest-hoist and babel-plugin-istanbul for free as a last step in your babel pipeline.
If you're using typescript/coffeescript/whatever, we'll jest run these two as a second transformation step. Performance would suck, but will solve a lot of issues
It's definitely ok if we go down this route. I wouldn't mind for everything that doesn't use babel already.
At FB, we use our custom preprocessor and we need to make sure we don't double process there – what kind of escape-hatch can you think of for this use-case?
To summarize:
Most helpful comment
Thanks for opening an issue. imports in TypeScript work similarly to ES2015 – they are hoisted above all other statements. When using babel-jest, we actually hoist
unmockcalls above imports. If you are using TypeScript, we don't have this custom transform available unfortunately.Here are a few ways to work around this issue:
import, userequireinside of your test files.babel-jestin your typescript preprocessor after processing your typescript code into JS. (basically:return babelJest.process(tsOutput, filename);).Of course, the right way to fix this would be a plugin for typescript that hoists
jest.unmockcalls.