diff --git a/src/core/reaction.ts b/src/core/reaction.ts index 990df53ed..a6293fcd5 100644 --- a/src/core/reaction.ts +++ b/src/core/reaction.ts @@ -166,7 +166,8 @@ export function runReactions() { // we converge to no remaining reactions after a while. while (allReactions.length > 0) { if (++iterations === MAX_REACTION_ITERATIONS) - throw new Error("Reaction doesn't converge to a stable state. Probably there is a cycle in the reactive function: " + allReactions[0].toString()); + throw new Error(`Reaction doesn't converge to a stable state after ${MAX_REACTION_ITERATIONS} iterations.` + + ` Probably there is a cycle in the reactive function: ${allReactions[0]}`); let remainingReactions = allReactions.splice(0); for (let i = 0, l = remainingReactions.length; i < l; i++) remainingReactions[i].runReaction(); diff --git a/test/reaction.js b/test/reaction.js index 560bfbda4..9eb8370ec 100644 --- a/test/reaction.js +++ b/test/reaction.js @@ -225,7 +225,7 @@ test("#278 do not rerun if expr output doesn't change structurally", t => { var values = []; var d = reaction(mobx.asStructure( - () => users.map(user => user.uppername) + () => users.map(user => user.uppername) ), newValue => { values.push(newValue); }, true) @@ -245,3 +245,22 @@ test("#278 do not rerun if expr output doesn't change structurally", t => { ]); t.end(); }) + +test("throws when the max iterations over reactions are done", t => { + var foo = mobx.observable({ + a: 1, + }); + + mobx.autorun("bar", () => { + var x = foo.a; + foo.a = Math.random(); + }); + + t.throws( + () => foo.a++, + new RegExp("Reaction doesn't converge to a stable state after 100 iterations\\. " + + "Probably there is a cycle in the reactive function: Reaction\\[bar\\]") + ); + mobx.extras.resetGlobalState(); + t.end(); +})