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

mock-interface

v1.0.3

Published

Zero dependency library that deeply mocks typescript interfaces via jest.fn() allowing you to adhere to easily SOLID principals.

Downloads

18

Readme

Mock Interface

Zero dependency library that deeply mocks typescript interfaces via jest.fn() allowing you to adhere to easily SOLID principals.

Type safety

Full type safety is preserved, type errors are thrown if a mock return/resolve type is incorrect or if method called incorrectly.

Natively Jest

Mock interfaces arbitrarily deeply, and they will behave as jest mocks.
All jest assertions will work as if it was a jest mock e.g. toHaveBeenCalledTimes, toHaveBeenCalledWith, ...
See full list from jest docs https://jestjs.io/docs/mock-function-api

Examples

describe('Mock Interface', () => {

  it('Should test person interface', () => {
    interface Person {
      speak: Speaker
      getName(): string
    }

    interface Speaker {
      say(phrase: string): string
    }

    const person = mockInterface<Person>()

    person.getName.mockReturnValue('Navi')
    person.speak.say.mockReturnValue('Hey, listen!')

    const name = person.getName()
    const say = person.speak.say('Hey, listen!')

    expect(name).toEqual('Navi')
    expect(person.getName).toHaveBeenCalledTimes(1)

    expect(say).toEqual('Hey, listen!')
    expect(person.speak.say).toHaveBeenNthCalledWith(1, 'Hey, listen!')
  })

  // This is typically an edge case of similar interface mockers that aren't caught
  it('Should mock toString function of interface', ()=>{
    interface JsonWriter{
      toString(): string
    }
    const mockJsonWriter = mockInterface<JsonWriter>()
    mockJsonWriter.toString.mockReturnValue('{"nice": "json"}')
    const w = mockJsonWriter.toString()
    expect(w).toBe('{"nice": "json"}')
    expect(mockJsonWriter.toString).toHaveBeenCalledTimes(1)
    expect(mockJsonWriter.toString).toHaveBeenCalledWith()
  })
  
  it('Should test arbitrarily deep interface', ()=>{
    interface l1 {
      l2: l2;
    }
    interface l2 {
      l3: l3;
    }
    interface l3 {
      l4: l4;
    }
    interface l4 {
      dig(): string;
    }

    const levels = mockInterface<l1>();
    levels.l2.l3.l4.dig.mockReturnValue('end of the line');

    const d = levels.l2.l3.l4.dig();
    expect(d).toBe('end of the line');
    expect(levels.l2.l3.l4.dig).toHaveBeenCalledTimes(1);
  })
})