npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-class-mock

v0.0.7

Published

A simple means to mock mathods within the prototype of ECMA6 classes ( or the transposed expression of such classes )

Downloads

3

Readme

node-class-mock

Allows you to mock Class methods for use in testing with a few extra testy bits thrown in.

Example

This will replace the method on our TestClass prototype to a method which firstly checks the arguments passed to it and will assert.fail in the event of errors. It will then return a resolved Promise with the given output.


    // First, instantiate ClassMock with the class you would like to mock
    const classMock = new ClassMock( TestClass );

    // Then replace one of the methods
    const methodMock = classMock.mock( 'myMethod' )
        .shouldHaveArguments([
            { type: 'string', value: 'Hello, World!' },
            { value: 1 },
            { type: 'string', value: 'Sausages', optional: true }
        ])
        .resolves( 'Such Promise resolution!' )

    // Then anything which calls your mocked-method will
    // have it's inputs checked and receieve a resolved promise
    // ( other outcomes are available )
    const myTestClass = new TestClass();
    myTestClass.doStuff( 'Hello, World!', 1 ).then(
        out => console.log( 'Hey, my test works!' )
    );

    // Finally, remember to un-mock!
    methodMock.unMock();

To start-a-mocking, simply instantiate a ClassMock with your desired class ( non instantiated ). The ClassMock object exposes the following methods...

ClassMock

This can be considered the mock factory or the mock father or the mock-mack-daddy. Having instantiated it with your desired class, it will produce little method mock children.

  • mock

    Main entry for overriding methods

    Arguments

    • methodName [Stirng] - Name of class method to override
    • callback [Function, Optional] - Function to use as mock
    • context [Object, Optional] - An instantiated object which acts as this in your override method

    Returns

    • methodMock [Object] - A representation of the mock dynamic
  • unMock

    Reinstates original method

    Arguments

    • methodName [String] - Name of method to reinstate
  • unMockAll

    Reinstate all mocked methods

Method Mocking

When calling mock on the mock-father you will receive back an object which exposes these methods...

  • shouldHaveArguments

    Provide a list of expected arguments, assert.fail if any are amiss

    Arguments

    • argumentDescriptorList [Array|Object] - A list of objects describing each expected parameter

      • value - The expected value
      • type - The typeof the argument
      • optional - Some arguments are optional
  • resolves

    The overriden method will result in a resolved Promise being returned with the given value.

    Arguments

    • output - Value to resolve
  • rejects

    The overriden method will result in a rejected Promise being returned with the given error.

    Arguments

    • error - Value to reject
  • returns

    The overriden method will result in the specified value being returned.

    Arguments

    • output - Value to reject
  • throws

    The overriden method will result in the given error being thrown

    Arguments

    • error - Value to throw
  • unMock

    Reinstate the original function

To develop

npm install
npm test

To compile

babel src/class-mock.js -o lib/class-mock.js

... to be continued