From 6f9ae267456dff4fddea9e47407588584277f951 Mon Sep 17 00:00:00 2001 From: Leon Senft Date: Tue, 10 Dec 2024 13:39:21 -0800 Subject: [PATCH] Test that `computed` can recover from a throwing `equal` implementation --- packages/core/test/signals/computed_spec.ts | 27 ++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/core/test/signals/computed_spec.ts b/packages/core/test/signals/computed_spec.ts index 27b954b7887763..24243bb9d1546d 100644 --- a/packages/core/test/signals/computed_spec.ts +++ b/packages/core/test/signals/computed_spec.ts @@ -7,7 +7,7 @@ */ import {computed, signal} from '@angular/core'; -import {createWatch, ReactiveNode, SIGNAL} from '@angular/core/primitives/signals'; +import {createWatch, ReactiveNode, SIGNAL, defaultEquals} from '@angular/core/primitives/signals'; describe('computed', () => { it('should create computed', () => { @@ -291,5 +291,30 @@ describe('computed', () => { // context either. expect(outerRunCount).toBe(1); }); + + it('should recover from exception', () => { + let shouldThrow = true; + const source = signal(0); + const derived = computed(source, { + equal: (a, b) => { + if (shouldThrow) { + throw new Error('equal'); + } + return defaultEquals(a, b); + }, + }); + + // Initial read doesn't throw because it doesn't call `equal`. + expect(derived()).toBe(0); + + // Update `source` to begin throwing. + source.set(1); + expect(() => derived()).toThrowError('equal'); + + // Stop throwing and update `source` to cause `derived` to recompute. + shouldThrow = false; + source.set(2); + expect(derived()).toBe(2); + }); }); });