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

tsmockit

v2.0.8

Published

Generic mocking library for TypeScript

Downloads

1,459

Readme

tsmockit

Generic mocking library for TypeScript

CI

Helpful links:

This library exposes a generic class, Mock<T>, which allows you to mock dependencies and verify usage in an intuitive and type safe manner. Its API is based on the C# "moq" library.

The below example demonstrates some of the features of this library. Please explore the "docs" linked above for further details.

// https://github.com/bayes343/tsmockit/blob/master/src/Mock/tests/Mock-Car-Example.spec.ts

interface IEngine {
  Start(): void;
  Stop(): void;
}

interface IOdometer {
  GetMileage(): number;
}

interface IStereo {
  SetStation(frequency: number): string;
}

class Car {
  private poweredOn = false;
  public get PoweredOn(): boolean {
    return this.poweredOn;
  }

  public get Mileage(): number {
    return this.odometer.GetMileage();
  }

  private engine: IEngine;
  private odometer: IOdometer;
  private stereo: IStereo;

  constructor(
    engine: IEngine,
    odometer: IOdometer,
    stereo: IStereo
  ) {
    this.engine = engine;
    this.odometer = odometer;
    this.stereo = stereo;
  }

  public StartEngine(): void {
    this.engine.Start();
    this.poweredOn = true;
  }

  public StopEngine(): void {
    this.engine.Stop();
    this.poweredOn = false;
  }

  public ChangeRadioStation(frequency: number): string {
    return this.stereo.SetStation(frequency);
  }
}

describe('Car', () => {
  let car: Car;
  const mockIEngine = new Mock<IEngine>();
  const mockIOdometer = new Mock<IOdometer>();
  const mockIStereo = new Mock<IStereo>();

  beforeEach(() => {
    car = new Car(mockIEngine.Object, mockIOdometer.Object, mockIStereo.Object);
  });

  it('should call Engine.Start when StartEngine is called', () => {
    mockIEngine.Setup(e => e.Start());
    car.StartEngine();
    mockIEngine.Verify(e => e.Start(), Times.Once);
  });

  it('should call Engine.Stop when StopEngine is called', () => {
    mockIEngine.Setup(e => e.Stop());
    car.StopEngine();
    mockIEngine.Verify(e => e.Stop(), Times.Once);
  });

  it('should return the result of Odometer.GetMileage on referencing the Mileage property', () => {
    mockIOdometer.Setup(o => o.GetMileage(), 100);

    const mileage = car.Mileage;

    expect(mileage).toEqual(100);
    mockIOdometer.Verify(o => o.GetMileage(), Times.Once);
  });

  it('should call Stereo.SetStation on calling ChangeRadioStation returning the string from Stereo', () => {
    mockIStereo.Setup(s => s.SetStation(Any<number>()), 'Station set'); // default fallback setup when a more specific setup isn't available
    mockIStereo.Setup(s => s.SetStation(3), 'Station 3');

    expect(car.ChangeRadioStation(3)).toEqual('Station 3');
    expect(car.ChangeRadioStation(0)).toEqual('Station set');
    expect(car.ChangeRadioStation(2)).toEqual('Station set');

    mockIStereo.Verify(s => s.SetStation(3), Times.Once);
    mockIStereo.Verify(s => s.SetStation(Any<number>()), 2);
  });
});

Version 2 notes

  • Any<T>
    • A helper function which allows clients to create "Setups" on methods to be used disregarding all, or some, of the exact values the method is called with.
      mockIStereo.Setup(s => s.SetStation(Any<number>()), 'Station set');
  • SetupOnce
    • Creates a setup the same as the regular Setup method except for once the setup is used, it will de-register itself.
    • One use could be an inferred guarantee that a given method on a dependency is not being called more than you expect. Using SetupOnce for a setup that should only be used once, will have the inherent effect of leading to a runtime error at test time on the second execution.
  • SetupSequence
    • Creates several, "one time" setups for a given method.
    • You'll occasionally want the first execution of a given method on a dependency to return "x" but then "y" on the following execution. Use SetupSequence to achieve this.