Coder Social home page Coder Social logo

Comments (2)

wihd avatar wihd commented on May 22, 2024 6

Although it would be better to integrate support for getters into the library, it is possible to mock a getter whilst using jest-mock-extended. Suppose you have an interface like this.

interface Strings {
    readonly size: number;
    item(index: number): string;
}

Just creating a mock for this interface using mock<Strings> will allow you to mock the item method. But since the type of size is not a function, an expression like mockObject.size.mockReturnValue(3) is a type error since you cannot call a method on an integer value.

However it is possible to define a property that uses a getter function directly on the implementation object that jest-mock-extended uses to record the state for its mock object. We need to pass an empty object into the mock<>() function so that we get a reference to this object. Consider the following code:

    // Construct the mockers
    const baseMock = {};
    const mockStrings = mock<Strings>(baseMock);
    const mockSizeGetter = jest.fn<number, []>();
    Object.defineProperty(baseMock, 'size', { get: mockSizeGetter });

This code creates an object mockStrings that mocks the Strings interface. The jest mock function mockSizeGetter is set to be the getter function for the size property. You have to call defineProperty on the baseMock object after creating the mocker. Otherwise the mock<>() function will attempt to alter it, which does not work since there is no setter function.

Now we can prepare the mockers to pretend to be a collection of three strings.

    mockSizeGetter.mockReturnValue(3);
    mockStrings.item
        .mockReturnValueOnce('zero')
        .mockReturnValueOnce('one')
        .mockReturnValueOnce('two');

The following code demonstrates that the getter have been mocked.

    const concatenate = (strings: Strings): string => {
        let result = '';
        for (let i = 0; i < strings.size; ++i) {
            if (i > 0) {
                result += ',';
            }
            result += strings.item(i);
        }
        return result;
    }

    expect(concatenate(mockStrings)).toEqual('zero,one,two');

from jest-mock-extended.

lebeier avatar lebeier commented on May 22, 2024

Thanks for this example. While I am not working with any projects that are using jest framework at the moment, I will give it a try in the future. =)

from jest-mock-extended.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.