From bdaba7829da366aabbc81885d84bb2401ab3eaff Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Thu, 11 Feb 2021 09:50:15 +0000 Subject: [PATCH] fix: jsonValue() type is generic (#6865) During the migration to TS we changed `jsonValue` so it returned `>`. This is only true if all the JSON values it returns are objects; but it could return an array, a string, a number, etc. Therefore we make the type generic, setting the default to `unknown`, so the user has control over the type. --- src/common/JSHandle.ts | 6 +++--- test/evaluation.spec.ts | 2 +- test/jshandle.spec.ts | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/common/JSHandle.ts b/src/common/JSHandle.ts index c0ee4070b9a4e..cbe97e1ffe62d 100644 --- a/src/common/JSHandle.ts +++ b/src/common/JSHandle.ts @@ -243,7 +243,7 @@ export class JSHandle { * on the object in page and consequent {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse | JSON.parse} in puppeteer. * **NOTE** The method throws if the referenced object is not stringifiable. */ - async jsonValue(): Promise> { + async jsonValue(): Promise { if (this._remoteObject.objectId) { const response = await this._client.send('Runtime.callFunctionOn', { functionDeclaration: 'function() { return this; }', @@ -251,9 +251,9 @@ export class JSHandle { returnByValue: true, awaitPromise: true, }); - return helper.valueFromRemoteObject(response.result); + return helper.valueFromRemoteObject(response.result) as T; } - return helper.valueFromRemoteObject(this._remoteObject); + return helper.valueFromRemoteObject(this._remoteObject) as T; } /** diff --git a/test/evaluation.spec.ts b/test/evaluation.spec.ts index a2497d9cb19c0..e3803d545d441 100644 --- a/test/evaluation.spec.ts +++ b/test/evaluation.spec.ts @@ -278,7 +278,7 @@ describe('Evaluation specs', function () { const windowHandle = await page.evaluateHandle(() => window); const errorText = await windowHandle - .jsonValue() + .jsonValue() .catch((error_) => error_.message); const error = await page .evaluate<(errorText: string) => Error>((errorText) => { diff --git a/test/jshandle.spec.ts b/test/jshandle.spec.ts index bb135083e8042..7978d4acda5db 100644 --- a/test/jshandle.spec.ts +++ b/test/jshandle.spec.ts @@ -114,9 +114,26 @@ describe('JSHandle', function () { const { page } = getTestState(); const aHandle = await page.evaluateHandle(() => ({ foo: 'bar' })); - const json = await aHandle.jsonValue(); + const json = await aHandle.jsonValue>(); expect(json).toEqual({ foo: 'bar' }); }); + + it('works with jsonValues that are not objects', async () => { + const { page } = getTestState(); + + const aHandle = await page.evaluateHandle(() => ['a', 'b']); + const json = await aHandle.jsonValue(); + expect(json).toEqual(['a', 'b']); + }); + + it('works with jsonValues that are primitives', async () => { + const { page } = getTestState(); + + const aHandle = await page.evaluateHandle(() => 'foo'); + const json = await aHandle.jsonValue(); + expect(json).toEqual('foo'); + }); + itFailsFirefox('should not work with dates', async () => { const { page } = getTestState();