@@ -920,6 +920,61 @@ test('runs timers as setTime passes ticks', (context) => {
920
920
});
921
921
```
922
922
923
+ ## Snapshot testing
924
+
925
+ > Stability: 1.0 - Early development
926
+
927
+ Snapshot tests allow arbitrary values to be serialized into string values and
928
+ compared against a set of known good values. The known good values are known as
929
+ snapshots, and are stored in a snapshot file. Snapshot files are managed by the
930
+ test runner, but are designed to be human readable to aid in debugging. Snapshot
931
+ files should be checked into source control along with your test files. In order
932
+ to enable snapshot testing, Node.js must be started with the
933
+ [ ` --experimental-test-snapshots ` ] [ ] command-line flag.
934
+
935
+ Snapshot files are generated by starting Node.js with the
936
+ [ ` --test-update-snapshots ` ] [ ] command-line flag. A separate snapshot file is
937
+ generated for each test entry point. By default, the snapshot file has the same
938
+ name as the entry point file with a ` .snapshot ` file extension. This behavior
939
+ can be configured using the ` config.setResolveSnapshotPath() ` function. Each
940
+ snapshot assertion corresponds to an export in the snapshot file.
941
+
942
+ An example snapshot test is shown below. The first time this test is executed,
943
+ it will fail because the corresponding snapshot file does not exist.
944
+
945
+ ``` js
946
+ // test.js
947
+ suite (' suite of snapshot tests' , () => {
948
+ test (' snapshot test' , (t ) => {
949
+ t .assert .snapshot ({ value1: 1 , value2: 2 });
950
+ t .assert .snapshot (5 );
951
+ });
952
+ });
953
+ ```
954
+
955
+ Generate the snapshot file by running the test file with
956
+ ` --test-update-snapshots ` . The test should pass, and a file named
957
+ ` test.js.snapshot ` is created in the same directory as the test file. The
958
+ contents of the snapshot file are shown below. Each snapshot is identified by
959
+ the full name of test and a counter to differentiate between snapshots in the
960
+ same test.
961
+
962
+ ``` js
963
+ exports [` suite of snapshot tests > snapshot test 1` ] = `
964
+ {
965
+ "value1": 1,
966
+ "value2": 2
967
+ }
968
+ ` ;
969
+
970
+ exports [` suite of snapshot tests > snapshot test 2` ] = `
971
+ 5
972
+ ` ;
973
+ ```
974
+
975
+ Once the snapshot file is created, run the tests again without the
976
+ ` --test-update-snapshots ` flag. The tests should pass now.
977
+
923
978
## Test reporters
924
979
925
980
<!-- YAML
@@ -1620,6 +1675,51 @@ describe('tests', async () => {
1620
1675
});
1621
1676
```
1622
1677
1678
+ ## ` config `
1679
+
1680
+ <!-- YAML
1681
+ added: REPLACEME
1682
+ -->
1683
+
1684
+ > Stability: 1.0 - Early development
1685
+
1686
+ An object whose methods are used to configure global test runner settings.
1687
+
1688
+ ### ` config.setDefaultSnapshotSerializers(serializers) `
1689
+
1690
+ <!-- YAML
1691
+ added: REPLACEME
1692
+ -->
1693
+
1694
+ > Stability: 1.0 - Early development
1695
+
1696
+ * ` serializers ` {Array} An array of synchronous functions used as the default
1697
+ serializers for snapshot tests.
1698
+
1699
+ This function is used to customize the default serialization mechanism used by
1700
+ the test runner. By default, the test runner performs serialization by calling
1701
+ ` JSON.stringify(value, null, 2) ` on the provided value. ` JSON.stringify() ` does
1702
+ have limitations regarding circular structures and supported data types. If a
1703
+ more robust serialization mechanism is required, this function should be used.
1704
+
1705
+ ### ` config.setResolveSnapshotPath(fn) `
1706
+
1707
+ <!-- YAML
1708
+ added: REPLACEME
1709
+ -->
1710
+
1711
+ > Stability: 1.0 - Early development
1712
+
1713
+ * ` fn ` {Function} A function used to compute the location of the snapshot file.
1714
+ The function receives the path of the entry point file as its only argument.
1715
+ If the entry point is not associated with a file (for example in the REPL),
1716
+ the input is undefined. ` fn() ` must return a string specifying the location of
1717
+ the snapshot file.
1718
+
1719
+ This function is used to customize the location of the snapshot file used for
1720
+ snapshot testing. By default, the snapshot filename is the same as the entry
1721
+ point filename with a ` .snapshot ` file extension.
1722
+
1623
1723
## Class: ` MockFunctionContext `
1624
1724
1625
1725
<!-- YAML
@@ -3048,6 +3148,41 @@ test('test', (t) => {
3048
3148
});
3049
3149
```
3050
3150
3151
+ #### ` context.assert.snapshot(actual[, options]) `
3152
+
3153
+ <!-- YAML
3154
+ added: REPLACEME
3155
+ -->
3156
+
3157
+ > Stability: 1.0 - Early development
3158
+
3159
+ * ` actual ` {any} A value to serialize to a string. If Node.js was started with
3160
+ the [ ` --test-update-snapshots ` ] [ ] flag, the serialized value is written to
3161
+ the snapshot file. Otherwise, the serialized value is compared to the
3162
+ corresponding value in the existing snapshot file.
3163
+ * ` options ` {Object} Optional configuration options. The following properties
3164
+ are supported:
3165
+ * ` serializers ` {Array} An array of synchronous functions used to serialize
3166
+ ` actual ` into a string. ` actual ` is passed as the only argument to the first
3167
+ serializer function. The return value of each serializer is passed as input
3168
+ to the next serializer. Once all serializers have run, the resulting value
3169
+ is coerced to a string. ** Default:** If no serializers are provided, the
3170
+ test runner's default serializers are used.
3171
+
3172
+ This function implements assertions for snapshot testing.
3173
+
3174
+ ``` js
3175
+ test (' snapshot test with default serialization' , (t ) => {
3176
+ t .assert .snapshot ({ value1: 1 , value2: 2 });
3177
+ });
3178
+
3179
+ test (' snapshot test with custom serialization' , (t ) => {
3180
+ t .assert .snapshot ({ value3: 3 , value4: 4 }, {
3181
+ serializers: [(value ) => JSON .stringify (value)]
3182
+ });
3183
+ });
3184
+ ```
3185
+
3051
3186
### ` context.diagnostic(message) `
3052
3187
3053
3188
<!-- YAML
@@ -3326,13 +3461,15 @@ Can be used to abort test subtasks when the test has been aborted.
3326
3461
[ TAP ] : https://testanything.org/
3327
3462
[ TTY ] : tty.md
3328
3463
[ `--experimental-test-coverage` ] : cli.md#--experimental-test-coverage
3464
+ [ `--experimental-test-snapshots` ] : cli.md#--experimental-test-snapshots
3329
3465
[ `--import` ] : cli.md#--importmodule
3330
3466
[ `--test-concurrency` ] : cli.md#--test-concurrency
3331
3467
[ `--test-name-pattern` ] : cli.md#--test-name-pattern
3332
3468
[ `--test-only` ] : cli.md#--test-only
3333
3469
[ `--test-reporter-destination` ] : cli.md#--test-reporter-destination
3334
3470
[ `--test-reporter` ] : cli.md#--test-reporter
3335
3471
[ `--test-skip-pattern` ] : cli.md#--test-skip-pattern
3472
+ [ `--test-update-snapshots` ] : cli.md#--test-update-snapshots
3336
3473
[ `--test` ] : cli.md#--test
3337
3474
[ `MockFunctionContext` ] : #class-mockfunctioncontext
3338
3475
[ `MockTimers` ] : #class-mocktimers
0 commit comments