New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

interface-mock

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

interface-mock

C#-like API for creating mock in TypeScript (Node.js)

latest
Source
npmnpm
Version
1.2.0
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

npm version tests

Interface Mock

C#-like API for creating mock in TypeScript for Node.js environment

Installation

npm install --save-dev interface-mock

or

yarn add --dev interface-mock

Usage

Create mock by passing interface to it and providing default implementation, then call .verify() method to check whether the method was called. Additionally you can specify how many times it should have been called

import { createInterfaceMock } from 'interface-mock';

interface IBus {
  send: (message: string) => Promise<void>;
}

describe('SomeTest', () => {
  it('works', () => {
    const busMock = createInterfaceMock<IBus>({ send: async () => {} });
    const someService = new SomeService(busMock);

    someService.execute();

    busMock.verify(m => m.send('Hello'));
  });

  it('works 2', () => {
    const busMock = createInterfaceMock<IBus>({ send: async () => {} });
    const someService = new SomeService(busMock);

    someService.execute();

    busMock.verify(m => m.send('Hello'), { times: 2 });
  });
});

Additionally you can manually specify the resulting type of createInterfaceMock function:

import { createInterfaceMock, InterfaceMock } from 'interface-mock';

interface IBus {
  send: (message: string) => Promise<void>;
}

describe('SomeTest', () => {
  let busMock: InterfaceMock<IBus>;

  beforeEach(() => {
    busMock = createInterfaceMock<IBus>({ send: async () => {} });
  });

  it('works', () => {
    const someService = new SomeService(busMock);

    someService.execute();

    busMock.verify(m => m.send('Hello'));
  });
});

Keywords

test

FAQs

Package last updated on 30 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts