简体   繁体   中英

Sinon: Mock an internal function which is called by named export

I have to mock an internally called function but the function I'm testing is exported using named export in typescript.

import { internalFunc } from './internal.ts';

const funcToTest = () => {
  internalFunc();   // I need to mock this function
}

export {
  funcToTest
}

Now my test file looks like this,

import { describe } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';

import { funcToTest } from './myModule.ts';

describe ('something meaningful', () => {
  it ('should pass', () => {
    sinon.stub();         // I'm stuck here. How do I mock this internalFunc()?
    let result = funcToTest();
  }
}

Can you please suggest a way to mock the internalFunc() method?

Well, I've found a way myself. Not sure if this is the right way to solve this.

import { describe } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';

import { funcToTest } from './myModule.ts';
import * as internal from './internal.ts';

describe ('something meaningful', () => {
  it ('should pass', () => {
    sinon.stub(internal, 'internalFunc').returns('some value');
    let result = funcToTest();
  }
}

If anyone found a better way to mock this internalFunc that would be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM