Skip to content

Shallow test renderer calls shouldComponentUpdate on forceUpdate #11236

@d4rky-pl

Description

@d4rky-pl

There is no extra guard or clause in enqueueForceUpdate when calling render which means the render always calls shouldComponentUpdate which can block even forced re-render during tests.

This for example breaks shallow rendering in Enzyme when using MobX and observables.

Minimal test case:

import React from 'react'
import ShallowRenderer from 'react-test-renderer/shallow';
const renderer = new ShallowRenderer();

class TestComponent extends React.Component {
  render() { return <span/> }
}

describe('forceUpdate', () => {
  it('does not call shouldComponentUpdate', () => {
    let called = 0
    TestComponent.prototype.shouldComponentUpdate = () => called += 1
    const component = renderer.render(<TestComponent/>);
    renderer._instance.forceUpdate()
    expect(called).toBe(0) // <- this throws because called === 1
  })
})

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions