Deno 2 and Playwright

I tried adding Playwright to an existing Deno/Fresh project recently and bumped into a few issues.

To get Playwright running I had to circumvent a downloading issue that was introduced in some recent versions (2.1.x) by downgrading to 2.1.4. As noted in the linked GitHub issue, the BadResource error has regressed in 2.1.5 and still exists in 2.1.7 (latest version at the time of writing). Downgrading temporarily won't help because tests would hang, presumably because there are version checks on the affected browser-related downloads. [Update, 2025-02-03] This issue has been fixed again in 2.1.8.

The project that I added Playwright to already contained tests that I run with the Deno test runner. Simply running tests with deno run -A npm:playwright test would include existing .test.ts tests, which lead to module not found errors like Error: Cannot find module '@std/expect/expect' for Deno dependencies. To get those running I have my Playwright tests take the .spec.ts extension instead of .test.ts, and simply select only .spec.ts files for the Playwright test runner with deno run -A npm:playwright test '\.spec\.ts'.

It is worth noting that you could also use the testMatch regex in playwright.config.ts to select files, but make sure that you exclude the webServer option from the config because, as far as I can tell, that's Node.js specific — otherwise tests won't run because of a package.json not found error.

In summary, to get things running:

  1. Ensure "nodeModulesDir": "auto" exists in the deno.json file of your project.
  2. Add Playwright to the project by running deno add npm:playwright.
  3. Install Playwright deno run -A npm:playwright install, downgrade to 2.1.4 if you are getting BadResource errors with the latest version of Deno.
  4. If you have existing tests that you want to isolate from the Playwright test runner, either use the [test-filter] regex arguments in the CLI, or use the testMatch regex in a playwright.config.ts that doesn't contain the testMatch option.
  5. Run tests with deno run -A npm:playwright test [test-filter], or deno run -A npm:playwright test if you are using a playwright.config.ts file.