Skip to content

Commit 642a576

Browse files
committed
📝 improve documentation
🏠 prepare package for release
1 parent 451073d commit 642a576

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
> - :nail_care: [Polish]
1212
1313

14-
## v0.1.0
15-
* :house: Initial set-up
14+
## v1.0.0
15+
* :house: Initial implementation

README.md

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,57 @@
4747

4848

4949
# 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).
92+
This serializer will return:
93+
94+
```jsx
95+
<Context.Consumer>
96+
() => (<Subscriber
97+
handler={handler}
98+
/>)
99+
</Context.Consumer>
100+
```
51101
52102
## Installation
53103
```sh
@@ -62,13 +112,44 @@ yarn add --dev jest-serializer-functions
62112
63113
### Having jest config as json
64114
```json
115+
// jest.json
65116
{
66117
...
67-
"snapshotSerializers":[
118+
"snapshotSerializers": [
68119
"jest-serializer-functions"
69120
],
70121
...
71122
}
72123
```
73124
74-
npm i --save-dev babel-jest @babel/core [email protected]
125+
### Having jest config as json inside package.json
126+
```json
127+
// package.json
128+
{
129+
...
130+
"jest": {
131+
...
132+
"snapshotSerializers": [
133+
"jest-serializer-functions"
134+
],
135+
...
136+
},
137+
...
138+
}
139+
```
140+
141+
## Take into consideration
142+
143+
> Everything has pros & cons.
144+
145+
### Pros
146+
This serializer will help you with a better understanding of your functions and fail tests that until now passed when doing partial refactoring.
147+
148+
### Cons
149+
The time to execute your tests will increase (depending on how many snapshots you have like this).
150+
151+
The space used by snapshot will increase, as more data will be stored.
152+
153+
## Contributing
154+
155+
Pull requests and issues are welcome :heart:.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@
4545
"files": [
4646
"lib"
4747
],
48-
"version": "0.1.0"
48+
"version": "1.0.0"
4949
}

0 commit comments

Comments
 (0)