Skip to content

Commit

Permalink
fix: Cucumber reporter doesn't work for non BDD projects (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed May 1, 2024
1 parent c44d8a9 commit 69e88a8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## dev
* feature: support _ in @timeout tag
* fix: support Playwright 1.44 and Cucumber 10.6
* fix: Cucumber reporter doesn't work for non BDD projects ([#143](https://github.com/vitalets/playwright-bdd/issues/143))

## 6.3.0
* improvement: set scenario timeout via `test.setTimeout` instead of anonymous describe ([#139](https://github.com/vitalets/playwright-bdd/issues/139))
Expand Down
38 changes: 32 additions & 6 deletions src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@
* For passing configs to playwright workers and bddgen.
*/

import path from 'node:path';
/*
Example of PLAYWRIGHT_BDD_CONFIGS:
{
'/Users/foo/bar/.features-gen/one': {
outputDir: '/Users/foo/bar/.features-gen/one',
paths: [ 'features-one/*.feature' ],
...
},
'/Users/foo/bar/.features-gen/two': {
outputDir: '/Users/foo/bar/.features-gen/two',
paths: [ 'features-two/*.feature' ],
...
},
}
*/

import { BDDConfig } from '.';
import { exit } from '../utils/exit';

type OutputDir = string;
type EnvConfigs = Record<OutputDir, BDDConfig>;

let envConfigsCache: EnvConfigs;

export function saveConfigToEnv(config: BDDConfig) {
const envConfigs = getEnvConfigs();
const existingConfig = envConfigs[config.outputDir];
Expand All @@ -29,13 +46,12 @@ export function saveConfigToEnv(config: BDDConfig) {
saveEnvConfigs(envConfigs);
}

export function getConfigFromEnv(outputDir: string) {
export function getConfigFromEnv(testDir: string) {
const envConfigs = getEnvConfigs();
outputDir = path.resolve(outputDir);
const config = envConfigs[outputDir];
const config = envConfigs[testDir];
if (!config) {
exit(
`Config not found for outputDir: "${outputDir}".`,
`Config not found for testDir: "${testDir}".`,
`Available dirs: ${Object.keys(envConfigs).join('\n')}`,
);
}
Expand All @@ -44,10 +60,20 @@ export function getConfigFromEnv(outputDir: string) {
}

export function getEnvConfigs() {
return JSON.parse(process.env.PLAYWRIGHT_BDD_CONFIGS || '{}') as EnvConfigs;
if (!envConfigsCache) {
envConfigsCache = JSON.parse(process.env.PLAYWRIGHT_BDD_CONFIGS || '{}');
}
return envConfigsCache;
}

export function hasBddConfig(testDir?: string) {
if (!testDir) return false;
const envConfigs = getEnvConfigs();
return Boolean(envConfigs[testDir]);
}

function saveEnvConfigs(envConfigs: EnvConfigs) {
envConfigsCache = envConfigs;
process.env.PLAYWRIGHT_BDD_CONFIGS = JSON.stringify(envConfigs);
}

Expand Down
4 changes: 4 additions & 0 deletions src/reporter/cucumber/messagesBuilder/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AutofillMap } from '../../../utils/AutofillMap';
import { GherkinDocuments } from './GherkinDocuments';
import { Pickles } from './Pickles';
import { ConcreteEnvelope } from './types';
import { hasBddConfig } from '../../../config/env';

export class MessagesBuilder {
private report = {
Expand Down Expand Up @@ -48,6 +49,9 @@ export class MessagesBuilder {
}

onTestEnd(test: pw.TestCase, result: pw.TestResult) {
// Skip tests of non-bdd projects
if (!hasBddConfig(test.parent.project()?.testDir)) return;

// For skipped tests Playwright doesn't run fixtures
// and we don't have bddData attachment -> don't know feature uri.
// Don't add such test run to report.
Expand Down
6 changes: 6 additions & 0 deletions test/reporter-cucumber-projects/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export default defineConfig({
}),
],
projects: [
{
// see: https://github.com/vitalets/playwright-bdd/issues/143
name: 'non-bdd-project',
testMatch: /setup\.ts/,
testDir: 'setup',
},
{
name: 'project one',
testDir: defineBddConfig({
Expand Down
3 changes: 3 additions & 0 deletions test/reporter-cucumber-projects/setup/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test as setup } from '@playwright/test';

setup('some setup', async ({}) => {});

0 comments on commit 69e88a8

Please sign in to comment.