You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+84-3Lines changed: 84 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,57 @@
47
47
48
48
49
49
# jest-serializer-functions
50
-
Jest snapshot serializer for functions. Reade more about [snapshotSerializers](https://jestjs.io/docs/en/configuration.html#snapshotserializers-array-string)
50
+
Jest snapshot serializer for functions. Read more about [snapshotSerializers](https://jestjs.io/docs/en/configuration.html#snapshotserializers-array-string)
51
+
52
+
## Background
53
+
Whenever you are using Jest for snapshot you may notice that functions will appear in snapshots in different ways.
54
+
If the function is mocked you will see it as:
55
+
*`[Function]`
56
+
* when the function is not mocked with Jest
57
+
* when you have React component that has a function inside JSX (consumers)
58
+
*`[MockFunction]`
59
+
* when the function is mocked with jest, usually implementation is: `myFn = jest.fn()`
60
+
*`[MockFunction <<function-name>>]`
61
+
* where `<<function-name>>` is the name of the function
62
+
*```[MockFunction] {calls: [] results: []}```
63
+
* when the mocked function was called previously
64
+
65
+
### How it works
66
+
This serializer will help you have a more consistent way of viewing functions as every time will return `[Function]` as the base.
67
+
68
+
If the function has a name it will serialize it as `[Function] <<function-name>>`.
69
+
As the part where the function is mocked or not just adds noise to the important part.
70
+
71
+
Now the good part :astonished: is when you are using this serialiser
72
+
with (React)[https://www.npmjs.com/package/react] (and [enzyme](https://www.npmjs.com/package/enzyme))
73
+
as it will improve what you can see in the snapshot. Take as example source code:
74
+
```jsx
75
+
<Context.Consumer>
76
+
{
77
+
(handler) => (
78
+
<Subscriber
79
+
handler={handler
80
+
/>
81
+
)
82
+
}
83
+
</Context.Consumer>
84
+
```
85
+
this will result in a snapshot that will look like:
86
+
```jsx
87
+
<Context.Consumer>
88
+
[Function]
89
+
</Context.Consumer>
90
+
```
91
+
I would not say this is very helpful (if I change something from the function nothing will break in my snapshot).
0 commit comments