Skip to content

Commit

Permalink
bugfix: enable run of non-bdd projects, #166
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Jun 29, 2024
1 parent cb5858a commit 0ee0b51
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## dev
* bugfix:

## 6.5.2
* bugfix: createBdd returns Cucumber-Style Typing when using Playwright-Style, ([#163](https://github.com/vitalets/playwright-bdd/issues/163))

Expand Down
2 changes: 1 addition & 1 deletion src/gen/testMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Tests are identified by special key constructed from title path.
*
* Example:
* const testMeta = {
* const testMetaMap = {
* "Simple scenario": { pickleLocation: "3:10", tags: ["@foo"] },
* "Scenario with examples|Example #1": { pickleLocation: "8:26", tags: [] },
* "Rule 1|Scenario with examples|Example #1": { pickleLocation: "9:42", tags: [] },
Expand Down
66 changes: 37 additions & 29 deletions src/run/bddFixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { appendNewCucumberStyleSteps } from '../../steps/cucumberStyle';
// BDD fixtures prefixed with '$' to avoid collision with user's fixtures.

export const test = base.extend<BddFixtures, BddFixturesWorker>({
// load cucumber once per worker (auto-fixture)
// load cucumber once per worker
// todo: maybe remove caching in cucumber/loadConfig.ts and cucumber/loadSteps.ts
// as we call it once per worker. Check generation phase.
$cucumber: [
Expand All @@ -45,19 +45,17 @@ export const test = base.extend<BddFixtures, BddFixturesWorker>({

await use({ runConfiguration, supportCodeLibrary, World, config });
},
{ auto: true, scope: 'worker' },
{ scope: 'worker' },
],
// apply timeout and slow from special tags in runtime instead of generating in test body
// to have cleaner test body and track fixtures in timeout calculation.
$applySpecialTags: [
async ({ $testMeta }, use, testInfo) => {
const specialTags = new SpecialTags($testMeta.ownTags, $testMeta.tags);
if (specialTags.timeout !== undefined) testInfo.setTimeout(specialTags.timeout);
if (specialTags.slow !== undefined) testInfo.slow();
await use();
},
{ auto: true },
],
$applySpecialTags: async ({ $testMeta }, use, testInfo) => {
const specialTags = new SpecialTags($testMeta.ownTags, $testMeta.tags);
if (specialTags.timeout !== undefined) testInfo.setTimeout(specialTags.timeout);
if (specialTags.slow !== undefined) testInfo.slow();
await use();
},

// $lang fixture can be overwritten in test file
$lang: ({}, use) => use(''),
// init $bddWorldFixtures with empty object, will be owerwritten in test file for cucumber-style
Expand Down Expand Up @@ -99,11 +97,23 @@ export const test = base.extend<BddFixtures, BddFixturesWorker>({
await world.destroy();
},

Given: ({ $bddWorld }, use) => use(new StepInvoker($bddWorld, 'Given').invoke),
When: ({ $bddWorld }, use) => use(new StepInvoker($bddWorld, 'When').invoke),
Then: ({ $bddWorld }, use) => use(new StepInvoker($bddWorld, 'Then').invoke),
And: ({ $bddWorld }, use) => use(new StepInvoker($bddWorld, 'And').invoke),
But: ({ $bddWorld }, use) => use(new StepInvoker($bddWorld, 'But').invoke),
// Unused fixtures below are important for lazy initialization only on bdd projects
// See: https://github.com/vitalets/playwright-bdd/issues/166
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Given: ({ $bddWorld, $before, $applySpecialTags }, use) =>
use(new StepInvoker($bddWorld, 'Given').invoke),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
When: ({ $bddWorld, $before, $applySpecialTags }, use) =>
use(new StepInvoker($bddWorld, 'When').invoke),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Then: ({ $bddWorld, $before, $applySpecialTags }, use) =>
use(new StepInvoker($bddWorld, 'Then').invoke),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
And: ({ $bddWorld, $before, $applySpecialTags }, use) =>
use(new StepInvoker($bddWorld, 'And').invoke),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
But: ({ $bddWorld, $before, $applySpecialTags }, use) =>
use(new StepInvoker($bddWorld, 'But').invoke),

// new Cucumber style world, can be overwritten in test files
$newCucumberStyleWorld: ({}, use: (arg: unknown) => unknown) => use(null),
Expand All @@ -127,7 +137,7 @@ export const test = base.extend<BddFixtures, BddFixturesWorker>({

// can be owerwritten in test file if there are scenario hooks
$scenarioHookFixtures: ({}, use) => use({}),
$before: [
$before:
// Unused dependencies are important:
// 1. $beforeAll / $afterAll: in pw < 1.39 worker-scoped auto-fixtures were called after test-scoped
// 2. $after: to call after hooks in case of errors in before hooks
Expand All @@ -140,15 +150,10 @@ export const test = base.extend<BddFixtures, BddFixturesWorker>({
await runScenarioHooks('before', { $bddWorld, $tags, $testInfo, ...$scenarioHookFixtures });
await use();
},
{ auto: true },
],
$after: [
async ({ $scenarioHookFixtures, $bddWorld, $tags }, use, $testInfo) => {
await use();
await runScenarioHooks('after', { $bddWorld, $tags, $testInfo, ...$scenarioHookFixtures });
},
{ auto: true },
],
$after: async ({ $scenarioHookFixtures, $bddWorld, $tags }, use, $testInfo) => {
await use();
await runScenarioHooks('after', { $bddWorld, $tags, $testInfo, ...$scenarioHookFixtures });
},

// can be owerwritten in test file if there are worker hooks
$workerHookFixtures: [({}, use) => use({}), { scope: 'worker' }],
Expand All @@ -161,13 +166,16 @@ export const test = base.extend<BddFixtures, BddFixturesWorker>({
await runWorkerHooks('beforeAll', { $workerInfo, ...$workerHookFixtures });
await use();
},
{ auto: true, scope: 'worker' },
{ scope: 'worker' },
],
$afterAll: [
async ({ $workerHookFixtures }, use, $workerInfo) => {
// Important unused dependencies:
// 1. $cucumber: to load hooks before this fixtures
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async ({ $workerHookFixtures, $cucumber }, use, $workerInfo) => {
await use();
await runWorkerHooks('afterAll', { $workerInfo, ...$workerHookFixtures });
},
{ auto: true, scope: 'worker' },
{ scope: 'worker' },
],
});
3 changes: 3 additions & 0 deletions test/projects/non-bdd/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from '../one/steps/fixtures';

test('non-bdd test', async () => {});
4 changes: 4 additions & 0 deletions test/projects/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ export default defineConfig({
}),
dependencies: ['project-one'],
},
{
name: 'non-bdd-project',
testDir: 'non-bdd',
},
],
});
5 changes: 3 additions & 2 deletions test/projects/test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { test, TestDir, execPlaywrightTest } from '../_helpers/index.mjs';
import { test, expect, TestDir, execPlaywrightTest } from '../_helpers/index.mjs';

const testDir = new TestDir(import.meta);

test(testDir.name, () => {
execPlaywrightTest(testDir.name);
const stdout = execPlaywrightTest(testDir.name);
expect(stdout).toContain('non-bdd test');
});

0 comments on commit 0ee0b51

Please sign in to comment.