Skip to content

Commit

Permalink
test(console-reporter): snippet flag and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
In1th committed Feb 29, 2024
1 parent cf117f1 commit 3f9d729
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { describe, it } from '@serenity-js/playwright-test';

describe('Playwright Test reporting', () => {

describe('A scenario', () => {
throw new Error('Something happened');

throw new Error('Something happened');
describe('A scenario', () => {

it('where this shouldnt be invoked', () => {
throw new Error(`This shouldn't happen`);
Expand Down
1 change: 1 addition & 0 deletions integration/playwright-test/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const config: PlaywrightTestConfig = {
'@integration/testing-tools:ChildProcessReporter',
'@serenity-js/core:StreamReporter',
],
includeSnippetOnError: !!process.env.SHOW_SNIPPETS
},
],
...[
Expand Down
31 changes: 30 additions & 1 deletion integration/playwright-test/spec/failing_scenarios.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ describe('@serenity-js/playwright-test', function () {
;
}));

it('fails with unhandled global exception', () => playwrightTest('--project=default', 'failing/global-error-thrown.spec.ts')
it('fails with unhandled global exception', () => playwrightTest(
'failing/global-error-thrown.spec.ts',
'--project=default',
'--pass-with-no-tests' // without that outcome changes to no tests found
)
.then(ifExitCodeIsOtherThan(1, logOutput))
.then(result => {
expect(result.exitCode).to.equal(1);
Expand All @@ -178,5 +182,30 @@ describe('@serenity-js/playwright-test', function () {
})
;
}));

it('fails with unhandled global exception with snippet', () => playwrightTest(
'failing/global-error-thrown.spec.ts',
'--project=default',
'--pass-with-no-tests', // without that outcome changes to no tests found
'export SHOW_SNIPPETS=true'
)
.then(ifExitCodeIsOtherThan(1, logOutput))
.then(result => {
expect(result.exitCode).to.equal(1);

PickEvent.from(result.events)
.next(TestRunFinished, event => {
const outcome: ProblemIndication = event.outcome as ProblemIndication;
expect(outcome).to.be.instanceOf(ExecutionFailedWithError);
expect(outcome.error.name).to.equal('Error');
expect(outcome.error.message).to.include('Error: Something happened');
expect(outcome.error.message).to.include('at failing/global-error-thrown.spec.ts:5');
expect(outcome.error.message).to.include('describe(\'Playwright Test reporting\', () => {');
expect(outcome.error.message).to.include('> 5 |');
expect(outcome.error.message).to.include('throw new Error(\'Something happened\');');
expect(outcome.error.message).to.include('describe(\'A scenario\', () => {');
})
;
}));
});
});
11 changes: 11 additions & 0 deletions integration/playwright-test/src/playwright-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ export function playwrightTest(...params: string[]): Promise<SpawnResult> {
reporters.map(([name, outputPath]) => [ `REPORTER_${ name.toUpperCase() }`, outputPath ])
);

const envModifier = params.find(parameter => parameter.startsWith('export '));
if (envModifier){
try{
const [envKey, envValue] = envModifier.replace('export ', '').split('=');
env[envKey] = envValue;
}
catch{
throw new Error('Invalid env variable modifier. Use "export KEY=VALUE" format');
}
}

return spawner(
playwrightExecutable,
{ cwd: path.resolve(__dirname, '..'), env },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ export interface SerenityReporterForPlaywrightTestConfig {
* - {@apilink SerenityConfig.outputStream}
*/
outputStream?: OutputStream;

/**
* Flag for including code snippets on error
*
* Defaults to false.
*/
includeSnippetOnError?: boolean;
}

/**
Expand All @@ -91,6 +98,7 @@ export class SerenityReporterForPlaywrightTest implements Reporter {
private errorParser = new PlaywrightErrorParser();
private sceneIds: Map<string, CorrelationId> = new Map();
private unhandledError?: Error;
private includeSnippetOnError: boolean;

/**
* @param config
Expand All @@ -108,6 +116,7 @@ export class SerenityReporterForPlaywrightTest implements Reporter {
),
) {
this.serenity.configure(config);
this.includeSnippetOnError = config.includeSnippetOnError ?? false;
}

onBegin(config: FullConfig, suite: Suite): void {
Expand Down Expand Up @@ -209,7 +218,7 @@ export class SerenityReporterForPlaywrightTest implements Reporter {
}

onError(error: TestError): void {
this.unhandledError = this.errorParser.errorFrom(error);
this.unhandledError = this.errorParser.errorFrom(error, this.includeSnippetOnError);
}

private determineScenarioOutcome(
Expand Down Expand Up @@ -240,11 +249,11 @@ export class SerenityReporterForPlaywrightTest implements Reporter {

if (['failed', 'interrupted', 'timedOut'].includes(result.status)) {
if (test.retries > result.retry) {
return new ExecutionIgnored(this.errorParser.errorFrom(result.error));
return new ExecutionIgnored(this.errorParser.errorFrom(result.error, this.includeSnippetOnError));
}

return new ExecutionFailedWithError(
this.errorParser.errorFrom(result.error),
this.errorParser.errorFrom(result.error, this.includeSnippetOnError),
);
}

Expand Down Expand Up @@ -351,16 +360,18 @@ class PlaywrightErrorParser {
'g',
);

public errorFrom(testError: TestError): Error {
public errorFrom(testError: TestError, includeSnippet: boolean): Error {
let message =
testError.message &&
PlaywrightErrorParser.stripAsciiFrom(testError.message);

const snippet =
testError.snippet &&
PlaywrightErrorParser.stripAsciiFrom(testError.snippet);

message = snippet ? [message, snippet].join('\n') : message;
if (includeSnippet){
const snippet =
testError.snippet &&
PlaywrightErrorParser.stripAsciiFrom(testError.snippet);

message = snippet ? [message, snippet].join('\n') : message;
}

let stack =
testError.stack && PlaywrightErrorParser.stripAsciiFrom(testError.stack);
Expand Down

0 comments on commit 3f9d729

Please sign in to comment.