Socket
Socket
Sign inDemoInstall

classtubber

Package Overview
Dependencies
27
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    classtubber

Stubs a class and returns references to methods on instances of said class


Version published
Maintainers
1
Created

Readme

Source

classtubber

Stubs a class and returns references to methods on instances of said class

This is useful when stubbing a class before it's instanciated and you don't have access to it from the test itself. I would anticipate you'd usually use this with injectr/mockery/proxyquire.

class TestClass {
  testMethod() {
    // does something
  }
  anotherTestMethod() {
    // does something else
  }
}

[
  stubbedTestClass,
  stubReference,
] = createStubbedClass(TestClass, { testMethod: myCustomStub })

stubbedTestClass is now a stubbed function/class. When stubbedTestClass is instanciated it will modify the testclass property on the stubReference to be the instance.

All methods on the class will be stubbed. You can pass in your own stubs if you'd like specific behaviour. You can then access methods on the instance and assert against them. e.g. `expect(stubReference.testclass.testMethod).to.have.been.called()``

You can also make assertions against the class itself as that is a spy. This is useful for checking a class has been instanciated correctly. e.g. `expect(stubbedTestClass).to.have.been.calledWith({foo: 'bar'})``

Keywords

FAQs

Last updated on 07 Feb 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc