You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mock objects do not work as I expect with expect.anything() (which should match any non-null value) or expect.any() (which should match a given prototype).
Mock objects are evaluated as undefined in these contexts.
Example:
classMyClass{someProperty: string;}constmyClassMock=mock<MyClass>();constspyFn=jest.fn();spyFn(myClassMock);// These all pass:expect(myClassMock).not.toBeFalsy();expect(myClassMock).not.toEqual(undefined);// These all fail:expect(myClassMock).toEqual(expect.any(MyClass));// Expected: Any<MyClass>, Received: undefinedexpect(myClassMock).toEqual(expect.anything());// Expected: Anything, Received: undefinedexpect(spyFn).toBeCalledWith(expect.anything());// Expected: Anything, Received: undefined
To give a bit more detail about what I'm trying to achieve, I want to assert a call to a service using toBeCalledWith. The method call uses some values that I want to assert, and some mock objects (returned by mocked dependencies) which I don't care about. e.g.
dependency=mock<Dependency>();dependency.getKey.mockResolvedValue(mock<Key>());// assume that sut calls dependency.getKey and passes to uploadServicesut.someMethod("testValue");expect(uploadService.upload).toHaveBeenCalledWith(expect.anything(),"testValue");
Is this a misuse of the jest-mock-extended mock objects, or is there something I'm missing in how I'm trying to use them?
PS. love the library otherwise, it's a much needed bridge between jest and TS. Thanks for your work.
EDIT: the same issue occurs when using the jest-mock-extended any() matcher. I guess the issue is more how it's evaluated in the toBeCalledWith/toEqual methods than the specific matcher used within those methods.
The text was updated successfully, but these errors were encountered:
eliykat
changed the title
Mocks are undefined in some expect statements
Mocks evaluate to undefined in some expect statements
Oct 10, 2022
eliykat
changed the title
Mocks evaluate to undefined in some expect statements
Mocks evaluate to undefined when used with expect.anything() or expect.any()
Oct 10, 2022
Mock objects do not work as I expect with expect.anything() (which should match any non-null value) or expect.any() (which should match a given prototype).
Mock objects are evaluated as
undefined
in these contexts.Example:
To give a bit more detail about what I'm trying to achieve, I want to assert a call to a service using
toBeCalledWith
. The method call uses some values that I want to assert, and some mock objects (returned by mocked dependencies) which I don't care about. e.g.Is this a misuse of the jest-mock-extended mock objects, or is there something I'm missing in how I'm trying to use them?
PS. love the library otherwise, it's a much needed bridge between jest and TS. Thanks for your work.
EDIT: the same issue occurs when using the jest-mock-extended
any()
matcher. I guess the issue is more how it's evaluated in thetoBeCalledWith
/toEqual
methods than the specific matcher used within those methods.The text was updated successfully, but these errors were encountered: