-
-
Notifications
You must be signed in to change notification settings - Fork 769
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
A very basic fs stub fails with typescript and ts-node. #2296
Comments
To my knowledge, Sinon doesn't support stubbing imported modules. |
I have seen an example of something like this. #458 . I just tried migrating that example to |
We are trying to keep the GitHub issues list tidy and focused on bugs and feature discussions. This ticket looks like a usage question; please post it to StackOverflow and tag it with If you feel that your topic is an issue with Sinon, please open a new ticket and follow the guidelines for reporting an issue. @mohitt Since you are using Typescript you are actually not using ES Modules (which is not possible without transpilation, see #2258). That means that what you are trying is probably possible, assuming it's transpiled to ES5 or something like that. The way to go about this is to attack it like you are working with ES5. So inspect what the resulting code looks like in Javascript after transpilation and work with that. You need to account for all kinds of weird stuff like module loaders, webpack, esm, etc. yourself, because the environment is different from project to project. Sinon only works with the normal javascript runtime and does not try to capture imports and interfere with the runtime. That is what tools like People on SO are usually quick to answer Sinon questions, btw. I also monitor the tags from time to time. |
any update on how we could stub fs in typescript using sinon ? |
@uchia-itachi Typescript is only Javascript in the end, so there is nothing inherent about TS that makes this any different than any non-TS project. As mentioned above:
So answering your question will depend on how your project is setup. Does it transpile to ES5.1 with synthetic getters, does it create actual ESM, does it use Vite, does it use Webpack, does it use Jest, etc. There is no general answer to this. You need to look into what the Javascript you want to test looks like and choose tools accordingly. If you use Jest, for instance, it will change the runtime in order to allow to capture all calls for modules. If you run using Vite, this all goes out the window and you need to find some other way of doing things. If I were you I would create a minimal project on GH that mimicks your existing project structure and ask a question on Stack Overflow, linking to that. Good luck! |
Describe the bug
I am trying to stub
existsSync
method of fs module and have written a very basic test for it. Is there anything I am doing wrong ?To Reproduce
Steps to reproduce the behavior:
npm run test
Following is the test https://github.com/mohitt/isolate-sinon/blob/master/fs.test.ts
I am using
typescript
andts-node
withmocha
.Expected behavior
The test should pass once the
existsSync
is stubbed usingSinon.stub
.existsSync
should always return true.Screenshots
If applicable, add screenshots to help explain your problem.
Context (please complete the following information):
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: