Skip to content

Commit b1fe77c

Browse files
authored
docs: Update snapshotSerializers Docs (#15841)
1 parent f73c098 commit b1fe77c

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

docs/Configuration.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,10 +1804,10 @@ A list of paths to snapshot serializer modules Jest should use for snapshot test
18041804

18051805
Jest has default serializers for built-in JavaScript types, HTML elements (Jest 20.0.0+), ImmutableJS (Jest 20.0.0+) and for React elements. See [snapshot test tutorial](TutorialReactNative.md#snapshot-test) for more information.
18061806

1807-
```js title="custom-serializer.js"
1807+
```js tab title="custom-serializer.js"
18081808
module.exports = {
18091809
serialize(val, config, indentation, depth, refs, printer) {
1810-
return `Pretty foo: ${printer(val.foo)}`;
1810+
return `Pretty foo: ${printer(val.foo, config, indentation, depth, refs)}`;
18111811
},
18121812

18131813
test(val) {
@@ -1816,6 +1816,22 @@ module.exports = {
18161816
};
18171817
```
18181818

1819+
```ts tab title="custom-serializer.ts"
1820+
import type {Plugin} from 'pretty-format';
1821+
1822+
const plugin: Plugin = {
1823+
serialize(val, config, indentation, depth, refs, printer): string {
1824+
return `Pretty foo: ${printer(val.foo, config, indentation, depth, refs)}`;
1825+
},
1826+
1827+
test(val): boolean {
1828+
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
1829+
},
1830+
};
1831+
1832+
export default plugin;
1833+
```
1834+
18191835
`printer` is a function that serializes a value using existing plugins.
18201836

18211837
Add `custom-serializer` to your Jest configuration:
@@ -1833,7 +1849,7 @@ module.exports = config;
18331849
import type {Config} from 'jest';
18341850

18351851
const config: Config = {
1836-
snapshotSerializers: ['path/to/custom-serializer.js'],
1852+
snapshotSerializers: ['path/to/custom-serializer.ts'],
18371853
};
18381854

18391855
export default config;

website/versioned_docs/version-29.7/Configuration.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,10 +1787,10 @@ A list of paths to snapshot serializer modules Jest should use for snapshot test
17871787

17881788
Jest has default serializers for built-in JavaScript types, HTML elements (Jest 20.0.0+), ImmutableJS (Jest 20.0.0+) and for React elements. See [snapshot test tutorial](TutorialReactNative.md#snapshot-test) for more information.
17891789

1790-
```js title="custom-serializer.js"
1790+
```js tab title="custom-serializer.js"
17911791
module.exports = {
17921792
serialize(val, config, indentation, depth, refs, printer) {
1793-
return `Pretty foo: ${printer(val.foo)}`;
1793+
return `Pretty foo: ${printer(val.foo, config, indentation, depth, refs)}`;
17941794
},
17951795

17961796
test(val) {
@@ -1799,6 +1799,22 @@ module.exports = {
17991799
};
18001800
```
18011801

1802+
```ts tab title="custom-serializer.ts"
1803+
import type {Plugin} from 'pretty-format';
1804+
1805+
const plugin: Plugin = {
1806+
serialize(val, config, indentation, depth, refs, printer): string {
1807+
return `Pretty foo: ${printer(val.foo, config, indentation, depth, refs)}`;
1808+
},
1809+
1810+
test(val): boolean {
1811+
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
1812+
},
1813+
};
1814+
1815+
export default plugin;
1816+
```
1817+
18021818
`printer` is a function that serializes a value using existing plugins.
18031819

18041820
Add `custom-serializer` to your Jest configuration:
@@ -1816,7 +1832,7 @@ module.exports = config;
18161832
import type {Config} from 'jest';
18171833

18181834
const config: Config = {
1819-
snapshotSerializers: ['path/to/custom-serializer.js'],
1835+
snapshotSerializers: ['path/to/custom-serializer.ts'],
18201836
};
18211837

18221838
export default config;

website/versioned_docs/version-30.0/Configuration.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,10 +1811,10 @@ A list of paths to snapshot serializer modules Jest should use for snapshot test
18111811

18121812
Jest has default serializers for built-in JavaScript types, HTML elements (Jest 20.0.0+), ImmutableJS (Jest 20.0.0+) and for React elements. See [snapshot test tutorial](TutorialReactNative.md#snapshot-test) for more information.
18131813

1814-
```js title="custom-serializer.js"
1814+
```js tab title="custom-serializer.js"
18151815
module.exports = {
18161816
serialize(val, config, indentation, depth, refs, printer) {
1817-
return `Pretty foo: ${printer(val.foo)}`;
1817+
return `Pretty foo: ${printer(val.foo, config, indentation, depth, refs)}`;
18181818
},
18191819

18201820
test(val) {
@@ -1823,6 +1823,22 @@ module.exports = {
18231823
};
18241824
```
18251825

1826+
```ts tab title="custom-serializer.ts"
1827+
import type {Plugin} from 'pretty-format';
1828+
1829+
const plugin: Plugin = {
1830+
serialize(val, config, indentation, depth, refs, printer): string {
1831+
return `Pretty foo: ${printer(val.foo, config, indentation, depth, refs)}`;
1832+
},
1833+
1834+
test(val): boolean {
1835+
return val && Object.prototype.hasOwnProperty.call(val, 'foo');
1836+
},
1837+
};
1838+
1839+
export default plugin;
1840+
```
1841+
18261842
`printer` is a function that serializes a value using existing plugins.
18271843

18281844
Add `custom-serializer` to your Jest configuration:
@@ -1840,7 +1856,7 @@ module.exports = config;
18401856
import type {Config} from 'jest';
18411857

18421858
const config: Config = {
1843-
snapshotSerializers: ['path/to/custom-serializer.js'],
1859+
snapshotSerializers: ['path/to/custom-serializer.ts'],
18441860
};
18451861

18461862
export default config;

0 commit comments

Comments
 (0)