sinonjs / sinon

Test spies, stubs and mocks for JavaScript.
https://sinonjs.org/
Other
9.63k stars 769 forks source link

Sinon won't stub package's function #2460

Closed tal-rofe closed 2 years ago

tal-rofe commented 2 years ago

Describe the bug I try to stub the fs-extra/outputJson function, but it fails. My src code is a bit non-trivial. I have an abstract class, and then I try to test a class which extends this abstract class. My abstract class uses the outputJson function. I suspect that this use case leads to the issue.

To Reproduce You basically need to have an abstract class and then create an another class which extends the abstract one (in 2 different files to ensure reproduction). Then, implement a usage of the outputJson function in the abstract class, then test this function by creating instance of the extending class and use the abstract class` function.

Expected behavior outputJson should be stubbed

Context (please complete the following information):

I have the following abstract class which uses the "fs-extra" library:

import fs from 'fs-extra';

export default abstract class AppConfigService<T extends object> {
    public async setConfig(config: T) {
        await fs.outputJson(filePath, validatedConfig);
    }
}

This is only snippet as I don't find the rest of the class relevant.

Then I have the following class:

import path from 'path';

import AppConfigService from '../../services/app-config';
import ConstantsModule from '../constants';
import { UserAppConfigDto } from './classes/user-app-config.dto';
import { APP_CONFIG_FILE_NAME } from './models/user-app-config';

export default class UserAppConfigService extends AppConfigService<UserAppConfigDto> {
}

And then I try to test the last class:

import { doesNotReject, rejects } from 'assert';

import sinon from 'sinon';
import { expect } from 'chai';
import fs from 'fs-extra';

import UserAppConfigModule from '../../app/modules/user-app-config';
import UserAppConfigService from '../../app/modules/user-app-config/user-app-config.service';

describe('[modules/user-app-config.module.ts]', () => {
    const sandbox = sinon.createSandbox();

    afterEach(sandbox.restore);

    describe('setConfig()', () => {
        it('should throws when "fs-extra/outputJson" rejects', async () => {
            sandbox.stub(fs, 'outputJson').rejects();

            await rejects(UserAppConfigModule.service.setConfig({}));
        });
    });

});

But for some reason sinon does not stub fs-extra, the function outputJson. Why is that? I suspect the fs-extra/outputJson is not stubbed because of any context issues? My first test fails because the function wasn't stubbed

fatso83 commented 2 years ago

Thanks for taking the time to create this issue

Trying to debug a usage issue inside a project one does not have access to is very hard, and so I would suggest you do what we recommend: try to create a minimal reproducible testcase. That saves everyone a lot of time.

If you want to have a look at what that could look like, here is a number of examples:

Making it as easy as git clone foo/issue-323; npm i; npm t; makes it a lot more likely that you will get some help. Put in the effort and others will too! 🙌


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.