Skip to content

Commit

Permalink
feat(console-reporter): global exception handling v1
Browse files Browse the repository at this point in the history
  • Loading branch information
In1th committed Feb 28, 2024
1 parent 85e2bf1 commit cf117f1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 47 deletions.
@@ -0,0 +1,13 @@
import { describe, it } from '@serenity-js/playwright-test';

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

describe('A scenario', () => {

throw new Error('Something happened');

it('where this shouldnt be invoked', () => {
throw new Error(`This shouldn't happen`);
});
});
});
17 changes: 16 additions & 1 deletion integration/playwright-test/spec/failing_scenarios.spec.ts
@@ -1,6 +1,6 @@
import { expect, ifExitCodeIsOtherThan, logOutput, PickEvent } from '@integration/testing-tools';
import { AssertionError, LogicError, TestCompromisedError } from '@serenity-js/core';
import { SceneFinished, SceneStarts, SceneTagged, TestRunnerDetected } from '@serenity-js/core/lib/events';
import { SceneFinished, SceneStarts, SceneTagged, TestRunFinished, TestRunnerDetected } from '@serenity-js/core/lib/events';
import { trimmed } from '@serenity-js/core/lib/io';
import { CapabilityTag, ExecutionCompromised, ExecutionFailedWithAssertionError, ExecutionFailedWithError, FeatureTag, Name, ProblemIndication } from '@serenity-js/core/lib/model';
import { describe, it } from 'mocha';
Expand Down Expand Up @@ -163,5 +163,20 @@ describe('@serenity-js/playwright-test', function () {
})
;
}));

it('fails with unhandled global exception', () => playwrightTest('--project=default', 'failing/global-error-thrown.spec.ts')
.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.equal('Error: Something happened');
})
;
}));
});
});
Expand Up @@ -5,7 +5,6 @@ import type {
DomainEvent} from '@serenity-js/core/lib/events';
import {
ActivityRelatedArtifactGenerated,
GlobalExceptionEncountered,
InteractionFinished,
InteractionStarts,
SceneFinished,
Expand Down Expand Up @@ -268,11 +267,6 @@ export class ConsoleReporter implements ListensToDomainEvents {
this.printScene(event.sceneId);
}

if (event instanceof GlobalExceptionEncountered && event.outcome instanceof ProblemIndication) {
// this.globalError.recordIfNeeded(event.outcome.error);
this.printTestRunErrorOutcome(event.outcome);
}

if (event instanceof TestRunFinished) {
this.summary.recordTestRunFinishedAt(event.timestamp);

Expand Down
28 changes: 0 additions & 28 deletions packages/core/src/events/GlobalExceptionEncountered.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/events/index.ts
Expand Up @@ -12,7 +12,6 @@ export * from './BusinessRuleDetected';
export * from './DomainEvent';
export * from './EmitsDomainEvents';
export * from './FeatureNarrativeDetected';
export * from './GlobalExceptionEncountered';
export * from './InteractionFinished';
export * from './InteractionStarts';
export * from './RetryableSceneDetected';
Expand Down
Expand Up @@ -21,7 +21,6 @@ import type { OutputStream } from '@serenity-js/core/lib/adapter';
import type { DomainEvent } from '@serenity-js/core/lib/events';
import * as events from '@serenity-js/core/lib/events';
import {
GlobalExceptionEncountered,
InteractionFinished,
RetryableSceneDetected,
SceneFinished,
Expand Down Expand Up @@ -91,6 +90,7 @@ export interface SerenityReporterForPlaywrightTestConfig {
export class SerenityReporterForPlaywrightTest implements Reporter {
private errorParser = new PlaywrightErrorParser();
private sceneIds: Map<string, CorrelationId> = new Map();
private unhandledError?: Error;

/**
* @param config
Expand Down Expand Up @@ -209,14 +209,7 @@ export class SerenityReporterForPlaywrightTest implements Reporter {
}

onError(error: TestError): void {
const parsedError = this.errorParser.errorFrom(error);

this.serenity.announce(
new GlobalExceptionEncountered(
new ExecutionFailedWithError(parsedError),
this.serenity.currentTime()
),
);
this.unhandledError = this.errorParser.errorFrom(error);
}

private determineScenarioOutcome(
Expand Down Expand Up @@ -284,9 +277,14 @@ export class SerenityReporterForPlaywrightTest implements Reporter {

try {
await this.serenity.waitForNextCue();

const outcome = this.unhandledError?
new ExecutionFailedWithError(this.unhandledError)
: new ExecutionSuccessful()

this.serenity.announce(
new TestRunFinished(
new ExecutionSuccessful(),
outcome,
this.serenity.currentTime(),
),
);
Expand Down Expand Up @@ -354,9 +352,16 @@ class PlaywrightErrorParser {
);

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

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 cf117f1

Please sign in to comment.