IssueRaiser

`useEffect` not called when the component is `shallow` renderered

### Current behavior `useEffect` fonction does not seem to be executed when the component is rendered with `shallow`. Given the following simple component using `useEffect` hook to call a method given as a prop: ``` import React, { useEffect } from 'react'; export const MyComponent = ({ callback }) => { useEffect(() => { callback('Say something'); }); return <div>Hello world!</div>; }; ``` And the following tests checking that the `callback` function is indeed called accordingly: ``` import React from 'react'; import { shallow, mount } from 'enzyme'; import { MyComponent } from '../MyComponent'; describe('MyComponent', () => { describe('shallow render', () => { it('should call callback when shallow rendered', () => { const callback = jest.fn(); shallow(<MyComponent callback={callback} />); expect(callback).toHaveBeenCalled(); }); }); describe('mount', () => { it('should call callback when mounted', () => { const callback = jest.fn(); mount(<MyComponent callback={callback} />); expect(callback).toHaveBeenCalled(); }); }); }); ``` We observe that the method is called as expected when "mounted", but not when using a `shallow` renderer: ``` FAIL src/tests/MyComponent.test.js MyComponent shallow render ✕ should call callback when shallow rendered (12ms) mount ✓ should call callback when mounted (24ms) ● MyComponent › shallow render › should call callback when shallow rendere

Posted by jlandic on Apr 8, 2019, 9:17 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments

reliability (Other)

`useEffect` not called when the component is `shallow` renderered

jlandicApr 8, 2019, 9:17 AM

### Current behavior `useEffect` fonction does not seem to be executed when the component is rendered with `shallow`. Given the following simple component using `useEffect` hook to call a method given as a prop: ``` import React, { useEffect } from 'react'; export const MyComponent = ({ callback }) => { useEffect(() => { callback('Say something'); }); return <div>Hello world!</div>; }; ``` And the following tests checking that the `callback` function is indeed called accordingly: ``` import React from 'react'; import { shallow, mount } from 'enzyme'; import { MyComponent } from '../MyComponent'; describe('MyComponent', () => { describe('shallow render', () => { it('should call callback when shallow rendered', () => { const callback = jest.fn(); shallow(<MyComponent callback={callback} />); expect(callback).toHaveBeenCalled(); }); }); describe('mount', () => { it('should call callback when mounted', () => { const callback = jest.fn(); mount(<MyComponent callback={callback} />); expect(callback).toHaveBeenCalled(); }); }); }); ``` We observe that the method is called as expected when "mounted", but not when using a `shallow` renderer: ``` FAIL src/tests/MyComponent.test.js MyComponent shallow render ✕ should call callback when shallow rendered (12ms) mount ✓ should call callback when mounted (24ms) ● MyComponent › shallow render › should call callback when shallow rendere

Comments (0)

No comments yet. Start the discussion.

Comment on this issue