-
Notifications
You must be signed in to change notification settings - Fork 453
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
ts-jest does not hoist variables beginning with mock #1088
Comments
Jest does not describe that all variables that begin with mock are hoisted, well it certainly does not now. It does not hoist variables with name that begins with mock.
The exception is that there will be no error thrown. Below is the code for the hoisting :
Note the
The code for
Can be found here Also note that code of the form This leads me to actual differences between jest and ts-jest. ts-jest does not throw on out of scope variables. The two differences are important and the source code for ts-jest should be changed. I am having issues with vscode wsl installation of ts-jest so I cannot create the changes but here are the details.
Of course the tests need to change.
to
The end to end test does not test disableAutomock or enableAutomock currently. Also afterAll does nothing useful. If you are interested I created compare-jest-tsjest-hoisting. This transforms typescript with ts-jest and javascript with babel and babel-preset-jest ( which provides babel-plugin-jest-hoist ) with their APIs and compares the two within a jest test. With the changes mentioned ts-jest and jest will be more aligned and you can see that this is so with this repo. |
Facing the same issue. My current work around is to use
|
@ahnpnl I'm a bit confused by this, as it seems like From what I can tell that's a bug in the
Except that jest does hoist variables whose name begins with "mock". Currently this code is erroring for me:
I'm happy to make a PR modifying the hoisting transformer to fix this, but didn't want to open up a new issue if this has already been decided on, since theres already been a few. I do think this should be documented at the very least, to help avoid confusion :) |
hi @G-Rath, it would be very nice that you can help The whole point from @tonyhallett is making Look like |
Awesome - feel free to assign this issue to me if you like :)
Not too sure what you mean, but what I'll do is just copy the code examples we use in the jest codebase for testing the hoisting into the test suite for |
Anytime you feel comfortable with :)
There was a discussion in jestjs/jest#9806 which I suppose need to be fixed for |
Looking into this further, I've realised that @tonyhallett is completely correct, and what he was meaning: jest doesn't hoist variables that start with What happens is that when doing the hoisting for So if it finds a variable (such "myFakeTimer") it'll throw; it's effectively doing a little bit of what TypeScript does in general, to be helpful given that this hoisting is happening in the background & so not obvious. However as an advanced escape hatch, the babel plugin allows variables that start with
But so tl;dr this bug isn't a bug - it's actually an (understandable) misinterpretation of the docs: jest doesn't hoist variables starting with "mock", it only ignores them when checking in hoisted Meanwhile, I've brought the tests used by the babel plugin over into So far they seem to just be working out of the box after I added For example, it's failing on scopes:
That makes sense as this is change required for supporting It might make sense to close this issue & open a new one to track the adding of support for |
Thank you for a very clear explanation. I will close this issue and please feel free to open a new issue to support @jest/global 👌 I was suspecting that it is required to support @jest/global when I see jest team working on that for Babel plugin, now you confirm then implementing the similar behavior for ts-jest makes sense. |
PR to clarify docs very much welcome 🙂 |
support hoisting with |
Issue :
Jest docs describe that all variables that begin with mock are hoisted to the top of the file, above any jest.mock calls:
https://jestjs.io/docs/en/es6-class-mocks#calling-jestmock-docs-en-jest-object-jestmockmodulename-factory-options-with-the-module-factory-parameter
Currently it seems like our implementation of hoist only hoists jest.mock and jest.unmock calls: https://github.com/kulshekhar/ts-jest/blob/a6bcec48ef28791235b3eba0b3f2bd1a944ba5b8/src/transformers/hoist-jest.ts
We should further implement it so that it hoists all variables called something with 'mock' to the top
The text was updated successfully, but these errors were encountered: