theKashey/rewiremock

Does rewiremock work with ts-node?

justin-calleja opened this issue · 3 comments

I left a repo here with an example of failing to mock: https://github.com/justin-calleja/use-rewiremock

Can anyone point me in the right direction?

Thanks

Closing issue as was missing:

rewiremock.enable()

and then it works. Added fix to example repo: justin-calleja/use-rewiremock@53f78a3

JFYI: https://github.com/justin-calleja/use-rewiremock/blob/main/src/tmp.ts should not work because all imports will be executed PRIOR any other code - they will be automatically hoisted as imports cannot happen in the middle of the file.

If you need something like that - you have to add rewiremock/babel

@theKashey thanks for taking the time to check this out!

all imports will be executed PRIOR any other code - they will be automatically hoisted as imports cannot happen in the middle of the file.

hmm then must be ts-node that's allowing me to use imports in middle of file? Not sure, but it is working; both latest (made some small changes): justin-calleja/use-rewiremock@ea80fd7

and original: justin-calleja/use-rewiremock@53f78a3

I'm running with:

❯ ./node_modules/.bin/ts-node ./src/tmp.ts

One thing I had to do (in another context / project) was to use full path like this:

rewiremock(resolve(__dirname, '../../src/path/to/file')).with(() => '... mocked response')

Without doing the full path - I'd have one import be mocked and another one (import of same module X from anther file - not mocked).