Sinon error Attempted to wrap function which is already wrapped

前端 未结 9 636
滥情空心
滥情空心 2020-12-24 04:01

Though there is a same question here but I could not find answer to my problem so here goes my question:

I am testing my node js app using mocha and chai. I am usin

9条回答
  •  离开以前
    2020-12-24 04:58

    I was also hitting this using the before() and after() hooks of Mocha. I was also using the restore() as mentioned everywhere. Single test file ran fine, multiple did not. Finally found about Mocha root-level-hooks: I did not have my before() and after() inside my own describe(). So it finds all files with before() at the root-level and executes those before starting any tests.

    So make sure you have a similar pattern:

    describe('my own describe', () => {
      before(() => {
        // setup stub code here
        sinon.stub(myObj, 'myFunc').callsFake(() => {
          return 'bla';
        });
      });
      after(() => {
        myObj.myFunc.restore();
      });
      it('Do some testing now', () => {
        expect(myObj.myFunc()).to.be.equal('bla');
      });
    });
    

提交回复
热议问题