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

jest-mock-props

v1.9.1

Published

Mock module and object properties

Downloads

3,033

Readme

Jest Mock Props

Dependabot badge Dependencies Build Status Coverage Status NPM Version

Extends jest to allow easy mocking of object and module properties.

Introduction

Getting Started

Install the extension using npm or yarn

npm install -D 'jest-mock-props'

API Reference

Mock Properties

Mock properties are "spies" that let you control the behavior of a property that is accessed indirectly by some other code.

These are the methods available on every mocked property spy object.

spy.mockClear()

Equivalent to mockReset.

spy.mockReset()

Removes any mocked values.

This is useful when you want to completely reset a property back to its initial value.

spy.mockRestore()

Restores the original (non-mocked) value.

This is useful when you want to mock properties in certain test cases and restore the original value in others.

spy.mockValue(value)

Accepts a value that should be result of accessing the mocked property.

Note: This is the same function used when setting the mocked property directly; e.g. obj.mockedProp = 'newValue'

spy.mockValueOnce(value)

Accepts a value that will be result of a single access to the mocked property. Can be chained so that multiple accesses produce different results. See example.

Note: When the mocked property runs out of values defined with mockValueOnce, it will have the default value set with obj.mockedProp = 'defaultValue' or spy.mockValue(defaultValue).

The Jest Object

The jest object needs to be extended in every test file. This allows mocked properties to be reset and restored with jest.resetAllMocks and jest.restoreAllMocks respectively.

jest.isMockProp(object, propertyName)

Determines if the given object property has been mocked.

jest.spyOnProp(object, propertyName)

Note: This is aliased as jest.spyOn as of v1.9.0, overriding the existing jest.spyOn to use spyOnProp when spying on a regular object property.

Creates a mock property attached to object[propertyName] and returns a mock property spy object, which controls all access to the object property. Repeating spying on the same object property will return the same mocked property spy.

Note: By default, spyOnProp preserves the object property value. If you want to overwrite the original value, you can use jest.spyOnProp(object, propertyName).mockValue(customValue) or jest.spyOn(object, methodName, accessType?) to spy on a getter or a setter.

Example

video.js

const video = {
    length: 1000,
};

module.exports = video;

video.test.js

import * as mockProps from "jest-mock-props";
mockProps.extend(jest);

const video = require("./video");

it("mocks video length", () => {
    const spy = jest.spyOn(video, "length");
    spy.mockValueOnce(200)
        .mockValueOnce(400)
        .mockValueOnce(600);
    expect(video.length).toEqual(200);
    expect(video.length).toEqual(400);

    video.length = 800;
    expect(video.length).toEqual(800);

    spy.mockReset();
    expect(video.length).toEqual(1000);
    expect(jest.isMockProp(video, "length")).toBe(true);

    spy.mockRestore();
    expect(jest.isMockProp(video, "length")).toBe(false);
});