Skip to content
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

Closed
mohitt opened this issue Sep 20, 2020 · 5 comments
Closed

A very basic fs stub fails with typescript and ts-node. #2296

mohitt opened this issue Sep 20, 2020 · 5 comments

Comments

@mohitt
Copy link

mohitt commented Sep 20, 2020

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:

  1. Go to https://github.com/mohitt/isolate-sinon
  2. Clone and run npm run test
  3. You will see a test failure. Ideally speaking that test should not fail.

Following is the test https://github.com/mohitt/isolate-sinon/blob/master/fs.test.ts

import chai from 'chai';
import sinon from 'sinon';
import * as fs from 'fs';
import chaiAsPromised from 'chai-as-promised';

chai.use(chaiAsPromised);

import { expect } from 'chai';


describe('readConfigFile', () => {
  it('returns Fallback value if file is not there - @FileNotThere', () => {

    sinon.stub(fs, 'existsSync').returns(true);
    expect(fs.existsSync('something')).true;

  });
});

I am using typescript and ts-node with mocha.

Expected behavior
The test should pass once the existsSync is stubbed using Sinon.stub. existsSync should always return true.

Screenshots
If applicable, add screenshots to help explain your problem.

Context (please complete the following information):

  • Library version: please verify that the bug exists in the latest release
  • Environment:
  • Example URL:
  • Other libraries you are using:

Additional context
Add any other context about the problem here.

@rgroothuijsen
Copy link
Contributor

To my knowledge, Sinon doesn't support stubbing imported modules.

@mohitt
Copy link
Author

mohitt commented Sep 20, 2020

I have seen an example of something like this. #458 . I just tried migrating that example to ts-node and typescript.

@fatso83
Copy link
Contributor

fatso83 commented Sep 22, 2020

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 sinon, so the bigger community can help answer your questions.

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 proxyquire, Jest, etc is for, as they change the runtime in order to do "magic" stuff. This is all totally different from project to project, so we cannot do much about this.

People on SO are usually quick to answer Sinon questions, btw. I also monitor the tags from time to time.

@fatso83 fatso83 closed this as completed Sep 22, 2020
@uchia-itachi
Copy link

any update on how we could stub fs in typescript using sinon ?

@fatso83
Copy link
Contributor

fatso83 commented Jan 19, 2022

@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:

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 proxyquire, Jest, etc is for, as they change the runtime in order to do "magic" stuff. This is all totally different from project to project, so we cannot do much about this.

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!

@sinonjs sinonjs locked and limited conversation to collaborators Jan 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants