Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 33 characters in body
Source Link
Lin Du
  • 103.8k
  • 139
  • 341
  • 576
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();
 });
});
const sinon = require('sinon');
const proxyquire = require('proxyquire');
describe('69759888', () => {
 it('should pass', async () => {
 const getCompanyStub = sinon.stub().resolves({});
 const Handler = proxyquire('./handler', {
 getCompany: getCompanyStub,
 });
 const handler = new Handler();
 await handler.init();
 });
});
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();
 });
});
Source Link
Lin Du
  • 103.8k
  • 139
  • 341
  • 576

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', {
 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 | 
------------|---------|----------|---------|---------|-------------------
lang-js

AltStyle によって変換されたページ (->オリジナル) /