Skip to content

Commit 86616c1

Browse files
authored
fix: action transparently forwards toString of underlying function (#3654)
* fix: action transparently forwards toString of underlying function * changeset
1 parent af64a0a commit 86616c1

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

.changeset/sweet-experts-allow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx": patch
3+
---
4+
5+
fix: action transparently forwards toString of underlying function

packages/mobx/__tests__/v5/base/action.js

+15
Original file line numberDiff line numberDiff line change
@@ -646,3 +646,18 @@ test("auto action should not update state from inside a derivation", async () =>
646646
})
647647
d()
648648
})
649+
650+
test("action forwards toString of underlying function", async () => {
651+
const fn = () => {
652+
/* not actually doing anything */
653+
}
654+
fn.a = 42
655+
fn.toString = function () {
656+
return `toString referencing this, a=${this.a}`
657+
}
658+
659+
const act = mobx.action(fn)
660+
661+
expect(fn.toString()).toBe("toString referencing this, a=42")
662+
expect(act.toString()).toBe("toString referencing this, a=42")
663+
})

packages/mobx/src/core/action.ts

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function createAction(
5050
return executeAction(actionName, autoAction, fn, ref || this, arguments)
5151
}
5252
res.isMobxAction = true
53+
res.toString = () => fn.toString()
5354
if (isFunctionNameConfigurable) {
5455
tmpNameDescriptor.value = actionName
5556
defineProperty(res, "name", tmpNameDescriptor)

0 commit comments

Comments
 (0)