Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jest serialization issue (#2642) #2959

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/jest-serialization-failure.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 4 additions & 1 deletion packages/jest/src/create-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions packages/jest/test/__snapshots__/react-enzyme.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,24 @@ exports[`enzyme mount with prop containing css element with other props 1`] = `
</Greeting>
`;

exports[`enzyme mount with prop containing empty classname 1`] = `
<Thing
renderProp={
<span
className=""
>
Hi
</span>
}
>
<div>
<span>
Hi
</span>
</div>
</Thing>
`;

exports[`enzyme mount with prop containing regular element 1`] = `
<Test
element={
Expand Down Expand Up @@ -796,6 +814,14 @@ exports[`enzyme shallow with prop containing css element with other props 1`] =
</div>
`;

exports[`enzyme shallow with prop containing empty classname 1`] = `
<div>
<span>
Hi
</span>
</div>
`;

exports[`enzyme shallow with prop containing regular element 1`] = `
<button>
Foo
Expand Down
8 changes: 8 additions & 0 deletions packages/jest/test/react-enzyme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ const cases = {
)
}
},
'with prop containing empty classname': {
render() {
const Thing = ({ renderProp }) => {
return <div>{renderProp}</div>
}
return <Thing renderProp={<span className="">Hi</span>} />
}
},
'with array of styles as css prop': {
render() {
const style1 = css`
Expand Down