From fd877872b536a86d9cfda329dcb70ab63f926387 Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Wed, 21 Dec 2022 14:30:50 +0100 Subject: [PATCH] Fix an issue that broke serialization (#2642) --- .changeset/jest-serialization-failure.md | 5 ++++ packages/jest/src/create-serializer.js | 5 +++- .../__snapshots__/react-enzyme.test.js.snap | 26 +++++++++++++++++++ packages/jest/test/react-enzyme.test.js | 8 ++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 .changeset/jest-serialization-failure.md diff --git a/.changeset/jest-serialization-failure.md b/.changeset/jest-serialization-failure.md new file mode 100644 index 000000000..ba715ea3d --- /dev/null +++ b/.changeset/jest-serialization-failure.md @@ -0,0 +1,5 @@ +--- +'@emotion/jest': patch +--- + +Fix an issue that broke serialization. Nodes in a render prop aren't writable, making the `delete node.props.className` instruction throw. diff --git a/packages/jest/src/create-serializer.js b/packages/jest/src/create-serializer.js index c1c5795bb..717102b75 100644 --- a/packages/jest/src/create-serializer.js +++ b/packages/jest/src/create-serializer.js @@ -172,7 +172,10 @@ function clean(node: any, classNames: string[]) { const { className } = node.props if (!className) { // if it's empty, remove it - delete node.props.className + const descriptor = Object.getOwnPropertyDescriptor(node, 'props') + if (descriptor && descriptor.writable) { + delete node.props.className + } } else { const hasKnownClass = hasIntersection(className.split(' '), classNames) if (hasKnownClass) { diff --git a/packages/jest/test/__snapshots__/react-enzyme.test.js.snap b/packages/jest/test/__snapshots__/react-enzyme.test.js.snap index 0117cc108..2dd52ba2c 100644 --- a/packages/jest/test/__snapshots__/react-enzyme.test.js.snap +++ b/packages/jest/test/__snapshots__/react-enzyme.test.js.snap @@ -440,6 +440,24 @@ exports[`enzyme mount with prop containing css element with other props 1`] = ` `; +exports[`enzyme mount with prop containing empty classname 1`] = ` + + Hi + + } +> +
+ + Hi + +
+
+`; + exports[`enzyme mount with prop containing regular element 1`] = ` `; +exports[`enzyme shallow with prop containing empty classname 1`] = ` +
+ + Hi + +
+`; + exports[`enzyme shallow with prop containing regular element 1`] = `