From 4d45eefa1cbaae5b1a70ae80eb8c166113c61588 Mon Sep 17 00:00:00 2001 From: Luke Hoban Date: Tue, 8 Jan 2019 11:59:22 -0800 Subject: [PATCH] More helpful toString on Output It is common for users to intentionally or accidentally try to `toSting` an Output expecting to get the underlying value. Instead, they currently get `[Object object]` with no clear reason why. We can't give the actual underlying value because it is only asynchronously availabe, but we can provide a more helpful placeholder value with a pointer to documentation about how to work with Outputs. Part of #2206. --- sdk/nodejs/resource.ts | 7 +++++++ sdk/nodejs/tests/output.spec.ts | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/sdk/nodejs/resource.ts b/sdk/nodejs/resource.ts index 17362d743..a75c9fde3 100644 --- a/sdk/nodejs/resource.ts +++ b/sdk/nodejs/resource.ts @@ -480,6 +480,13 @@ export class Output { To manipulate the value of this Output, use '.apply' instead.`); }; } + + // We override `toString` to provide more useful guidance when `Output`s are converted to strings, instead of just + // printing `[object Object]`. + toString() { + return "<<< Warning: called 'toString' on an Output, use 'apply' instead: https://pulumi.io/help/outputs >>>"; + } + } /** diff --git a/sdk/nodejs/tests/output.spec.ts b/sdk/nodejs/tests/output.spec.ts index 65e9ba96b..a5d91e973 100644 --- a/sdk/nodejs/tests/output.spec.ts +++ b/sdk/nodejs/tests/output.spec.ts @@ -64,4 +64,11 @@ describe("output", () => { assert.fail("Should not read here"); })); + + it("toStrings to helpful warnning.", asyncTest(async () => { + const output = new resource.Output(new Set(), Promise.resolve("outer"), Promise.resolve(true)); + const expected = "<<< Warning: called 'toString' on an Output, use 'apply' instead: https://pulumi.io/help/outputs >>>"; + assert.equal("" + output, expected); + assert.equal(`${output}`, expected); + })); });