1 | # snapshot-context
|
2 |
|
3 | [![npm version](https://badge.fury.io/js/snapshot-context.svg)](https://www.npmjs.com/package/snapshot-context)
|
4 |
|
5 | `snapshot-context` is Context For Snapshot Testing In _Zoroaster_.
|
6 |
|
7 | ```sh
|
8 | yarn add -DE snapshot-context
|
9 | ```
|
10 |
|
11 | >**Since Zoroaster 3.8.5, the snapshot functionality is enabled by returning the result of the test, there's no need to install this package manually.**
|
12 |
|
13 | ## Table Of Contents
|
14 |
|
15 | - [Table Of Contents](#table-of-contents)
|
16 | - [class `SnapshotContext`](#class-snapshotcontext)
|
17 | - [API](#api)
|
18 | * [setDir(path:string)](#setdirpathstring)
|
19 | * [async test(path:string, actual: string|object)](#async-testpathstring-actual-stringobject)
|
20 | - [Copyright & License](#copyright--license)
|
21 |
|
22 | <div align="center"><a href="#table-of-contents">
|
23 | <img src="/.documentary/section-breaks/0.svg?sanitize=true">
|
24 | </a></div>
|
25 |
|
26 | ## class `SnapshotContext`
|
27 |
|
28 | The snapshot context should be passed in to a [`Zoroaster`](https://github.com/contexttesting/zoroaster) test suite in the `context` property.
|
29 |
|
30 | ```js
|
31 | import { fork } from 'spawncommand'
|
32 | import SnapshotContext from 'snapshot-context'
|
33 | import Context from '../context'
|
34 |
|
35 | /**
|
36 | * @type {Object.<string, (c: Context, s: SnapshotContext)>} */
|
37 | const T = {
|
38 | context:[
|
39 | context,
|
40 | snapshotContext,
|
41 | ],
|
42 | async 'produces correct output'({ TEST_SUITE_PATH }, { test, setDir }) {
|
43 | setDir(SNAPSHOT_DIR)
|
44 | const { promise } = fork(BIN, [TEST_SUITE_PATH, '--babel'], {
|
45 | stdio: 'pipe',
|
46 | })
|
47 | const { stdout } = await promise
|
48 | await test('integration-stdout.txt', s)
|
49 | },
|
50 | }
|
51 |
|
52 | export default T
|
53 | ```
|
54 |
|
55 | <div align="center"><a href="#table-of-contents">
|
56 | <img src="/.documentary/section-breaks/1.svg?sanitize=true">
|
57 | </a></div>
|
58 |
|
59 | ## API
|
60 |
|
61 | There is a set of methods made available by the API.
|
62 |
|
63 | <div align="center"><a href="#table-of-contents">
|
64 | <img src="/.documentary/section-breaks/2.svg?sanitize=true" width="15">
|
65 | </a></div>
|
66 |
|
67 | ### setDir(path:string)
|
68 |
|
69 | Sets the root directory to save to and read snapshots from. Otherwise, an absolute path to the file can be passed. By default, the **`test/snapshot`** directory is used.
|
70 |
|
71 | ```js
|
72 | import { resolve } from 'path'
|
73 | import snapshotContext, { SnapshotContext } from 'snapshot-context' // eslint-disable-line
|
74 | import erte from '../../src' // tested lib
|
75 |
|
76 | const SNAPSHOT_DIR = resolve(__dirname, '../snapshot')
|
77 |
|
78 | const stringContext = {
|
79 | /**
|
80 | * A string with 1 new line
|
81 | */
|
82 | s: 'I am all in a sea of wonders.\nI doubt;\nI fear;\n',
|
83 | /**
|
84 | * A string with 2 new lines
|
85 | */
|
86 | t: 'I am all in a sea of wonders.\n\nI doubt;\n\nI fear;\n\n',
|
87 | }
|
88 |
|
89 | /** @type {Object.<string, (ctxString: stringContext ctx: SnapshotContext)>} */
|
90 | const T = {
|
91 | context: [
|
92 | function () {
|
93 | Object.assign(this, stringContext)
|
94 | },
|
95 | snapshotContext,
|
96 | ],
|
97 | async 'replaces new lines'({ s, t }, { setDir, test }) {
|
98 | setDir(SNAPSHOT_DIR)
|
99 | const res = erte(s, t)
|
100 | await test('new-lines.txt', res)
|
101 | },
|
102 | // absolute path without set-dir
|
103 | async 'replaces reverse new lines'({ s, t }, { test }) {
|
104 | const res = erte(t, s)
|
105 | const path = resolve(SNAPSHOT_DIR, 'new-lines-reverse.txt')
|
106 | await test(path, res)
|
107 | },
|
108 | }
|
109 |
|
110 | export default T
|
111 | ```
|
112 |
|
113 | <div align="center"><a href="#table-of-contents">
|
114 | <img src="/.documentary/section-breaks/3.svg?sanitize=true" width="15">
|
115 | </a></div>
|
116 |
|
117 | ### async test(path:string, actual: string|object)
|
118 |
|
119 | Test whether a snapshot matches the one saved in the path. An equality of strings is asserted, and objects are deep-equal tested. Objects are serialised as `JSON` for writing, and back when reading.
|
120 |
|
121 | - If there's no snapshot file existing, the user is prompted to answer the `save snapshot` question with a `y` to confirm new snapshot.
|
122 | - The path to the file will be ensured, so that all directories in the path are made.
|
123 | - The error stack will start at the point where `test` is called.
|
124 |
|
125 | ![test cli demo](doc/test.gif)
|
126 |
|
127 | The difference between objects will be shown using `deepEqual` from the `assert-diff`
|
128 |
|
129 | ![object diff](doc/object-diff.png)
|
130 |
|
131 | The difference between strings will be highlighted with `erte`.
|
132 |
|
133 | ![string diff](doc/string-diff.png)
|
134 |
|
135 | <div align="center"><a href="#table-of-contents">
|
136 | <img src="/.documentary/section-breaks/4.svg?sanitize=true">
|
137 | </a></div>
|
138 |
|
139 | ## Copyright & License
|
140 |
|
141 | GNU Affero General Public License v3.0
|
142 |
|
143 | <table>
|
144 | <tr>
|
145 | <th>
|
146 | <a href="https://www.artd.eco">
|
147 | <img width="100" src="https://gitlab.com/uploads/-/system/group/avatar/7454762/artdeco.png"
|
148 | alt="Art Deco">
|
149 | </a>
|
150 | </th>
|
151 | <th>© <a href="https://www.artd.eco">Art Deco™</a> for <a href="https://www.contexttesting.com">ContextTesting</a> 2020</th>
|
152 | <th>
|
153 | <a href="https://www.contexttesting.com">
|
154 | <img src="https://avatars1.githubusercontent.com/u/44418436?s=100" width="100" alt="ContextTesting">
|
155 | </a>
|
156 | </th>
|
157 | <th><a href="LICENSE"><img src=".documentary/agpl-3.0.svg" alt="AGPL-3.0"></a></th>
|
158 | </tr>
|
159 | </table>
|
160 |
|
161 | <div align="center"><a href="#table-of-contents">
|
162 | <img src="/.documentary/section-breaks/-1.svg?sanitize=true">
|
163 | </a></div> |
\ | No newline at end of file |