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

omock

v1.0.0

Published

js tool for mocking objects

Downloads

1

Readme

EN RU

Omock is small set of methods for object property mocking. It is very conveniently for testing when you have dependence between modules

Why is it not jest __mock__ folder or analogue? Because mocking of export in separate folder is more complex in supporting. And hang of it realization have to keep in mind right along. Mocking directly in test case is more clear and readable.

Note! Omock use es6 Map, your test environment should have it Note! If you try mock no exist property then it will throw exception

Example:

//foo.js
export default {
  ten: () => 10,
  deferredTen: new Promise(resolve => setTimeout(() => resolve(10), 100),
}

// bar.js
import Foo from 'foo.js';

export default {
  twenty: () => Foo.ten() * 2,
  deferredTwenty: () => Foo.deferredTen().then(ten => ten * 2),
}

// bar.test.js
import Bar from 'bar.js';
import Foo from 'foo.js';
import { mockMethod, unmockAll, configureMock } from 'omock';

configureMock({ methodSpyCreator: jest.fn })

describe('Bar', () => {
  afterEach(() => {
    unmockAll();
  });
  it('twenty() by mock', () => {
    mock(Foo, 'ten', () => 100);

    expect(Bar.twenty()).toBe(200);
  });
  it('twenty() by mockMethod', () => {
    const mockedTen = mockMethod(Foo, 'ten', 100);

    expect(Bar.twenty()).toBe(200);
    expect(mockedTen.mock.calls.length).toBe(1);
  });
    it('deferredTwenty()', () => {
    const { method, promise } = mockAsyncMethod(Foo, 'deferredTen', 100);

    Bar.deferredTwenty();

    return promise.then(result => {
      expect(result).toBe(200);
      expect(method.mock.calls.length).toBe(1);
    });
  });
  ...
})

Api

  • configureMock - It is first configuration (optional).
  • mock - It is main method allowing substitute object property.
  • unmockAll - It reset to original all substituted property in all objects.
  • getOriginal - It return original value of object property.
  • mockMethod - It is conveniently way for substitute of object method
  • mockAsyncMethod - It is conveniently way for substitute of object method that return promise
  • mockAsyncMethodWithException - It is conveniently way for substitute of object method that return promise with specified exception

configureMock

It allow for mockMethod, mockAsyncMethod, mockAsyncMethodWithException methods set spy function like as jest.fn

Input:

  • Object: {     methodSpyCreator: (method: Function, ...args: Array) => SpiedFunction; }

methodSpyCreator - spy util for creating of mocked function like as jest.fn

Output: void

mock

It allow substitute object property to specified value

Input:

  • object: Object - target object
  • name: string - name of property
  • value: any - new value of property

Output: void

unmockAll

It allow reset all substitutions.

Input: void

Output: void

getOriginal

It return original value of object property.

Input:

  • object: Object - target object
  • name: string - name of property

mockMethod

It allow substitute result of object method.

Input:

  • object: Object - target object
  • name: string - name of object method
  • value: any - new result of object method

Output: substituting function, if spy creator is configured then function will wrapped by spy creator.

mockAsyncMethod

It allow substitute promise result of object async method.

Input:

  • object: Object - target object
  • name: string - name of object async method
  • value: any - new promise result of object method

Output: Object: {     method - substituting function, if spy creator is configured then function will wrapped by spy creator.     promise - returning promise by method }

mockAsyncMethodWithException

It allow substitute promise result of object async method as rejected.

Input:

  • object: Object - target object
  • name: string - name of object async method
  • error: any - value of exception

Output: Object: {     method - substituting function, if spy creator is configured then function will wrapped by spy creator.     promise - returning rejected promise by method }