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:
- Ensure
"nodeModulesDir": "auto"
exists in thedeno.json
file of your project. - Add Playwright to the project by running
deno add npm:playwright
. - Install Playwright
deno run -A npm:playwright install
, downgrade to2.1.4
if you are gettingBadResource
errors with the latest version of Deno. - 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 thetestMatch
regex in aplaywright.config.ts
that doesn't contain thetestMatch
option. - Run tests with
deno run -A npm:playwright test [test-filter]
, ordeno run -A npm:playwright test
if you are using aplaywright.config.ts
file.