Skip to content

Commit 1a25293

Browse files
authored
Added includeStyles option to createSerializer to optionally disable styles printing
[@emotion/jest] Add `includeStyles` option to `createSerializer`
1 parent 2d3d7dd commit 1a25293

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

.changeset/polite-icons-judge.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/jest': minor
3+
---
4+
5+
Added `includeStyles` option to `createSerializer` to optionally disable styles printing.

packages/jest/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ import { createSerializer } from '@emotion/jest'
9090
expect.addSnapshotSerializer(createSerializer({ DOMElements: false }))
9191
```
9292

93+
### `includeStyles`
94+
95+
@emotion/jest's snapshot serializer inserts styles. If you would like to disable this behavior, you can do so by passing `{ includeStyles: false }`. For example:
96+
97+
```jsx
98+
import { createSerializer } from '@emotion/jest'
99+
100+
// configures @emotion/jest to not insert styles
101+
expect.addSnapshotSerializer(createSerializer({ includeStyles: false }))
102+
```
103+
93104
# Custom matchers
94105

95106
## toHaveStyleRule

packages/jest/src/create-serializer.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function getPrettyStylesFromClassNames(
7474

7575
export type Options = {
7676
classNameReplacer?: (className: string, index: number) => string,
77-
DOMElements?: boolean
77+
DOMElements?: boolean,
78+
includeStyles?: boolean
7879
}
7980

8081
function filterEmotionProps(props = {}) {
@@ -183,7 +184,8 @@ function clean(node: any, classNames: string[]) {
183184

184185
export function createSerializer({
185186
classNameReplacer,
186-
DOMElements = true
187+
DOMElements = true,
188+
includeStyles = true
187189
}: Options = {}) {
188190
const cache = new WeakSet()
189191
const isTransformed = val => cache.has(val)
@@ -202,11 +204,9 @@ export function createSerializer({
202204
const converted = deepTransform(val, convertEmotionElements)
203205
const nodes = getNodes(converted)
204206
const classNames = getClassNamesFromNodes(nodes)
205-
const styles = getPrettyStylesFromClassNames(
206-
classNames,
207-
elements,
208-
config.indent
209-
)
207+
const styles = includeStyles
208+
? getPrettyStylesFromClassNames(classNames, elements, config.indent)
209+
: ''
210210
clean(converted, classNames)
211211

212212
nodes.forEach(cache.add, cache)

packages/jest/test/__snapshots__/printer.test.js.snap

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ exports[`jest-emotion with dom elements replaces class names and inserts styles
7474
</div>"
7575
`;
7676

77+
exports[`jest-emotion with style insertion disabled does not insert styles into snapshots 1`] = `
78+
"<div
79+
class=\\"emotion-0\\"
80+
>
81+
<svg
82+
class=\\"emotion-1\\"
83+
/>
84+
</div>"
85+
`;
86+
7787
exports[`prints speedy styles 1`] = `
7888
".emotion-0 {
7989
color: hotpink;

packages/jest/test/printer.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ describe('jest-emotion with DOM elements disabled', () => {
9898
})
9999
})
100100

101+
test("allows to opt-out from styles printing", () => {
102+
const emotionPlugin = createSerializer({ includeStyles: false })
103+
104+
const divStyle = css`
105+
color: red;
106+
`
107+
108+
const svgStyle = css`
109+
width: 100%;
110+
`
111+
112+
const divRef = React.createRef()
113+
render(
114+
<div css={divStyle} ref={divRef}>
115+
<svg css={svgStyle} />
116+
</div>
117+
)
118+
119+
const output = prettyFormat(divRef.current, {
120+
plugins: [emotionPlugin, ReactElement, ReactTestComponent, DOMElement]
121+
})
122+
123+
expect(output).toMatchSnapshot()
124+
})
125+
101126
test('does not replace class names that are not from emotion', () => {
102127
let tree = renderer
103128
.create(

packages/jest/types/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const matchers: EmotionMatchers
2626
export interface CreateSerializerOptions {
2727
classNameReplacer?: (className: string, index: number) => string
2828
DOMElements?: boolean
29+
includeStyles?: boolean
2930
}
3031
export function createSerializer(
3132
options?: CreateSerializerOptions

0 commit comments

Comments
 (0)