I have tried couple different ways of defining the mock function and all of my tries failed. When I try to define it as follow:
jest.mock('../src/data/server', ()=> ({server: {report: jest.fn()}})); expect(server.report.mock).toBeCalledWith(id, data, () => {...}, () => {...});
I get the this error:
expect(jest.fn())[.not].toBeCalledWith() jest.fn() value must be a mock function or spy. Received: undefined
If I define the mock as :
var spy = jest.mock('../src/data/server', ()=> ({server: {report: jest.fn()}})); expect(spy).toBeCalledWith(id, data, () => {...}, () => {...});
it returns the following error:
expect(jest.fn())[.not].toBeCalledWith() jest.fn() value must be a mock function or spy. Received: object: {"addMatchers": [Function anonymous], "autoMockOff": [Function anonymous], "autoMockOn": [Function anonymous], "clearAllMocks": [Function anonymous], "clearAllTimers": [Function anonymous], "deepUnmock": [Function anonymous], "disableAutomock": [Function anonymous], "doMock": [Function anonymous], "dontMock": [Function anonymous], "enableAutomock": [Function anonymous], "fn": [Function anonymous], "genMockFn": [Function bound getMockFunction], "genMockFromModule": [Function anonymous], "genMockFunction": [Function bound getMockFunction], "isMockFunction": [Function isMockFunction], "mock": [Function anonymous], "resetModuleRegistry": [Function anonymous], "resetModules": [Function anonymous], "runAllImmediates": [Function anonymous], "runAllTicks": [Function anonymous], "runAllTimers": [Function anonymous], "runOnlyPendingTimers": [Function anonymous], "runTimersToTime": [Function anonymous],"setMock": [Function anonymous], "unmock": [Function anonymous], "useFakeTimers": [Function anonymous], "useRealTimers": [Function anonymous]}
As my third try I defined the mock as:
const st = require.requireActual('../src/data/server', ()=> ({server: {report: jest.fn()}})); st.report = jest.fn(); expect(st.report).toBeCalledWith(id, data, () => {...}, () => {...});
And I get this error:
expect(jest.fn()).toBeCalledWith(expected) Expected mock function to have been called with: ["1033083fe", {"address": "test address", "affiliation": "testaaa", "city": "testcity", "copyright": true, "country": "testcountry", "email": "sss@test.com", "message": "testmessage", "name": "testname", "phone": "1234567890", "zipcode": "12345"}, [Function anonymous], [Function anonymous]] But it was not called.
I wonder what is the problem and how these 3 ways of defining mock are different?
P.S. the code can be find here: Write a Unit test in Jest for a React form