Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ff3cb16
Change `Global` component to use the `StyleSheet` constructor of the …
Jack-Works Mar 10, 2022
7f8910a
Removed the direct dependency on from as it's no longer used that way
Andarist Mar 10, 2022
239f2d0
Version Packages (#2679)
github-actions[bot] Mar 10, 2022
2d3d7dd
Fixed `CacheProvider` demo in the docs (#2678)
pengx17 Mar 10, 2022
1a25293
Added `includeStyles` option to `createSerializer` to optionally disa…
thompsongl Apr 5, 2022
d935762
Fixed a false positive warning for `content` properties that included…
arturmuller Apr 5, 2022
8c1c7c7
Fixed a Prettier error
Andarist Apr 5, 2022
fc52bed
Update snapshots after renaming a test (#2711)
Andarist Apr 6, 2022
7a478fc
Version Packages (#2710)
github-actions[bot] Apr 6, 2022
3743483
Fixed an issue with `createEnzymeSerializer` not handling the recentl…
thompsongl Apr 7, 2022
6c9dbe5
Version Packages (#2716)
github-actions[bot] Apr 7, 2022
cd535bf
Update to new DocSearch index (#2718)
emmatown Apr 9, 2022
ae0f650
Fixed an issue in the minifying logic that could remove rules with th…
Andarist Apr 10, 2022
888377a
Version Packages (#2722)
github-actions[bot] Apr 10, 2022
409b237
Replace CodeSandbox link in bug report issue template (#2726)
srmagura Apr 16, 2022
6c652d9
Fix TS 4.7 error related to isPropValid
srmagura Apr 24, 2022
b575ef7
Merge branch 'main' of https://github.com/emotion-js/emotion into ts-…
srmagura Apr 24, 2022
69b3e06
Merge branch 'ts-april-23' into ts-merge
srmagura Apr 24, 2022
6bf9be3
Remove accidentally-committed .d.ts files
srmagura Apr 24, 2022
b9640a5
Fix unintentional breaking change to @emotion/memoize
srmagura Apr 24, 2022
f187b1d
Reorder function overloads in packages/memoize/src/index.ts
srmagura Apr 24, 2022
ba5f146
Make snapshots more readable (#2733)
SimenB Apr 25, 2022
7a3f608
Merge branch 'main' of https://github.com/emotion-js/emotion into ts-…
srmagura Apr 28, 2022
bf0a262
Merge pull request #2734 from emotion-js/preconstruct-upgrade
Andarist Apr 28, 2022
6c2d7a6
Allow any `Node` as container (#2728)
Peeja Apr 28, 2022
26e4e3e
Update "no component selectors" error message (#2727)
srmagura Apr 28, 2022
30f5cc4
Merge branch 'main' of https://github.com/emotion-js/emotion into ts-…
srmagura Apr 30, 2022
a485db3
styled: require prop to be a string
srmagura Apr 30, 2022
b45662d
Put in hacks to make .d.ts generation succeed for @emotion/eslint-plugin
srmagura Apr 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added includeStyles option to createSerializer to optionally disa…
…ble styles printing

[@emotion/jest] Add `includeStyles` option to `createSerializer`
  • Loading branch information
thompsongl authored Apr 5, 2022
commit 1a25293fffa348d55bbf78e8855a6390bc54230b
5 changes: 5 additions & 0 deletions .changeset/polite-icons-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/jest': minor
---

Added `includeStyles` option to `createSerializer` to optionally disable styles printing.
11 changes: 11 additions & 0 deletions packages/jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ import { createSerializer } from '@emotion/jest'
expect.addSnapshotSerializer(createSerializer({ DOMElements: false }))
```

### `includeStyles`

@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:

```jsx
import { createSerializer } from '@emotion/jest'

// configures @emotion/jest to not insert styles
expect.addSnapshotSerializer(createSerializer({ includeStyles: false }))
```

# Custom matchers

## toHaveStyleRule
Expand Down
14 changes: 7 additions & 7 deletions packages/jest/src/create-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ function getPrettyStylesFromClassNames(

export type Options = {
classNameReplacer?: (className: string, index: number) => string,
DOMElements?: boolean
DOMElements?: boolean,
includeStyles?: boolean
}

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

export function createSerializer({
classNameReplacer,
DOMElements = true
DOMElements = true,
includeStyles = true
}: Options = {}) {
const cache = new WeakSet()
const isTransformed = val => cache.has(val)
Expand All @@ -202,11 +204,9 @@ export function createSerializer({
const converted = deepTransform(val, convertEmotionElements)
const nodes = getNodes(converted)
const classNames = getClassNamesFromNodes(nodes)
const styles = getPrettyStylesFromClassNames(
classNames,
elements,
config.indent
)
const styles = includeStyles
? getPrettyStylesFromClassNames(classNames, elements, config.indent)
: ''
clean(converted, classNames)

nodes.forEach(cache.add, cache)
Expand Down
10 changes: 10 additions & 0 deletions packages/jest/test/__snapshots__/printer.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ exports[`jest-emotion with dom elements replaces class names and inserts styles
</div>"
`;

exports[`jest-emotion with style insertion disabled does not insert styles into snapshots 1`] = `
"<div
class=\\"emotion-0\\"
>
<svg
class=\\"emotion-1\\"
/>
</div>"
`;

exports[`prints speedy styles 1`] = `
".emotion-0 {
color: hotpink;
Expand Down
25 changes: 25 additions & 0 deletions packages/jest/test/printer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ describe('jest-emotion with DOM elements disabled', () => {
})
})

test("allows to opt-out from styles printing", () => {
const emotionPlugin = createSerializer({ includeStyles: false })

const divStyle = css`
color: red;
`

const svgStyle = css`
width: 100%;
`

const divRef = React.createRef()
render(
<div css={divStyle} ref={divRef}>
<svg css={svgStyle} />
</div>
)

const output = prettyFormat(divRef.current, {
plugins: [emotionPlugin, ReactElement, ReactTestComponent, DOMElement]
})

expect(output).toMatchSnapshot()
})

test('does not replace class names that are not from emotion', () => {
let tree = renderer
.create(
Expand Down
1 change: 1 addition & 0 deletions packages/jest/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const matchers: EmotionMatchers
export interface CreateSerializerOptions {
classNameReplacer?: (className: string, index: number) => string
DOMElements?: boolean
includeStyles?: boolean
}
export function createSerializer(
options?: CreateSerializerOptions
Expand Down