Skip to content

Commit af1ab24

Browse files
bensampaioSimenB
authored andcommitted
Support React.memo in pretty-format (#7891)
1 parent 9e31173 commit af1ab24

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- `[expect]`: Improve report when matcher fails, part 7 ([#7866](https://github.com/facebook/jest/pull/7866))
66
- `[expect]`: Improve report when matcher fails, part 8 ([#7876](https://github.com/facebook/jest/pull/7876))
7+
- `[pretty-format]` Support `React.memo` ([#7891](https://github.com/facebook/jest/pull/7891))
78

89
### Fixes
910

packages/pretty-format/src/__tests__/react.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,16 @@ test('supports forwardRef with a child', () => {
709709
).toEqual('<ForwardRef(Cat)>\n mouse\n</ForwardRef(Cat)>');
710710
});
711711

712+
test('supports memo with a child', () => {
713+
function Dog(props: any) {
714+
return React.createElement('div', props, props.children);
715+
}
716+
717+
expect(
718+
formatElement(React.createElement(React.memo(Dog), null, 'cat')),
719+
).toEqual('<Memo(Dog)>\n cat\n</Memo(Dog)>');
720+
});
721+
712722
test('supports context Provider with a child', () => {
713723
const {Provider} = React.createContext('test');
714724

packages/pretty-format/src/plugins/ReactElement.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const fragmentSymbol = Symbol.for('react.fragment');
1919
const forwardRefSymbol = Symbol.for('react.forward_ref');
2020
const providerSymbol = Symbol.for('react.provider');
2121
const contextSymbol = Symbol.for('react.context');
22+
const memoSymbol = Symbol.for('react.memo');
2223

2324
// Given element.props.children, or subtree during recursive traversal,
2425
// return flattened array of children.
@@ -60,6 +61,12 @@ const getType = (element: any) => {
6061
? 'ForwardRef(' + functionName + ')'
6162
: 'ForwardRef';
6263
}
64+
65+
if (type.$$typeof === memoSymbol) {
66+
const functionName = type.type.displayName || type.type.name || '';
67+
68+
return functionName !== '' ? 'Memo(' + functionName + ')' : 'Memo';
69+
}
6370
}
6471
return 'UNDEFINED';
6572
};

0 commit comments

Comments
 (0)