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

ts-simple-mock

v0.0.3

Published

`ts-simple-mock` is a lightweight TypeScript library designed to simplify mocking and stubbing in unit tests. It provides a straightforward API for creating mock objects and defining behaviors for methods using matchers.

Downloads

8

Readme

ts-simple-mock

ts-simple-mock is a lightweight TypeScript library designed to simplify mocking and stubbing in unit tests. It provides a straightforward API for creating mock objects and defining behaviors for methods using matchers.

Installation

You can install ts-simple-mock using npm:

yarn add ts-simple-mock

Usage

Mocking Interfaces

To mock an interface, use the mock function and define the behavior using the when function with matchers such as any or eq.

import { when, mock, any, eq } from 'ts-simple-mock'

interface ExampleType {
  func1(id: string): string
  func2(id: string, str: string): string
}

// Mocking a method with any() matcher
const example = mock<ExampleType>()
when(example.func1, any()).thenReturn('hello world')
console.log(example.func1('anyId')) // Output: hello world

// Mocking a method with eq() matcher
when(example.func1, eq('hello')).thenReturn('world')
console.log(example.func1('hello')) // Output: world
console.log(example.func1('anotherId')) // Output: undefined (not matched)

Mocking Classes

To mock a class, use the mock function similarly. You can also mock specific instances of classes.

class ExampleClass {
  sayHello(name: string): string {
    return `Hello, ${name}!`
  }
}

// Mocking a method with any() matcher for a class
const exampleClassMock = mock<ExampleClass>()
when(exampleClassMock.sayHello, any()).thenReturn('Hi any!')
console.log(exampleClassMock.sayHello('World')) // Output: Hi any!

// Mocking a method with eq() matcher for a class
when(exampleClassMock.sayHello, eq('Guillaume')).thenReturn('Hi Guillaume')
console.log(exampleClassMock.sayHello('Guillaume')) // Output: Hi Guillaume
console.log(exampleClassMock.sayHello('World')) // Output: undefined (not matched)

Using Real Methods

If you want to use the real methods of a class instance when not mocked, you can pass the instance to the mock function.

onst example = new ExampleClass()
const mockExample = mock<ExampleClass>(example)

// Using the real method if not mocked
console.log(mockExample.sayHello('World')) // Output: Hello, World!

// Mocking a specific method
when(mockExample.sayHello, eq('Guillaume')).thenReturn('Hi Guillaume')
console.log(mockExample.sayHello('Guillaume')) // Output: Hi Guillaume
console.log(mockExample.sayHello('World')) // Output: Hello, World! (real method)

API

mock(instance?: T): T

Creates a mock object for the given type T. If an instance is provided, the mock will use the real methods when not explicitly mocked.

when(method: (...args: any[]) => T, matcher: Matcher): Stubbing

Defines a behavior for a mocked method.

Matchers

- any(): Matches any argument.
- eq(value: any): Matches an argument that equals the specified value.

Stubbing

Stubbing allows you to define what the mock should return when the method is called with the specified matcher.

- thenReturn(value: T): Specifies the value to return when the method is called.
- thenThrowError(exception: string): Throw exception when the method is called

Running Tests

To run the tests for ts-simple-mock, use a test runner like Jest. Make sure to include the necessary test setup to support TypeScript.

yarn run test