From e2dfdc8c62a126cfd66637ab3883768cded78154 Mon Sep 17 00:00:00 2001 From: Vitaliy Potapov Date: Thu, 24 Oct 2024 18:00:23 +0400 Subject: [PATCH] fix worker hooks and hooks-tags test --- src/run/bddFixtures/test.ts | 4 +++- test/hooks-tags/features/hooks.ts | 4 ++-- test/hooks-tags/features/sample2.feature | 4 ++++ test/hooks-tags/features/steps.ts | 6 +++++ test/hooks-tags/test.mjs | 29 ++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 test/hooks-tags/features/sample2.feature diff --git a/src/run/bddFixtures/test.ts b/src/run/bddFixtures/test.ts index 70159aa0..b08c508f 100644 --- a/src/run/bddFixtures/test.ts +++ b/src/run/bddFixtures/test.ts @@ -99,7 +99,9 @@ export const test = base.extend({ // Unused fixtures below are important for lazy initialization only on bdd projects // See: https://github.com/vitalets/playwright-bdd/issues/166 Given: [ - ({ $bddContext, $applySpecialTags }, use) => use(createStepInvoker($bddContext)), + // todo: $beforeAll, $afterAll should go away + ({ $bddContext, $applySpecialTags, $beforeAll, $afterAll }, use) => + use(createStepInvoker($bddContext)), fixtureOptions, ], // All invoke step fixtures use the same Given fixture, b/c we get keyword from step meta (by index) diff --git a/test/hooks-tags/features/hooks.ts b/test/hooks-tags/features/hooks.ts index f887b8f0..1e3048a5 100644 --- a/test/hooks-tags/features/hooks.ts +++ b/test/hooks-tags/features/hooks.ts @@ -1,16 +1,16 @@ -/* eslint-disable @typescript-eslint/no-unused-vars */ - import { expect } from '@playwright/test'; import { Before, After, AfterAll } from './fixtures'; import { track, calls } from './track'; const logger = console; +// eslint-disable-next-line @typescript-eslint/no-unused-vars Before('@bar', async function ({ $testInfo, $tags, fixtureForBar }) { track(`Before @bar ${$testInfo.title}`); expect($tags).toEqual(['@foo', '@bar']); }); +// eslint-disable-next-line @typescript-eslint/no-unused-vars Before({ tags: '@foo and not @bar' }, async ({ $testInfo, fixtureForFoo }) => { track(`Before @foo and not @bar ${$testInfo.title}`); }); diff --git a/test/hooks-tags/features/sample2.feature b/test/hooks-tags/features/sample2.feature new file mode 100644 index 00000000..c0e8ff66 --- /dev/null +++ b/test/hooks-tags/features/sample2.feature @@ -0,0 +1,4 @@ +Feature: sample + + Scenario: scenario 3 + Given step in sample2 diff --git a/test/hooks-tags/features/steps.ts b/test/hooks-tags/features/steps.ts index 65b8297b..7322aa8c 100644 --- a/test/hooks-tags/features/steps.ts +++ b/test/hooks-tags/features/steps.ts @@ -1,6 +1,12 @@ import { Given } from './fixtures'; import { track } from './track'; +const logger = console; + Given('State {int}', async ({ $testInfo }) => { track(`Step ${$testInfo.title}`); }); + +Given('step in sample2', async () => { + logger.log(`step in sample2`); +}); diff --git a/test/hooks-tags/test.mjs b/test/hooks-tags/test.mjs index 8443a28a..9cc78ad5 100644 --- a/test/hooks-tags/test.mjs +++ b/test/hooks-tags/test.mjs @@ -16,6 +16,7 @@ test(`${testDir.name} (run all tests)`, () => { expect(stdout).toContain('setup fixture for foo'); expect(stdout).toContain('setup fixture for bar'); + expect(stdout).toContain('step in sample2'); expectHookCalls(stdout, [ 'Before @bar scenario 1', @@ -51,6 +52,7 @@ test(`${testDir.name} (gen not bar)`, () => { expect(stdout).toContain('setup fixture for foo'); expect(stdout).not.toContain('setup fixture for bar'); + expect(stdout).toContain('step in sample2'); expectHookCalls(stdout, [ 'Before @foo and not @bar scenario 2', // prettier-ignore @@ -91,6 +93,7 @@ test(`${testDir.name} (run not bar)`, { skip: !pwSupportsTags }, () => { // We can't know beforehand, which will it be filtered with PW tags in runtime. // Looks like a minor issue. expect(stdout).toContain('setup fixture for bar'); + expect(stdout).toContain('step in sample2'); expectHookCalls(stdout, [ 'Before @foo and not @bar scenario 2', // prettier-ignore @@ -99,6 +102,32 @@ test(`${testDir.name} (run not bar)`, { skip: !pwSupportsTags }, () => { ]); }); +test(`${testDir.name} (gen not foo)`, () => { + const stdout = execPlaywrightTest( + testDir.name, + `${BDDGEN_CMD} --tags "not @foo" && ${PLAYWRIGHT_CMD}`, + ); + + expect(stdout).toContain('step in sample2'); + + expect(stdout).not.toContain('setup fixture for foo'); + expect(stdout).not.toContain('setup fixture for bar'); + expectHookCalls(stdout, []); +}); + +test(`${testDir.name} (run not foo)`, () => { + const stdout = execPlaywrightTest( + testDir.name, + `${BDDGEN_CMD} && ${PLAYWRIGHT_CMD} --grep-invert "@foo"`, + ); + + expect(stdout).toContain('step in sample2'); + + expect(stdout).not.toContain('setup fixture for foo'); + expect(stdout).not.toContain('setup fixture for bar'); + expectHookCalls(stdout, []); +}); + function expectHookCalls(stdout, hookCalls) { expect(stdout).toContain(['Start', ...hookCalls, 'End'].join('\n')); }