3.1.4 • Published 2 years ago

simple-ts-mock v3.1.4

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

simple-ts-mock


Description

This lightweight, testing framework agnostic, package provides an easy way to mock interfaces and classes within your Typescript and Javascript projects. Inspired by the .NET package MOQ, .NET developers will find this package quite familiar. Will work with Mocha, Jest, Jasmine, or any other framework you use for writing unit tests!


Example Usage

Example classes

export class UserService {
    constructor() {}

    getUser(userId: int): Promise<User> {
        return Promise.resolve(new User());
    }
}

export class UserProvider {
    private readonly _userService: UserService;

    constructor(userService = new UserService()) {
        this._userService = userService;
    }

    async doUserStuff(userId: int): Promise<boolean> {
        const user = await this._userService.getUser(userId);

        return user ? true : false;
    }
}

Example testing: (Jest's syntax is used in this example, but you can use any other framework the same way.)

import Mock from "simple-js-mock";
import { UserProvider, UserService } from "./user";

test("UserProvider returns true when user retrieved successfully", async () => {
    // Arrange
    const mockService = new Mock<UserService>();
    const userProvider = new UserProvider(mockService.object());
    const expected = true;
    mockService.setup((s) => s.getUser()).returnsAsync(new User());

    // Act
    const actual = await userProvider.doUserStuff(123);

    // Assert
    expect(mockService.getCallCount((s) => s.getUser())).toEqual(1);
    expect(actual).toEqual(expected);
});

API

Mock\<T>

ConfigurableMock

3.1.3

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.4

2 years ago

2.4.3

2 years ago

2.4.4

2 years ago

3.0.0

2 years ago

2.4.2

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago