Skip to content

Commit 6d0b577

Browse files
committed
Add tests for non-error throws
1 parent 90a0095 commit 6d0b577

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

packages/core/test/signal.test.tsx

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ describe("computed()", () => {
865865
expect(() => effect(() => a.value)).to.not.throw();
866866
});
867867

868-
it("should store failures and recompute only after a dependency changes", () => {
868+
it("should store thrown errors and recompute only after a dependency changes", () => {
869869
const a = signal(0);
870870
const spy = sinon.spy(() => {
871871
a.value;
@@ -880,6 +880,39 @@ describe("computed()", () => {
880880
expect(spy).to.be.calledTwice;
881881
});
882882

883+
it("should store thrown non-errors and recompute only after a dependency changes", () => {
884+
const a = signal(0);
885+
const spy = sinon.spy();
886+
const c = computed(() => {
887+
a.value;
888+
spy();
889+
throw undefined;
890+
});
891+
892+
try {
893+
c.value;
894+
expect.fail();
895+
} catch (err) {
896+
expect(err).to.be.undefined;
897+
}
898+
try {
899+
c.value;
900+
expect.fail();
901+
} catch (err) {
902+
expect(err).to.be.undefined;
903+
}
904+
expect(spy).to.be.calledOnce;
905+
906+
a.value = 1;
907+
try {
908+
c.value;
909+
expect.fail();
910+
} catch (err) {
911+
expect(err).to.be.undefined;
912+
}
913+
expect(spy).to.be.calledTwice;
914+
});
915+
883916
it("should conditionally unsubscribe from signals", () => {
884917
const a = signal("a");
885918
const b = signal("b");
@@ -1529,14 +1562,25 @@ describe("batch/transaction", () => {
15291562
expect(batch(() => 1)).to.equal(1);
15301563
});
15311564

1532-
it("should throw the error raised from the callback", () => {
1565+
it("should throw errors throws from the callback", () => {
15331566
expect(() =>
15341567
batch(() => {
15351568
throw Error("hello");
15361569
})
15371570
).to.throw("hello");
15381571
});
15391572

1573+
it("should throw non-errors thrown from the callback", () => {
1574+
try {
1575+
batch(() => {
1576+
throw undefined;
1577+
});
1578+
expect.fail();
1579+
} catch (err) {
1580+
expect(err).to.be.undefined;
1581+
}
1582+
});
1583+
15401584
it("should delay writes", () => {
15411585
const a = signal("a");
15421586
const b = signal("b");

0 commit comments

Comments
 (0)