Skip to content

Commit bf42bb1

Browse files
test: add a getter that throws two levels up, assert only what is seen last
Co-authored-by: Johnson Chu <johnsoncodehk@users.noreply.github.com>
1 parent 0ee1648 commit bf42bb1

1 file changed

Lines changed: 58 additions & 7 deletions

File tree

‎tests/errors.spec.ts‎

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const attempt = (fn: () => void) => {
88
};
99

1010
// What an exception leaves behind. None of these say what a read or a write
11-
// should do while something throws, only that it is over once the cause is
12-
// gone: a later change still reaches whoever depends on it.
11+
// should do while something throws, or which effects still run in a flush
12+
// that fails, only that it is over once the cause is gone: a later change
13+
// still reaches whoever depends on it.
1314

1415
test('an effect that threw runs again', () => {
1516
const a = signal(0);
@@ -24,7 +25,7 @@ test('an effect that threw runs again', () => {
2425
attempt(() => a(1));
2526
a(2);
2627

27-
expect(seen).toEqual([0, 1, 2]);
28+
expect(seen.pop()).toBe(2);
2829
});
2930

3031
test('an effect skipped because another one threw runs on the next write', () => {
@@ -40,7 +41,7 @@ test('an effect skipped because another one threw runs on the next write', () =>
4041
attempt(() => a(1));
4142
a(2);
4243

43-
expect(seen).toEqual([0, 2]);
44+
expect(seen.pop()).toBe(2);
4445
});
4546

4647
// Fails: the effect was taken off the queue to run, and the getter threw
@@ -61,7 +62,7 @@ test.fails('an effect runs again after a getter it depends on threw', () => {
6162
attempt(() => a(0));
6263
a(2);
6364

64-
expect(seen).toEqual([1, 2]);
65+
expect(seen.pop()).toBe(2);
6566
});
6667

6768
// Fails: the second effect is dropped from the queue when the first throws.
@@ -81,7 +82,7 @@ test.fails('an effect behind a computed, skipped because another one threw, runs
8182
attempt(() => a(1));
8283
a(2);
8384

84-
expect(seen).toEqual([1, 3]);
85+
expect(seen.pop()).toBe(3);
8586
});
8687

8788
// Fails: a read that throws leaves before the dependency is recorded. The
@@ -128,5 +129,55 @@ test.fails('an effect notices a change in a getter that threw while it was readi
128129
attempt(endBatch);
129130
a(5);
130131

131-
expect(seen).toEqual([1, 5]);
132+
expect(seen.pop()).toBe(5);
133+
});
134+
135+
// Fails: the check that walks from a reader up to what changed is abandoned
136+
// where the getter throws. The computed it had passed stays marked as
137+
// possibly changed while the reader ends up marked as up to date, and a later
138+
// write stops at the first node that is marked already.
139+
test.fails('a computed notices a change in a getter that threw two levels up', () => {
140+
const a = signal(1);
141+
const b = signal(0);
142+
const inner = computed(() => {
143+
if (a() === 0) {
144+
throw new Error('zero');
145+
}
146+
return a();
147+
});
148+
const mid = computed(() => inner());
149+
const outer = computed(() => b() + mid());
150+
151+
expect(outer()).toBe(1);
152+
a(0);
153+
b(1);
154+
attempt(outer);
155+
a(5);
156+
157+
expect(outer()).toBe(6);
158+
});
159+
160+
test.fails('an effect notices a change in a getter that threw two levels up', () => {
161+
const a = signal(1);
162+
const b = signal(0);
163+
const seen: number[] = [];
164+
const inner = computed(() => {
165+
if (a() === 0) {
166+
throw new Error('zero');
167+
}
168+
return a();
169+
});
170+
const mid = computed(() => inner());
171+
172+
effect(() => {
173+
b();
174+
seen.push(mid());
175+
});
176+
startBatch();
177+
a(0);
178+
b(1);
179+
attempt(endBatch);
180+
a(5);
181+
182+
expect(seen.pop()).toBe(5);
132183
});

0 commit comments

Comments
 (0)