Mocking named imports and constructors ES6 & Ava

1.4k Views Asked by At

I have a class constructor, with a function I want to stub:

class Service {
  constructor(){}
  async someFunction() {
    try {
    // does stuff
    }
    catch (e) {}
  }
}

In the file I want to test, this is imported an used like so:

const { Service } = require('something')
const newService = new Service('xyz')

I'm struggling to get this to import & stub correctly in my tests.

Currently am importing like this:

t.context.service = {
  Service: class Service {
    constructor () {
      this.someFunction = sinon.stub()
    }
  }
}

This import seems to work, but then I can't get a reference back to it through the constructed version. Any help on this one?

I want to be able to make an assertion like:

t.true(t.context.service.Service.someFunction.calledOnce)
1

There are 1 best solutions below

1
On

AVA doesn't provide any stubbing. Have a look at https://github.com/testdouble/testdouble.js/ or http://sinonjs.org/.