Skip to content

Commit

Permalink
fix: scenario hooks dont store worldFixture
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Nov 29, 2024
1 parent 82eaadb commit a3c76c8
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/generate/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,12 @@ export class TestFile {
});
});

this.hooks.getWorldFixtureNames().forEach((name) => worldFixtureNames.add(name));

if (worldFixtureNames.size > 1) {
throw new Error(
[
`All steps in a feature file should have the same worldFixture.`,
`All steps and hooks in a feature file should have the same worldFixture.`,
`Found fixtures: ${[...worldFixtureNames].join(', ')}`,
`File: ${this.featureUri}`,
].join('\n'),
Expand Down
11 changes: 11 additions & 0 deletions src/generate/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export class TestFileHooks {
]);
}

getWorldFixtureNames() {
return new Set([
...this.before.getWorldFixtureNames(), // prettier-ignore
...this.after.getWorldFixtureNames(),
]);
}

render() {
const lines = [
...this.beforeAll.render(), // prettier-ignore
Expand Down Expand Up @@ -96,6 +103,10 @@ class TestFileScenarioHooks<T extends ScenarioHookType> {
getFixtureNames() {
return getScenarioHooksFixtureNames([...this.hooks]);
}

getWorldFixtureNames() {
return new Set([...this.hooks].map((hook) => hook.worldFixture).filter(toBoolean));
}
}

class TestFileWorkerHooks<T extends WorkerHookType> {
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ScenarioHook<Fixtures, World> = {
location: PlaywrightLocation;
customTest?: TestTypeCommon;
defaultTags?: string;
worldFixture?: string;
};

/**
Expand Down Expand Up @@ -64,7 +65,7 @@ export function scenarioHookFactory<
TestFixtures extends KeyValue,
WorkerFixtures extends KeyValue,
World,
>(type: ScenarioHookType, { customTest, defaultTags }: HookConstructorOptions) {
>(type: ScenarioHookType, { customTest, defaultTags, worldFixture }: HookConstructorOptions) {
type AllFixtures = TestFixtures & WorkerFixtures;
type Args = ScenarioHookDefinitionArgs<AllFixtures, World>;

Expand All @@ -77,6 +78,7 @@ export function scenarioHookFactory<
location: getLocationByOffset(3),
customTest,
defaultTags,
worldFixture,
});
};
}
Expand Down
1 change: 1 addition & 0 deletions test/world-ambiguos/only-steps/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { test as base } from 'playwright-bdd';

export const test = base.extend<{ world1: string; world2: string }>({
world1: async ({}, use) => use('world1'),
world2: async ({}, use) => use('world2'),
});
2 changes: 1 addition & 1 deletion test/world-ambiguos/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig } from '@playwright/test';
import { defineBddConfig } from 'playwright-bdd';

const testDir = defineBddConfig({
featuresRoot: 'only-steps',
featuresRoot: process.env.FEATURES_ROOT!,
});

export default defineConfig({
Expand Down
6 changes: 6 additions & 0 deletions test/world-ambiguos/scenario-hooks/fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { test as base } from 'playwright-bdd';

export const test = base.extend<{ world1: string; world2: string }>({
world1: async ({}, use) => use('world1'),
world2: async ({}, use) => use('world2'),
});
6 changes: 6 additions & 0 deletions test/world-ambiguos/scenario-hooks/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createBdd } from 'playwright-bdd';
import { test } from './fixtures';

const { Before } = createBdd(test, { worldFixture: 'world2' });

Before(async function () {});
4 changes: 4 additions & 0 deletions test/world-ambiguos/scenario-hooks/sample.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Feature: feature

Scenario: scenario
Given step with world1
6 changes: 6 additions & 0 deletions test/world-ambiguos/scenario-hooks/steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createBdd } from 'playwright-bdd';
import { test } from './fixtures';

const { Given } = createBdd(test, { worldFixture: 'world1' });

Given('step with world1', async function () {});
25 changes: 20 additions & 5 deletions test/world-ambiguos/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ import { test, TestDir, execPlaywrightTestWithError } from '../_helpers/index.mj

const testDir = new TestDir(import.meta);

test(testDir.name, () => {
execPlaywrightTestWithError(testDir.name, [
'All steps in a feature file should have the same worldFixture',
'Found fixtures: world1, world2',
]);
test(`${testDir.name} (only-steps)`, () => {
execPlaywrightTestWithError(
testDir.name,
[
'All steps and hooks in a feature file should have the same worldFixture',
'Found fixtures: world1, world2',
],
{ env: { FEATURES_ROOT: 'only-steps' } },
);
});

test(`${testDir.name} (scenario-hooks)`, () => {
execPlaywrightTestWithError(
testDir.name,
[
'All steps and hooks in a feature file should have the same worldFixture',
'Found fixtures: world1, world2',
],
{ env: { FEATURES_ROOT: 'scenario-hooks' } },
);
});

0 comments on commit a3c76c8

Please sign in to comment.