playwright-watch icon indicating copy to clipboard operation
playwright-watch copied to clipboard

`yarn test:watch` at the very first run executes test twice

Open github0013 opened this issue 4 years ago • 9 comments

Dockerfile

FROM mcr.microsoft.com/playwright:focal
ENV LANG C.UTF-8

WORKDIR /app
RUN yarn install

User pwuser
RUN npx playwright install

package.json

{
  "scripts": {
    "test": "playwright test",
    "test:watch": "playwright-watch exec yarn test"
  },
  "devDependencies": {
    "@playwright/test": "^1.16.2",
    "@types/node": "^16.11.6",
    "playwright-watch": "^1.3.21",
  }
}

foo.spec.ts

import { expect, test } from "@playwright/test"

test.describe("yarn test:watch", () => {
  test.beforeAll(async () => {
    console.log("beforeAll:")
  })
  test.afterAll(async () => {
    console.log("afterAll:")
  })
  test("test1", async ({ page }) => {
    expect(true).toBe(true)
  })
})

making sure yarn test only runs once for foo.spec.ts

pwuser@309e51291d88:/app$ yarn test
yarn run v1.22.10
warning package.json: No license field
$ playwright test
Using config at /app/playwright.config.ts

Running 1 test using 1 worker

beforeAll:
  ✓  tests/foo.spec.ts:23:3 › yarn test:watch › test1 (288ms)
afterAll:


  1 passed (621ms)
Done in 1.00s.

yarn test:watch runs the test twice on the initial run

Notice

  • Using config at /app/playwright.config.ts
  • beforeAll
  • afterAll

They are output twice.

💾 HERE, I saved foo.spec.ts to run again

After that, it only runs once on a save.

pwuser@309e51291d88:/app$ yarn test:watch
yarn run v1.22.10
warning package.json: No license field
$ playwright-watch exec yarn test
warning package.json: No license field
warning package.json: No license field
$ playwright test
$ playwright test
Using config at /app/playwright.config.ts
Using config at /app/playwright.config.ts

Running 1 test using 1 worker

[Error: ENOENT: no such file or directory, scandir '/app/test-results'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'scandir',
  path: '/app/test-results'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
beforeAll:
  ✓  tests/foo.spec.ts:23:3 › yarn test:watch › test1 (309ms)
afterAll:


  1 passed (644ms)
warning package.json: No license field // <= 💾  HERE, I saved foo.spec.ts to run again
$ playwright test
Using config at /app/playwright.config.ts

Running 1 test using 1 worker

beforeAll:
  ✓  tests/foo.spec.ts:23:3 › yarn test:watch › test1 (308ms)
afterAll:


  1 passed (637ms)

github0013 avatar Oct 29 '21 03:10 github0013

Thank you for your reporting. Please try simply npx playwright-watch exec echo changed, and see if it executes twice?

iamyoki avatar Oct 30 '21 14:10 iamyoki

pwuser@07be25028166:/app$ npx playwright-watch exec echo changed
changed
changed

github0013 avatar Oct 31 '21 22:10 github0013

Think it's probably when you save it triggered by .spec.ts source file and dist file which compiles by tsc at the same time. Write a playwright.config.js file to match only the source one or dist.

playwright.config.js

testMatch: ".*spec\.ts"

iamyoki avatar Nov 01 '21 03:11 iamyoki

testMatch: ".*spec.ts"

(vscode corrects me .*spec\.ts to .*spec.ts)

With this config, I see

=================
 no tests found.
=================

Changing that to this ran tests.

testMatch: "*spec.ts"

However, it wasn't really the case as I did more tests. It seems when there are multiple spec.ts files, the initial run gets multiplied by the number of the files. (whether or not you have that testMatch part) e.g. if you had 2 spec.ts files, you get each test file run 2 times

Does it happen on your environment?

github0013 avatar Nov 01 '21 23:11 github0013

Okay, I'll look into it, thank you very much.

iamyoki avatar Nov 02 '21 04:11 iamyoki

same issue here, tests are launched twice in watch mode.

$ yarn test:watch reserv
yarn run v1.22.17
$ playwright-watch exec yarn test reserv
$ playwright test reserv
$ playwright test reserv
Using config at /Users/.../apps/demo-e2e/playwright.config.ts
Using config at /Users/.../apps/demo-e2e/playwright.config.ts

Running 1 test using 1 worker


Running 1 test using 1 worker

I can also confirm the issue only happens when there is multiple test files. (In my case 2 test files)

deeeed avatar Feb 06 '22 06:02 deeeed

I happened to me, the first time it launch as many as test file I have in Typescript

asciicast

Reproduction repo: https://github.com/leosuncin/redux-offline-example

leosuncin avatar Mar 22 '22 22:03 leosuncin

I'm seeing similar issue in a project right now, producing 4 duplicate entries:

npx playwright-watch exec echo changed
changed
changed
changed
changed

HalfdanJ avatar Apr 02 '23 02:04 HalfdanJ

Following this PR. I see similar behaviour:

$ npx playwright-watch exec echo changed
changed
changed
changed
changed
changed

MrMurga avatar Apr 12 '23 04:04 MrMurga