mercredi 19 juillet 2017

How can I stub an anonymous function in Sinon?

The following:

const sinon = require('sinon')

const a = () => { return 1 }
sinon.stub(a)

throws TypeError: Attempted to wrap undefined property undefined as function.

stub works if there is an object, so I tried using this. In node.js REPL:

> const a = () => { return 1 }
undefined
> this.a
[Function: a]

However, in my mocha spec, it fails:

const a = () => { return 1 }                                        

console.log(a)
// => [Function: a]

console.log(this.a)
// => undefined

What am I missing? How can I make this work?

Aucun commentaire:

Enregistrer un commentaire