@effectionx/converge 0.1.0 → 0.1.2
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.
- package/LICENSE +9 -0
- package/README.md +199 -0
- package/images/assertion-after.png +0 -0
- package/images/convergent-assertion.png +0 -0
- package/images/false-negative.png +0 -0
- package/package.json +5 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2024 The Frontside Software, Inc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# @effectionx/converge
|
|
2
|
+
|
|
3
|
+
Recognize a desired state and synchronize on when that state has been achieved.
|
|
4
|
+
|
|
5
|
+
> This package is a port of [@bigtest/convergence](https://github.com/bigtestjs/convergence) adapted for [Effection](https://frontside.com/effection) structured concurrency.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Why Convergence?
|
|
10
|
+
|
|
11
|
+
Let's say you want to write an assertion to verify a simple cause and
|
|
12
|
+
effect: when a certain button is clicked, a dialog appears containing
|
|
13
|
+
some text that gets loaded from the network.
|
|
14
|
+
|
|
15
|
+
In order to do this, you have to make sure that your assertion runs
|
|
16
|
+
_after_ the effect you're testing has been realized.
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
If not, then you could end up with a false negative, or "flaky test"
|
|
21
|
+
because you ran the assertion too early. If you'd only waited a little
|
|
22
|
+
bit longer, then your test would have passed. So sad!
|
|
23
|
+
|
|
24
|
+

|
|
25
|
+
|
|
26
|
+
In fact, test flakiness is the reason most people shy away from
|
|
27
|
+
writing big tests in JavaScript in the first place. It seems almost
|
|
28
|
+
impossible to write robust tests without having visibility into the
|
|
29
|
+
internals of your runtime so that you can manually synchronize on
|
|
30
|
+
things like rendering and data loading. Unfortunately, those can be a
|
|
31
|
+
moving target, and worse, they couple you to your framework.
|
|
32
|
+
|
|
33
|
+
But what if instead of trying to run our assertions at just the right
|
|
34
|
+
time, we ran them _many_ times until they either pass or we decide to
|
|
35
|
+
give up?
|
|
36
|
+
|
|
37
|
+

|
|
38
|
+
|
|
39
|
+
This is the essence of what `@effectionx/converge` provides:
|
|
40
|
+
repeatedly testing for a condition and then allowing code to run when
|
|
41
|
+
that condition has been met.
|
|
42
|
+
|
|
43
|
+
And it isn't just for assertions either. Because it is a general
|
|
44
|
+
mechanism for synchronizing on any observed state, it can be used to
|
|
45
|
+
properly time test setup and teardown as well.
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm install @effectionx/converge
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
### `when(assertion, options?)`
|
|
60
|
+
|
|
61
|
+
Converges when the assertion passes _within_ the timeout period. The
|
|
62
|
+
assertion runs repeatedly (every 10ms by default) and is considered
|
|
63
|
+
passing when it does not throw or return `false`. If it never passes
|
|
64
|
+
within the timeout, the operation throws with the last error.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { when } from "@effectionx/converge";
|
|
68
|
+
|
|
69
|
+
// Wait for a condition to become true
|
|
70
|
+
let { value } = yield* when(function* () {
|
|
71
|
+
if (total !== 100) throw new Error("not ready");
|
|
72
|
+
return total;
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// With custom timeout
|
|
76
|
+
yield* when(
|
|
77
|
+
function* () {
|
|
78
|
+
if (!element.isVisible) throw new Error("not visible");
|
|
79
|
+
},
|
|
80
|
+
{ timeout: 5000 },
|
|
81
|
+
);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `always(assertion, options?)`
|
|
85
|
+
|
|
86
|
+
Converges when the assertion passes _throughout_ the timeout period.
|
|
87
|
+
Like `when()`, the assertion runs repeatedly, but it must pass
|
|
88
|
+
consistently for the entire duration. If it fails at any point, the
|
|
89
|
+
operation throws immediately.
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { always } from "@effectionx/converge";
|
|
93
|
+
|
|
94
|
+
// Verify a condition remains true
|
|
95
|
+
yield* always(function* () {
|
|
96
|
+
if (counter >= 100) throw new Error("counter exceeded limit");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// With custom timeout
|
|
100
|
+
yield* always(
|
|
101
|
+
function* () {
|
|
102
|
+
if (!connection.isAlive) throw new Error("connection lost");
|
|
103
|
+
},
|
|
104
|
+
{ timeout: 5000 },
|
|
105
|
+
);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## Options
|
|
111
|
+
|
|
112
|
+
Both `when` and `always` accept an options object:
|
|
113
|
+
|
|
114
|
+
| Option | Type | Default | Description |
|
|
115
|
+
| ---------- | -------- | ------------------------------ | --------------------------------------------- |
|
|
116
|
+
| `timeout` | `number` | `2000` (when) / `200` (always) | Maximum time to wait in milliseconds |
|
|
117
|
+
| `interval` | `number` | `10` | Time between assertion retries in milliseconds |
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Stats Object
|
|
122
|
+
|
|
123
|
+
Both functions return a `ConvergeStats` object with timing and execution info:
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
interface ConvergeStats<T> {
|
|
127
|
+
start: number; // Timestamp when convergence started
|
|
128
|
+
end: number; // Timestamp when convergence ended
|
|
129
|
+
elapsed: number; // Milliseconds the convergence took
|
|
130
|
+
runs: number; // Number of times the assertion was executed
|
|
131
|
+
timeout: number; // The timeout that was configured
|
|
132
|
+
interval: number; // The interval that was configured
|
|
133
|
+
value: T; // The return value from the assertion
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Example:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
let stats = yield* when(
|
|
141
|
+
function* () {
|
|
142
|
+
return yield* fetchData();
|
|
143
|
+
},
|
|
144
|
+
{ timeout: 5000 },
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
console.log(`Converged in ${stats.elapsed}ms after ${stats.runs} attempts`);
|
|
148
|
+
console.log(stats.value); // the fetched data
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## Examples
|
|
154
|
+
|
|
155
|
+
### Waiting for an element to appear
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
yield* when(function* () {
|
|
159
|
+
let element = document.querySelector('[data-test-id="dialog"]');
|
|
160
|
+
if (!element) throw new Error("dialog not found");
|
|
161
|
+
return element;
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Verifying a value remains stable
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
yield* always(
|
|
169
|
+
function* () {
|
|
170
|
+
if (connection.status !== "connected") {
|
|
171
|
+
throw new Error("connection dropped");
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
{ timeout: 1000 },
|
|
175
|
+
);
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Using with file system operations
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { when } from "@effectionx/converge";
|
|
182
|
+
import { access } from "node:fs/promises";
|
|
183
|
+
import { until } from "effection";
|
|
184
|
+
|
|
185
|
+
// Wait for a file to exist
|
|
186
|
+
yield* when(
|
|
187
|
+
function* () {
|
|
188
|
+
let exists = yield* until(
|
|
189
|
+
access(filePath).then(
|
|
190
|
+
() => true,
|
|
191
|
+
() => false,
|
|
192
|
+
),
|
|
193
|
+
);
|
|
194
|
+
if (!exists) throw new Error("file not found");
|
|
195
|
+
return true;
|
|
196
|
+
},
|
|
197
|
+
{ timeout: 10000 },
|
|
198
|
+
);
|
|
199
|
+
```
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effectionx/converge",
|
|
3
|
-
"
|
|
3
|
+
"description": "Poll and wait for conditions to be met with automatic retry and timeout",
|
|
4
|
+
"version": "0.1.2",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"main": "./dist/mod.js",
|
|
6
7
|
"types": "./dist/mod.d.ts",
|
|
@@ -14,7 +15,7 @@
|
|
|
14
15
|
"effection": "^3 || ^4"
|
|
15
16
|
},
|
|
16
17
|
"dependencies": {
|
|
17
|
-
"@effectionx/timebox": "
|
|
18
|
+
"@effectionx/timebox": "0.4.1"
|
|
18
19
|
},
|
|
19
20
|
"license": "MIT",
|
|
20
21
|
"author": "engineering@frontside.com",
|
|
@@ -30,6 +31,6 @@
|
|
|
30
31
|
},
|
|
31
32
|
"sideEffects": false,
|
|
32
33
|
"devDependencies": {
|
|
33
|
-
"@effectionx/bdd": "
|
|
34
|
+
"@effectionx/bdd": "0.4.2"
|
|
34
35
|
}
|
|
35
|
-
}
|
|
36
|
+
}
|