0

I have a class modules/handler.js, which looks like this:

const {getCompany} = require('./helper');
module.exports = class Handler {
 constructor () {...}
 async init(){
 await getCompany(){
 ...
 }
}

it imports the function getCompany from the file modules/helper.js:

exports.getCompany = async () => {
 // async calls
}

Now in an integration test, I want to stub the getCompany method, and it should just return a mockCompany. However, proxyquire is not stubbing the method getCompany, instead the real ones gets called. The test:

const sinon = require('sinon');
const proxyquire = require("proxyquire");
const Handler = require('../modules/handler');
describe('...', () => {
 const getCompanyStub = sinon.stub();
 getCompanyStub.resolves({...});
 const test = proxyquire('../modules/handler.js'), {
 getCompany: getCompanyStub
 });
 it('...', async () => {
 const handler = new Handler();
 await handler.init(); // <- calls real method 
 ... 
 });
});

I've also tried it out without the sinon.stub where proxyquire returns a function directly returning an object, however, this also did not work.

I would be very thankful for every pointer. Thanks.

asked Oct 28, 2021 at 19:33

1 Answer 1

1

The Handler class you are using is required by the require function rather than proxyquire.

handler.js:

const { getCompany } = require('./helper');
module.exports = class Handler {
 async init() {
 await getCompany();
 }
};

helper.js:

exports.getCompany = async () => {
 // async calls
};

handler.test.js:

const sinon = require('sinon');
const proxyquire = require('proxyquire');
describe('69759888', () => {
 it('should pass', async () => {
 const getCompanyStub = sinon.stub().resolves({});
 const Handler = proxyquire('./handler', {
 './helper': {
 getCompany: getCompanyStub,
 },
 });
 const handler = new Handler();
 await handler.init();
 });
});

test result:

 69759888
 ✓ should pass (2478ms)
 1 passing (2s)
------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 | 
 handler.js | 100 | 100 | 100 | 100 | 
 helper.js | 100 | 100 | 100 | 100 | 
------------|---------|----------|---------|---------|-------------------
answered Oct 29, 2021 at 2:52
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for the reply! Thanks to you I was able to pinpoint the problem. Though using const Handler = proxyquire('../modules/handler', { getCompany: getCompanyStub, }); did not work for me, but const Handler = proxyquire('../modules/handler', { './helpers' : { getCompany: getCompanyStub},}); worked. This seems odd to me, but I'll take it.
@seveneights Sorry, I have a typo. Updated answer. You are correct

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.