Skip to content

State is always initialized under shallow #1849

@abettadapur

Description

@abettadapur

Describe the bug
I have a component that does not initialize any state.

If I shallow mount this component, when render is called, this.state has a {} value, even though I did not initialize it.

Repro:

const Enzyme = require("enzyme");
const React = require("react");
class Foo extends React.Component {
    constructor(props, context) {
        super(props, context);
    }
    render() {
        console.log(`State: ${JSON.stringify(this.state)}`); // prints State: {}
        return (React.createElement("div", null));
    }
}
it("Test render", () => {
    Enzyme.shallow(React.createElement(Foo, null));
});

Expected behavior
this.state should be undefined

** Versions **
Enzyme: 3.3.2
Enzyme-Adapter-React-16: 1.5.0

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