简体   繁体   中英

Jest manual mock doesn't return correct value

I have an issue with jest manual mocking after upgraing to 15.1.1 (react is 15.3.1)

When I set a mock result inside the test, when the mocked method is called the actual result is not the one expected, but the initial one when the variable was created.

It was working just fine before I upgared react and jest.

Here's my mock :

 'use strict'; const psMock = jest.genMockFromModule('../ProcessService'); import clone from 'lodash/clone' var _resultDeOuf = []; function __setMockResult(result) { _resultDeOuf = result; } psMock.getRelatedProcessesByGroupingId = jest.fn(() => { return { then: (callback) => callback(_resultDeOuf); } }); psMock.__setMockResult = __setMockResult; export default psMock`

Here's my test :

 jest.unmock('../SuperProcessRow'); jest.unmock('../ProcessRow'); import React from "react"; import ReactDom from "react-dom"; import TestUtils from "react-addons-test-utils"; import processService from 'ProcessService' import SuperProcessRow from '../SuperProcessRow' const defaultSuperProcess = { "processId": "97816", "executionId": null, "cancelExecutionId": null } describe('SuperProcessRow', () => { beforeEach(() => { processService.getRelatedProcessesByGroupingId.mockClear() }); it('load sub processes on super process click ', () => { let responseSubProcesses = { processes : subProcesses, totalCount : 5 }; processService.__setMockResult(responseSubProcesses); let superProcessRow = TestUtils.renderIntoDocument( <table><SuperProcessRow process={defaultSuperProcess}/></table>); superProcessRow = ReactDom.findDOMNode(superProcessRow); let icon = superProcessRow.querySelector('i'); TestUtils.Simulate.click(icon); expect(processService.getRelatedProcessesByGroupingId.mock.calls.length).toEqual(1); }) });

And In the actual production code, I have a call to getRelatedProcessGroupingId and I handle the response inside a .then method. And instead of retrieving data set on the test, I got the intial value: [].

does someone have an idea ?

Thank you Vincent

I fixed it by setting _resultDeOuf inside the window object. It's ugly but it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM