Skip to content

Convert elements to test objects for toMatchObject in Jest #9747

@pedrottimark

Description

@pedrottimark

Do you want to request a feature or report a bug?

Request your guidance to add a feature for testing, especially with Jest and Enzyme

What is the current behavior?

To get the benefit of descriptive JSX you must pay a cost of irrelevant details in some snapshot tests, which cause unnecessary updates, and risk incorrect decisions.

What is the proposed behavior?

Goal: balance effort to write tests initially and update tests correctly when components change.

Replace some toMatchSnapshot with toMatchObject for elements with a relevant subset of props.

Therefore, this issue to convert the expected React elements directly to test objects, especially for shallow rendering and a new deep-event/shallow-test pattern (see Table-C example).

For more information, see:

Proposed: be able to import from react-test-renderer

Can you think of clearer names? I am willing to contribute with your guidance.

Draft of code: https://github.com/pedrottimark/whimper/blob/master/src/testing/react-test-renderer/relevantTestObject.js

Because toMatchObject expects an object, relevantTestObject doesn’t expect an array of elements, which components can render in React 16. Can you think of test cases when it should?

Examples of tests:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions