2

My lwc has an input and a button. When I press the button an apex call with the input's value is made. Even though the method is called with the right params, the return value is some sort of empty object

const APEX_OBJECTS_LIST = require("./data/objectsList.json");

jest.mock(
  "@salesforce/apex/ObjectController.findObject",
  () => {
    return { default: jest.fn() };
  },
  { virtual: true }
);

async function flushPromises() {
  return Promise.resolve();
}
---
findObject.mockResolvedValue(APEX_OBJECTS_LIST);
const buttonEl = element.shadowRoot.querySelector("lightning-button");
buttonEl.click();

await flushPromises();

expect(findObject).toHaveBeenCalledWith({ searchKey: "OBJ-123456" });
expect(findObject).toHaveReturnedWith(APEX_OBJECTS_LIST);
# Error
expect(jest.fn()).toHaveReturnedWith(expected)

Expected: APEX_OBJECTS_LIST
Received: {}

The most helpful answer I found was from this question. I also test other tags that are populated with the mocked list and they are successful. This means that the mocked object does come through but is not "cough" at toHaveReturnedWith().

If I try toHaveReturnedWith({}) the error is the same, meaning that what is received is not an empty object after all.

1 Answer 1

0

The jest documentation has a .resolves/.rejects testing section. Testing this out, it seems the resolves.toBe() can help you assert that your resolved promise is what you provided

expect(findObject()).resolves.toBe(APEX_OBJECTS_LIST)

If the above were to fail, you'll notice the following would be the error

JestAssertionError: expect(received).resolves.toBe(expected)

Looking at your error message, you can see it's referencing the mock implementation: jest.fn().

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .