モック関数の定義方法がいくつか違っていて、すべての試行に失敗しました。私は次のようにそれを定義しようとすると:それは、次のエラーを返しjest.mock(モジュール)とjest.fn()の違い
var spy = jest.mock('../src/data/server',()=> ({server: {report: jest.fn()}}));
expect(spy).toBeCalledWith(id, data,() => {...},() => {...});
:として私はモックを定義する場合
expect(jest.fn())[.not].toBeCalledWith()
jest.fn() value must be a mock function or spy.
Received: undefined
:
jest.mock('../src/data/server',()=> ({server: {report: jest.fn()}}));
expect(server.report.mock).toBeCalledWith(id, data,() => {...},() => {...});
私はこのエラーを取得する
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]}
私の3回目の試みとして、モックを定義しました以下のように:
const st = require.requireActual('../src/data/server',()=> ({server: {report: jest.fn()}}));
st.report = jest.fn();
expect(st.report).toBeCalledWith(id, data, () => {...},() => {...});
そして、私はこのエラーを取得する:
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": "[email protected]", "message": "testmessage",
"name": "testname", "phone": "1234567890", "zipcode": "12345"}, [Function anonymous], [Function anonymous]]
But it was not called.
私が問題とどのようにモックを定義し、これらの3つの方法が異なっているのだろうか?
P.S.コードはここで見つけることができます:Write a Unit test in Jest for a React form