microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
65.47k stars 3.56k forks source link

[Bug]: Property `screen` does not exist on type `PlaywrightTestOptions` #31463

Closed silverwind closed 3 weeks ago

silverwind commented 2 months ago

Version

1.45.0

Steps to reproduce

  1. Clone https://github.com/silverwind/playwright-screen
  2. Run npm i && npx tsc

Expected behavior

No error

Actual behavior

Typescript error:

playwright.config.ts:10:9 - error TS2353: Object literal may only specify known properties, and 'screen' does not exist in type 'UseOptions<PlaywrightTestOptions & CustomProperties<{}>, PlaywrightWorkerOptions & CustomProperties<{}>>'.

10         screen: {width: 1920, height: 1080},
           ~~~~~~

  node_modules/playwright/types/test.d.ts:113:3
    113   use?: UseOptions<TestArgs, WorkerArgs>;
          ~~~
    The expected type comes from property 'use' which is declared here on type 'Project<PlaywrightTestOptions & CustomProperties<{}>, PlaywrightWorkerOptions & CustomProperties<{}>>'

Additional context

The object in use is a copy of what @playwright/test currently exports as devices["Desktop Chrome"]:

{
  userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6422.26 Safari/537.36',
  viewport: { width: 1280, height: 720 },
  screen: { width: 1920, height: 1080 }, // <- unknown property
  deviceScaleFactor: 1,
  isMobile: false,
  hasTouch: false,
  defaultBrowserType: 'chromium'
}

screen needs to be either removed if it's unused or be added to PlaywrightTestOptions.

Environment

  System:
    OS: macOS 14.5
    CPU: (16) arm64 Apple M3 Max
    Memory: 1.95 GB / 48.00 GB
  Binaries:
    Node: 22.3.0 - /opt/homebrew/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.8.1 - /opt/homebrew/bin/npm
    pnpm: 9.4.0 - ~/.npm-global/bin/pnpm
    bun: 1.1.16 - /opt/homebrew/bin/bun
  IDEs:
    VSCode: 1.89.1 - /opt/homebrew/bin/code
  Languages:
    Bash: 5.2.26 - /opt/homebrew/bin/bash
  npmPackages:
    @playwright/test: 1.45.0 => 1.45.0
mxschmitt commented 1 month ago

@silverwind for your scenario, we recommend removing the screen property.


Investigation notes:

Proposals:

silverwind commented 1 month ago

To explain my use case: I'm in a library that provides the playwright config and want to avoid the @playwright/test runtime dependency so I just extract the one browser object I need from the module so that the config becomes pure JSON:

cat node_modules/playwright-core/lib/server/deviceDescriptorsSource.json | jq '.["Desktop Chrome"]' > chrome.json

This JSON object is then imported and passed the projects.use option. While I could filter out the screen property in this export, I think playwright would be better off when it's own exports match its own schema.

If typescript would be more thourough in type checking the object, it would actually raise an error in the basic config because the value of devices['Desktop Chrome'] violates the type for project.use.

pavelfeldman commented 3 weeks ago

I see. This sounds like it is outside of the scope for Playwright, you should be able to filter out the property during your json processing.

silverwind commented 3 weeks ago

I see. This sounds like it is outside of the scope for Playwright, you should be able to filter out the property during your json processing.

I already have a workaround in place, but it's still a bug in Playwright that the exported object does not match the exported type.