diff --git a/.changeset/sweet-experts-allow.md b/.changeset/sweet-experts-allow.md new file mode 100644 index 000000000..6565ff681 --- /dev/null +++ b/.changeset/sweet-experts-allow.md @@ -0,0 +1,5 @@ +--- +"mobx": patch +--- + +fix: action transparently forwards toString of underlying function diff --git a/packages/mobx/__tests__/v5/base/action.js b/packages/mobx/__tests__/v5/base/action.js index cfe782d11..570257825 100644 --- a/packages/mobx/__tests__/v5/base/action.js +++ b/packages/mobx/__tests__/v5/base/action.js @@ -646,3 +646,18 @@ test("auto action should not update state from inside a derivation", async () => }) d() }) + +test("action forwards toString of underlying function", async () => { + const fn = () => { + /* not actually doing anything */ + } + fn.a = 42 + fn.toString = function () { + return `toString referencing this, a=${this.a}` + } + + const act = mobx.action(fn) + + expect(fn.toString()).toBe("toString referencing this, a=42") + expect(act.toString()).toBe("toString referencing this, a=42") +}) diff --git a/packages/mobx/src/core/action.ts b/packages/mobx/src/core/action.ts index 57c20cb91..ec8ab01e6 100644 --- a/packages/mobx/src/core/action.ts +++ b/packages/mobx/src/core/action.ts @@ -50,6 +50,7 @@ export function createAction( return executeAction(actionName, autoAction, fn, ref || this, arguments) } res.isMobxAction = true + res.toString = () => fn.toString() if (isFunctionNameConfigurable) { tmpNameDescriptor.value = actionName defineProperty(res, "name", tmpNameDescriptor)