testdouble / testdouble-jest

A testdouble.js extension to add support for Jest module mocking
MIT License
37 stars 5 forks source link

Does testdouble-jest work with TypeScript? #7

Open rene-leanix opened 3 years ago

rene-leanix commented 3 years ago

For our own Angular TypeScript repo, I encountered the following error when I tried out testdouble-jest: It appears the test is being run by Jest, but the testdouble-jest module has not been initialized.... I did do the initialisation with require('testdouble-jest')(td, jest) and the mock function was added to the imported testdouble module, but it was not available in the module when required again here.

I tried to reproduce this by taking the example tests from this repo, porting them to TypeScript and updating to the latest version of Jest. See #6. Unfortunately, the error did not occur there, but at the same time all the test cases fail for other reasons. I haven't had time to investigate this any further, but am posting this here in case someone from the testdouble team would like to look at this.

forivall commented 9 months ago

When I was looking through td-jest's documentation, and with knowledge of some parts of jest.mock's internals, td.mock will need to have special code transformer logic to hoist the mock module declarations above imports for mocking to work properly. I'm not sure if that is the case here, but it certainly is Test Double Trouble.

searls commented 9 months ago

I'll be honest in that I've always avoided using Jest and only slapped this module together well enough to get it to work at initial release. There's not much code here, though, so if someone has an idea on how to fix it and an interest in helping, I'd appreciate!