From 2d37430838a76019ab5bff656e73dc533bf6bead Mon Sep 17 00:00:00 2001 From: Jan Scheffler Date: Thu, 7 Oct 2021 18:04:08 +0200 Subject: [PATCH] chore: update types for JSHandle (#7650) Closes #7583 --- src/common/JSHandle.ts | 5 +++-- test/jshandle.spec.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/common/JSHandle.ts b/src/common/JSHandle.ts index 15fcc6d0..cc4f2452 100644 --- a/src/common/JSHandle.ts +++ b/src/common/JSHandle.ts @@ -191,7 +191,7 @@ export class JSHandle { /** Fetches a single property from the referenced object. */ - async getProperty(propertyName: string): Promise { + async getProperty(propertyName: string): Promise { const objectHandle = await this.evaluateHandle( (object: Element, propertyName: string) => { const result = { __proto__: null }; @@ -201,7 +201,8 @@ export class JSHandle { propertyName ); const properties = await objectHandle.getProperties(); - const result = properties.get(propertyName) || null; + const result = properties.get(propertyName); + assert(result instanceof JSHandle); await objectHandle.dispose(); return result; } diff --git a/test/jshandle.spec.ts b/test/jshandle.spec.ts index ccd9e709..a3f201ae 100644 --- a/test/jshandle.spec.ts +++ b/test/jshandle.spec.ts @@ -15,6 +15,7 @@ */ import expect from 'expect'; +import { JSHandle } from '../lib/cjs/puppeteer/common/JSHandle.js'; import { getTestState, setupTestBrowserHooks, @@ -96,6 +97,19 @@ describe('JSHandle', function () { const twoHandle = await aHandle.getProperty('two'); expect(await twoHandle.jsonValue()).toEqual(2); }); + + it('should return a JSHandle even if the property does not exist', async () => { + const { page } = getTestState(); + + const aHandle = await page.evaluateHandle(() => ({ + one: 1, + two: 2, + three: 3, + })); + const undefinedHandle = await aHandle.getProperty('doesnotexist'); + expect(undefinedHandle).toBeInstanceOf(JSHandle); + expect(await undefinedHandle.jsonValue()).toBe(undefined); + }); }); describe('JSHandle.jsonValue', function () {