Replacing modules with testdouble.js

In notebook:
Work Notes
Created at:
2017-11-21
Updated:
2017-11-21
Tags:

I have a ./utils/fs.js file that is imported by saveImage.js.

Here's how I create a stub for it with testdouble, if I also want to verify the calling of a side-effect (createDir)

  describe('saving an image', () => {
  let subject, fakefs
  
  after(td.reset) // more reliable for me than afterEach with Wallaby.js
  
  before('set up stubs', () => {
    fakefs = td.object({
      createDir: td.function('createDir') // creates side effect
    })
    td.when(fakefs.createDir('shoes')).thenReturn('pants')
    
    const fs = td.replace('./utils/fs', fakefs)
    
    subject = require('./saveImage')  
  })
  
  // then verify
  describe('side effect created', () => {
    before(() => {
      subject('shoes')
    })
    it('runs the side effect', () => {
      td.verify(fakefs.createDir('shoes'))
    })
  })
  
})

If I don't care about verifying the calling of createDir, then it's even simpler:

  const fs = td.replace('./utils/fs')

td.when(fs.createDir()).thenReturn('pants')