question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

shallow->setProps: props not being set

See original GitHub issue

Current behavior

When running setProps. This results in it not being set in the most simple of scenarios:

simplified down to a basic component / test

import React from 'react';
import {configure, shallow} from 'enzyme';

import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });

const Component = ({prop1}) => <div>{prop1}</div>

describe.only('Component', () => {
  it('should set the props', () => {
    const component = shallow(<Component />);

    component.setProps({prop1: 'prop1'});

    expect(component.prop('prop1')).toEqual('prop1');
    expect(component.props()).toEqual({prop1: 'prop1'});
  });
});

result:

image

Expected behavior

shallow -> component
-> set props
-> props -> the props that were set

Your environment

Running this inside of jest (23.6.0) on Node (8.14.0).

API

  • shallow
  • mount
  • render

Version

library version
enzyme 3.7.0
react 16.3.0
react-dom 16.3.0
react-test-renderer n/a
adapter (below) 1.7.0

Adapter

  • enzyme-adapter-react-16
  • enzyme-adapter-react-16.3
  • enzyme-adapter-react-16.2
  • enzyme-adapter-react-16.1
  • enzyme-adapter-react-15
  • enzyme-adapter-react-15.4
  • enzyme-adapter-react-14
  • enzyme-adapter-react-13
  • enzyme-adapter-react-helper
  • others ( )

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
paddotkcommented, Oct 3, 2019

These answers don’t help. What is .setProps() for if it’s not expected to do anything?

1reaction
ljharbcommented, Oct 3, 2019

It certainly is expected to do something - it sets the props on the rendered element, not on the element whose render method is being evaluated. (for shallow)

Read more comments on GitHub >

github_iconTop Results From Across the Web

shallow->setProps: props not being set #1939 - GitHub
Current behavior When running setProps. This results in it not being set in the most simple of scenarios: simplified down to a basic ......
Read more >
setProps(nextProps) · Enzyme - GitHub Pages
A method that sets the props of the root component, and re-renders. Useful for when you are wanting to test how the component...
Read more >
"setProps()" not triggering "useEffect" in Jest - Stack Overflow
See useEffect not called when the component is shallow renderered and this issue. ... setProps({ userId }); expect(props.myAction).
Read more >
Enzyme cheatsheet - Devhints
Shallow wrapping doesn't descend down to sub-components. A full mount also mounts sub-components. See: Shallow rendering, Full rendering. Debugging. console.
Read more >
setProps(nextProps) | Enzyme - UNPKG
A method that sets the props of the root component, and re-renders. Useful for when you are wanting to test how the component...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found