enzymejs / enzyme

JavaScript Testing utilities for React
https://enzymejs.github.io/enzyme/
MIT License
19.95k stars 2.01k forks source link

setState of the component fails when updating the props using shallow wrapper #2455

Open anirudha615 opened 4 years ago

anirudha615 commented 4 years ago

Hey guys,

I am not sure whether this is an issue or mistake from my side. I have beating my mind around the below issue for a long time and is unable to get past it.

Pseudo-code of React native application: export default class MyProgram extends Component<props, states> { constructor () {// some code} componentDidMount() { // some native/backend call and adding a listener method through nativeEventEmitter } componentDidUpdate() { // few check and same native/backend call} paramsFromBackend() { // listening the JS event and parsing the JS data and hence changing the state of the component } }.

Pseudo-code of Jest Framework: const wrapper = shallowWrapper<MyProgram {...initialProps}>. // This hits constructor and componentDidMount(). // mocked nativeEventEmitter and emitted a fake JS event with fake data (basically replacing the backend data) // paramsFromBackend() gets called and the state changes wrapper.setProps({...oldProps, newProps}). // It hits the componentDidUpdate(). Again mocked nativeEventEmitter and emitted a fake JS event with fake data (basically replacing the backend data) // paramsFromBackend() gets called but the state does not change. Instead it throws the below error:

Method “setState” is meant to be run on 1 node. 3 found instead.

Method “setState” is meant to be run on 1 node. 3 found instead.

  73 |             }
> 74 |             this.setState({
     |                  ^
  75 |                 sidekickCardList: sidekickCardList,
  76 |             });
  77 |         }

Any help will be appreciated.

P.S. I cannot provide the exact code due to policy.

Thanks, Anirudha

anirudha615 commented 4 years ago

Any help will be appreciated.