Giter Site home page Giter Site logo

Comments (2)

marchaos avatar marchaos commented on July 28, 2024

Hey,

So it seems that you want to only allow a specific set of args to calledWith, otherwise you wan to fail the test:

Something like:

mockApi.get
            .calledWith(CORRECT_API, "/some-path", anyObject())
            .mockResolvedValue(response)
            .otherwise(fail('something'));

Am I following that right? As you mention, something like this might be hard in the case of promises as exception will get caught in the reject.

from jest-mock-extended.

atishnazir avatar atishnazir commented on July 28, 2024

Hi,
Yes; I'm quite used to the semantics of GoMock, TypeMoq, GoogleMock where you can put quite tight constraints on Mock behaviour. The Promise conversion issue is an annoyance, but it's really a issue of Jest that it uses an Assertion throw (there are workaround here by making an assertions re-entrant and getting expect to hook off that, I've slapped example at end).

Your illustrative API reads well, it's the sort of thing I'm looking for. In usage it might be ambigous what should happen in the test code without creating aggregating matchers:

mockAPI.get
            .calledWith(API, "/pricelist", anyObject())
            .mockResolvedValueOnce(pricelistResponse)
            .otherwise(fail("unexpected API call"));

mockAPI.get
            .calledWith(API, "/inventory", anyObject())
            .mockResolvedValueOnce(inventoryResponse)
            .otherwise(fail("unexpected API call"));

const value = sut.getTotalStockValue();

In the above example, my test should:

  • fail: /pricelist is invoked multiple times
  • fail: /inventory is invoked multiple times
  • fail: /somethingElse is invoked

Cheers.

Here's the gist of resurfacing swallowed assertions. It's a bit yuck:

describe("surfacing swallowed assertion hack", () => {
    let horribleHook: Error|undefined;

    function fauxExpect<T>(expression: T): jest.Matchers<T> {
        const result = expect(expression);

        if (horribleHook) {
            throw horribleHook
        }
        return result;
    }

    function fail(reason: string) {
        horribleHook = new AssertionError({message: reason});
        throw horribleHook;
    }

    function fauxMock() {
        fail("oh noes, I don't want to be called");
    }

    function sut(callMock: boolean): boolean {
        try {
            if (callMock) {
                fauxMock();
            }
        } catch(e) {
            console.warn("swallowing assertion");
        }
        return true;
    }

    beforeEach(() => {
        horribleHook = undefined;
    });

    test("example of swallowed assertion", () => {
        fauxExpect(sut(false)).toBeTruthy();
        fauxExpect(sut(true)).toBeTruthy();
    });
});

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.