Sinon CallFake在其他文件中返回相同的输出

时间:2018-07-16 11:17:21

标签: javascript node.js sinon sinon-chai

我的模块文件包含以下代码

每当承诺被拒绝时process.exit都将用代码1调用

module.exports = myPromiseReturningFunc(args)
.then(el => {
  return true;
})
.catch(err => {
  console.error('Error ', err)
  return process.exit(1);
})

// here is my index.test.js
const sinon = require('sinon');
describe('Index', () => {
  let exitStub;
  beforeEach(done => {  
    exitStub = sinon.stub(process, 'exit').callsFake(() => {
      return 'error'
    })
  })
  afterEach(done => {
    process.exit.restore();
  })

  it('should exit with error', (done) => {
    myModuleFunction(args).then(result => {
      expect(result).to.be.eql('error'); // passed here
      done();
    })
  })
})

// another test file
describe('Help Module', () => {
  it('should return true', (done) => {
    myModuleFunction({}).then(result => {
      console.log(result); // prints error here again :-(
      expect(result).to.be.true;
    })
  })
})

我想对过程进行一次存根,然后退出,但是尽管其他函数返回成功响应,它也会在其他文件中返回相同的错误

0 个答案:

没有答案