0

**edit: I restored the mocked function after test1, using sinon.restore(), but again, it feels unnecessary to restore all mocks when I just want to restore one.

Just wondering when and how I should remove this mocked module?

It's only needed for test1, and needs to be removed (or at least changed) for test2.

  • afterEach might work I guess, but it doesn't need to be reset after every test, just one. ~
  • I could move test1 into its own describe block, but this seems a bit unnecessary
  • I could just remove the mock in the describe block itself, but I'm not sure if that would work as expected
  • I could just change the mockedFn, but again, I'm not sure where best to do it

The docs also say that I should call proxyquire.del('../../utils/validation') to remove the overrides, but this results in an error: TypeError: proxyquire.del is not a function, which seems a bit odd?

   const proxyquire = require('proxyquire');
   let validationModule;
 
   describe('Unit Test Examples', () => {
        // Module/function mocked before test1
        const mockedFn = sinon.stub();
        mockedFn.returns(null);
    
        validationModule = proxyquire('../../utils/validation', {
          '../models/Person': {
            findOne: mockedFn
           }
        });
        
        it('test1', () => {});

        // Removed after test1?

        it('test2', () => {});  
    })
Lin Du
  • 88,126
  • 95
  • 281
  • 483
NickW
  • 1,207
  • 1
  • 12
  • 52

0 Answers0