@jsenv/snapshot 2.15.9 → 2.15.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +6 -6
  2. package/readme.md +148 -55
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/snapshot",
3
- "version": "2.15.9",
3
+ "version": "2.15.11",
4
4
  "description": "Snapshot testing",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -32,13 +32,13 @@
32
32
  "sideEffects": false,
33
33
  "dependencies": {
34
34
  "@jsenv/assert": "4.5.3",
35
- "@jsenv/ast": "6.7.1",
35
+ "@jsenv/ast": "6.7.3",
36
36
  "@jsenv/url-meta": "8.5.7",
37
37
  "@jsenv/exception": "1.2.1",
38
- "@jsenv/humanize": "1.5.2",
39
- "@jsenv/filesystem": "4.15.2",
40
- "@jsenv/terminal-recorder": "1.5.17",
41
- "@jsenv/urls": "2.7.4",
38
+ "@jsenv/humanize": "1.6.0",
39
+ "@jsenv/filesystem": "4.15.3",
40
+ "@jsenv/terminal-recorder": "1.5.20",
41
+ "@jsenv/urls": "2.8.0",
42
42
  "@jsenv/utils": "2.3.1",
43
43
  "ansi-regex": "6.1.0",
44
44
  "pixelmatch": "7.1.0",
package/readme.md CHANGED
@@ -1,9 +1,33 @@
1
- # snapshot
1
+ # @jsenv/snapshot
2
2
 
3
3
  [![npm package](https://img.shields.io/npm/v/@jsenv/snapshot.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/snapshot)
4
4
 
5
5
  A powerful snapshot testing tool for JavaScript applications.
6
6
 
7
+ 📸 Create readable, reliable test snapshots
8
+ 🔄 Document code behavior with markdown
9
+ 📊 Track changes in code output over time
10
+ 🧩 Normalize fluctuating values for stable comparisons
11
+
12
+ ## Table of Contents
13
+
14
+ - [@jsenv/snapshot](#jsenvsnapshot)
15
+ - [Table of Contents](#table-of-contents)
16
+ - [Introduction to Snapshot Testing](#introduction-to-snapshot-testing)
17
+ - [Installation](#installation)
18
+ - [How `@jsenv/snapshot` Works](#how-jsenvsnapshot-works)
19
+ - [API Reference](#api-reference)
20
+ - [snapshotTests(testFileUrl, fnRegisteringTests, options)](#snapshotteststestfileurl-fnregisteringtests-options)
21
+ - [when radius is 10](#when-radius-is-10)
22
+ - [when radius is null](#when-radius-is-null)
23
+ - [Why Use snapshotTests?](#why-use-snapshottests)
24
+ - [takeFileSnapshot(fileUrl)](#takefilesnapshotfileurl)
25
+ - [takeDirectorySnapshot(directoryUrl)](#takedirectorysnapshotdirectoryurl)
26
+ - [Stable Snapshots Across Environments](#stable-snapshots-across-environments)
27
+ - [Advanced Examples](#advanced-examples)
28
+ - [Compatibility](#compatibility)
29
+ - [Contributing](#contributing)
30
+
7
31
  ## Introduction to Snapshot Testing
8
32
 
9
33
  Snapshot testing is a technique that:
@@ -17,6 +41,12 @@ Snapshot testing is a technique that:
17
41
 
18
42
  This approach ensures your code continues to behave as expected by verifying its outputs remain consistent over time.
19
43
 
44
+ ## Installation
45
+
46
+ ```console
47
+ npm install --save-dev @jsenv/snapshot
48
+ ```
49
+
20
50
  ## How `@jsenv/snapshot` Works
21
51
 
22
52
  When running tests:
@@ -26,10 +56,111 @@ When running tests:
26
56
  - In CI environments (`process.env.CI` is set): An error is thrown if differences are detected
27
57
  - Locally: No error is thrown, allowing you to review changes with tools like git diff
28
58
 
29
- > **Note**: All functions accept a throwWhenDiff parameter to force errors even in local environments.
59
+ > **Note**: All functions accept a `throwWhenDiff` parameter to force errors even in local environments.
30
60
 
31
61
  ## API Reference
32
62
 
63
+ ### snapshotTests(testFileUrl, fnRegisteringTests, options)
64
+
65
+ The most powerful feature of this library - creates readable markdown snapshots of test executions.
66
+
67
+ ```js
68
+ import { snapshotTests } from "@jsenv/snapshot";
69
+
70
+ const getCircleArea = (circleRadius) => {
71
+ if (isNaN(circleRadius)) {
72
+ throw new TypeError(
73
+ `circleRadius must be a number, received ${circleRadius}`,
74
+ );
75
+ }
76
+ return circleRadius * circleRadius * Math.PI;
77
+ };
78
+
79
+ await snapshotTests(import.meta.url, ({ test }) => {
80
+ test("when radius is 2", () => {
81
+ return getCircleArea(2);
82
+ });
83
+
84
+ test("when radius is 10", () => {
85
+ return getCircleArea(10);
86
+ });
87
+
88
+ test("when radius is null", () => {
89
+ try {
90
+ return getCircleArea(null);
91
+ } catch (e) {
92
+ return e;
93
+ }
94
+ });
95
+ });
96
+ ```
97
+
98
+ This generates a markdown file documenting how your code behaves in different scenarios:
99
+
100
+ ````markdown
101
+ # getCircleArea
102
+
103
+ ## when radius is 2
104
+
105
+ ```js
106
+ getCircleArea(2);
107
+ ```
108
+ ````
109
+
110
+ Returns:
111
+
112
+ ```js
113
+ 12.566370614359172;
114
+ ```
115
+
116
+ ## when radius is 10
117
+
118
+ ```js
119
+ getCircleArea(10);
120
+ ```
121
+
122
+ Returns:
123
+
124
+ ```js
125
+ 314.1592653589793;
126
+ ```
127
+
128
+ ## when radius is null
129
+
130
+ ```js
131
+ getCircleArea(null);
132
+ ```
133
+
134
+ Throws:
135
+
136
+ ```
137
+ TypeError: circleRadius must be a number, received null
138
+ ```
139
+
140
+ ````
141
+
142
+ See a full example at [./docs/\_circle_area.test.js/circle_area.test.js.md](./docs/_circle_area.test.js/circle_area.test.js.md)
143
+
144
+ #### snapshotTests Options
145
+
146
+ ```js
147
+ await snapshotTests(import.meta.url, fnRegisteringTests, {
148
+ throwWhenDiff: true, // Force error on snapshot differences (default: false in local, true in CI)
149
+ formatValue: (value) => {}, // Custom formatter for values
150
+ trackingConfig: {}, // Track specific resources like network requests or file operations
151
+ });
152
+ ````
153
+
154
+ #### Why Use snapshotTests?
155
+
156
+ - **Assertion-free testing**: Simply call your functions and let the snapshots document their behavior
157
+ - **Self-documenting tests**: Markdown files serve as both test validation and documentation
158
+ - **Visual change reviews**: Code changes are reflected in snapshots, making reviews easy
159
+ - **Side effect tracking**: Automatically captures and documents:
160
+ - Console logs ([example](./docs/_log.test.js/log.test.js.md))
161
+ - Filesystem operations ([example](./docs/_filesystem.test.js/filesystem.test.js.md))
162
+ - And more
163
+
33
164
  ### takeFileSnapshot(fileUrl)
34
165
 
35
166
  Captures and compares the state of a specific file.
@@ -40,7 +171,7 @@ import { takeFileSnapshot } from "@jsenv/snapshot";
40
171
 
41
172
  const fileTxtUrl = new URL("./file.txt", import.meta.url);
42
173
  const writeFileTxt = (content) => {
43
- writeFileSync(writeFileTxt, content);
174
+ writeFileSync(fileTxtUrl, content);
44
175
  };
45
176
 
46
177
  // take snapshot of "./file.txt"
@@ -66,66 +197,22 @@ const writeManyFiles = () => {
66
197
 
67
198
  // take snapshot of "./dir/"
68
199
  const directorySnapshot = takeDirectorySnapshot(directoryUrl);
69
- writeFileTxt(directoryUrl);
200
+ writeManyFiles();
70
201
  // compare the state of "./dir/" with previous version
71
202
  directorySnapshot.compare();
72
203
  ```
73
204
 
74
- ### snapshotTests(testFileUrl, fnRegistertingTests, options)
75
-
76
- The most powerful feature of this library - creates readable markdown snapshots of test executions.
77
-
78
- ```js
79
- import { snapshotTests } from "@jsenv/snapshot";
80
-
81
- const getCircleArea = (circleRadius) => {
82
- if (isNaN(circleRadius)) {
83
- throw new TypeError(
84
- `circleRadius must be a number, received ${circleRadius}`,
85
- );
86
- }
87
- return circleRadius * circleRadius * Math.PI;
88
- };
89
-
90
- await snapshotTests(import.meta.url, ({ test }) => {
91
- test("when radius is 2", () => {
92
- return getCircleArea(2);
93
- });
94
-
95
- test("when radius is 10", () => {
96
- return getCircleArea(10);
97
- });
98
-
99
- test("when radius is null", () => {
100
- return getCircleArea(null);
101
- });
102
- });
103
- ```
104
-
105
- This generates a markdown file documenting how your code behaves in different scenarios.
106
- See an example at [./docs/\_circle_area.test.js/circle_area.test.js.md](./docs/_circle_area.test.js/circle_area.test.js.md)
107
-
108
- ## Why Use snapshotTests?
109
-
110
- - **Assertion-free testing**: Simply call your functions and let the snapshots document their behavior
111
- - **Self-documenting tests**: Markdown files serve as both test validation and documentation
112
- - **Visual change reviews**: Code changes are reflected in snapshots, making reviews easy
113
- - **Side effect tracking**: Automatically captures and documents:
114
-
115
- - Console logs [example](./docs/_log.test.js/log.test.js.md)
116
- - Filesystem operations [example](./docs/_filesystem.test.js/filesystem.test.js.md)
117
- - And more
118
-
119
- - Fluctuating values are replaced with stable values, see [#Fluctuating values replacement](#fluctuating-values-replacement)
120
-
121
205
  ## Stable Snapshots Across Environments
122
206
 
123
207
  To ensure snapshots remain consistent across different machines and CI environments, `@jsenv/snapshot` automatically normalizes fluctuating values:
124
208
 
125
- - Time values like "2s" become "Xs"
126
- - Filesystem paths are standardized
127
- - Network ports in URLs are removed
128
- - And much more...
209
+ | Fluctuating Value | Stabilized As |
210
+ | ----------------- | ----------------------------- |
211
+ | Time durations | `"Xs"` instead of `"2.34s"` |
212
+ | Filesystem paths | Platform-independent paths |
213
+ | Network ports | Removed from URLs |
214
+ | Random IDs | Consistent placeholder values |
215
+ | Stack traces | Simplified and normalized |
129
216
 
130
217
  This ensures your snapshot tests remain stable regardless of when or where they run.
131
218
 
@@ -134,6 +221,12 @@ This ensures your snapshot tests remain stable regardless of when or where they
134
221
  - Testing complex assertion behavior: [@jsenv/assert/tests/array.test.js.md](../assert/tests/_array.test.js/array.test.js.md)
135
222
  - Testing server-side builds with browser execution: [@jsenv/core/tests/script_type_module_basic.test.mjs](../../../tests/build/basics/script_type_module_basic/_script_type_module_basic.test.mjs/script_type_module_basic.test.mjs.md)
136
223
 
224
+ ## Compatibility
225
+
226
+ - Node.js: 16.x and above
227
+ - Works with most JavaScript test runners
228
+ - Compatible with ESM and CommonJS modules
229
+
137
230
  ## Contributing
138
231
 
139
232
  If you encounter unstable snapshots due to fluctuating values not being properly normalized, please open an issue or submit a pull request.