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

@hexancore/mocker

v1.2.0

Published

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

Downloads

371

Readme

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
  • andReturnThis() - define returns this
  • andReturnResolved - simple sugar function for andReturnWith((() => Promise.resolve(value))
  • andReturnRejected - simple sugar function for andReturnWith((() => Promise.reject(error))

Using Jest.expect matchers in Mocker.expects

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

Reset

Mock expectations can be reset with Mocker.reset() or with passed true to Mocker.checkExpections(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();
  });