2.0.1 โ€ข Published 6 years ago

stubbable-decorator v2.0.1

Weekly downloads
2
License
MIT
Repository
-
Last release
6 years ago

stubbable-decorator

A decorator to make possible to stub modules in ECMAScript 2015+.

Installation

npm install -S stubbable-decorator

Usage

With a class

//
// Module declaration
//

import stubbable from 'stubbable-decorator'

class Foo {
  constructor(bar) {
    this.bar = bar
  }
}

export default stubbable(Foo)
//
// Testing
//

Foo.stub = sinon.stub().returns({})
const obj = new Foo()
expect(VHS.api.Poller.stub).calledOnce // ๐Ÿ‘

With a function

//
// Module declaration
//

import stubbable from 'stubbable-decorator'

function foo() { return 123 }

export default stubbable(foo)
//
// Testing
//

foo.stub = sinon.stub().returns(321)
const result = foo()
expect(result).to.be.equal(321) // ๐Ÿ‘

With a decorator

In the current spec it is only possible to decorate classes and classes properties.

//
// Module declaration
//

import stubbable from 'stubbable-decorator'

@stubbable
class Foo {
  constructor(bar) {
    this.bar = bar
  }
}

export default Foo
//
// Testing
//

Foo.stub = sinon.stub().returns({})
const obj = new Foo()
expect(VHS.api.Poller.stub).calledOnce // ๐Ÿ‘

Credits

  • Icon by Sergey Demushkin from The Noun Project

caiogondim.com ย ยทย  GitHub @caiogondim ย ยทย  Twitter @caio_gondim