Socket
Socket
Sign inDemoInstall

@hexancore/mocker

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hexancore/mocker

Simple and magical mocks for TypeScript, works with jest and vitest


Version published
Weekly downloads
154
decreased by-17.65%
Maintainers
1
Install size
18.5 kB
Created
Weekly downloads
 

Readme

Source

Mocker

Two words can describe it: simple and magical :) Helper stuff to create mocks in TypeScript in Jest or Vitest

You just need it!

Usage:

In Jest

import { M, mock } from '@hexancore/mocker';

In Vitest

import { M, mock } from '@hexancore/mocker/vitest';

Example

import { M, mock } from '@hexancore/mocker';

interface TestMock {
  a(param1: string, param2: boolean): boolean;
  b(param1: string, param2: boolean): boolean;
}

describe('Example test suite', () => {
  let m: M<TestMock>; // Define mock variable with your class or interface wrapped with `M` type

  beforeEach(() => {
    // in beforeEach create `Mocker` with mock() factory method
    // you can simply mock interface or class and give it descriptive name(used in errors)
    m = mock('my_mock');
  });

  afterEach(() => {
    // after execute your code, you can check sets expectations results with it(for many tests call it in jest "afterEach")
    mock.checkExpections();
  });

  test('Example test', () => {
    // define method call expectation(available methods list will be shows in VS + it will hint all parameters and return type )
    m.expects('a', 'test', true).andReturn(true);

    // mock "i" property is object of mocked class, pass it where you need
    m.i.a('test', true);
  });

Defining expectation

Mocker::expects(method, ...args) returns MethodMock object with you can define return value by:

  • andReturnWith((implementation: (...args: any) => any) - you can define your own method implementation
  • andReturn(value: any) - define returns passed value once
  • andReturnResolved - simple sugar function for andReturnWith((() => Promise.resolve(value))

Using Jest.expect matchers in Mocker.expects

let userRepository = mock<UserRepository>();
userRepository
      .expects('save', expect.objectContaining({ email, username, password: hashedPassword }))
      .andReturn(OKA(true));

Mockers

Manages a group of defined mocks in a more efficient way of writing test code and simplifing injecting many mocks to class object.

For use add to your tsconfig.json

 "compilerOptions": {
    "useDefineForClassFields": true,
 }

Example

import { mocks } from '@hexancore/mocker';

class TestMock {
  public method(a: number): number {
    return a * 2;
  }
}

class TestClassConstructor {
  public constructor(public mock1: TestMock, public readonly b: number, public mock2: TestMock) {}

  public method(): number {
    return mock1.method(this.b)+mock2.method(this.b);
  }
}

describe('Mockers', () => {
  // define variable using `mocks` and anonymous class with mocks you want create
  let m = mocks(
    new (class {
      mock1: TestMock;
      mock2: TestMock;
    })(),
  );

  // variable to create instance with injected mocks
  let testClass: TestClassConstructor;


  beforeEach(() => {
    // use xFresh() to get new Mockers instance
    // methods of Mockers using `x` prefix for be after your mocks names on VS list
    m = m.xFresh();

    // you can create new instance of your class with injected mocks and other parameters
    testClass = m.xNewInject(TestClassConstructor, { b: 10 });

    // other non mock parameters can be given as object or array
    // testClass = m.xNewInject(TestClassConstructor, [10]);
  });

  afterEach(() => {
    m.xCheckExpections(); // checks all mocks expections
  });

  test('test()', () => {
    m.mock1.expects('method', 10).andReturn(5);
    m.mock2.expects('method', 10).andReturn(6);

    const current = testClass.method();
  });

Keywords

FAQs

Last updated on 06 Nov 2023

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc