Cyro Buosi
Cyro Buosi

Reputation: 71

Configuring PlayWright and Jest returns 'Playwright Test needs to be invoked via 'npx playwright test' and excluded from Jest test runs'

I've tried configuring Jest into my Playwright project, so far I've done this

npm init && npm install -D jest jest-playwright-preset playwright

and then added a file called

jest.config.js

which contains

module.exports = {
  rootDir: '.',
  testTimeout: 20000,
  testMatch: [
    '<rootDir>/*.spec.js'
  ],
  preset: 'jest-playwright-preset'
}

and added to my package.json file

{
  "name": "projeto_pessoal",
  "version": "1.0.0",
  "description": "teste api",
  "main": "index.js",
  "dependencies": {},
  "devDependencies": {
    "@playwright/test": "^1.32.3",
    "jest": "^29.5.0",
    "jest-playwright-preset": "^3.0.1",
    "playwright": "^1.32.3"
  },
  "scripts": {
    "test": "jest"
  },
  "author": "unnamed",
  "license": "ISC"
}

Yet, running npm test returns this error message:

Playwright Test needs to be invoked via 'npx playwright test' and excluded from Jest test runs. Creating one directory for Playwright tests and one for Jest is the recommended way of doing it. See https://playwright.dev/docs/intro for more information about Playwright Test.

Upvotes: 4

Views: 5720

Answers (3)

Sun
Sun

Reputation: 173

If you have a different extension for your jest tests vs your playwright tests, excluding is what solved this for me.

For example, I have .test.js for jest and .spec.js for playwright. In jest config, I added modulePathIgnorePatterns: ["spec.js"], and it solved the issue.

Upvotes: 1

RolKau
RolKau

Reputation: 1822

I reckon that what you want to do is to run tests that are generated using playwright codegen with Jest.

However, this does not seem to be possible: The tests are generated with import { test, expect } from '@playwright/test' at the top, and this is the module that throws the error. The tests that are generated with Playwright use the API from this module, and this is slightly different than the one from Jest. (It gets fixtures as parameters).

If you use the implementation from Playwright (for instance by mocking away the throwIfRunningInsideJest function), it still won't work because then the tests won't be registered in the Jest test runner.

Upvotes: 1

jtsnr
jtsnr

Reputation: 1210

Remove the @playwright/test from the package.json devDependencies.

To write tests see expect-playwright which is pre-installed with jest-playwright.

Upvotes: 1

Related Questions