Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
fix(core): DebugReporter prints domain events serialised as ndjson
DebugReporter used to serialise the DomainEvents it received using their toString() method. This was OK for quick manual verification, but ndjson makes it easier to parse and analyse debug output submitted by other developers using Serenity/JS.
- Loading branch information
Showing
with
59 additions
and 4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -1,16 +1,62 @@ | ||
import WriteStream = NodeJS.WriteStream; | ||
import { DomainEvent } from '../../../events'; | ||
import { Stage } from '../../Stage'; | ||
import { StageCrewMember } from '../../StageCrewMember'; | ||
|
||
/** | ||
* @desc | ||
* Serialises all the {@link DomainEvent} objects it receives and outputs | ||
* them as [ndjson](http://ndjson.org/) | ||
* so that they can be analysed by the Serenity/JS team. | ||
* | ||
* @example <caption>Writing the DomainEvents to a file</caption> | ||
* import { serenity } form '@serenity-js/core'; | ||
* import fs = require('fs'); | ||
* | ||
* serenity.setTheStage( | ||
* new DebugReporter(fs.createWriteStream('./debug-output.ndjson')), | ||
* ); | ||
* | ||
* @extends {StageCrewMember} | ||
*/ | ||
export class DebugReporter implements StageCrewMember { | ||
|
||
/** | ||
* @param {WriteStream} output - A WriteStream that should receive the output | ||
* @param {Stage} [stage=null] - The stage this {@link StageCrewMember} should be assigned to | ||
*/ | ||
constructor( | ||
private readonly output: WriteStream = process.stdout, | ||
private readonly stage: Stage = null, | ||
) { | ||
} | ||
|
||
/** | ||
* @desc | ||
* Creates a new instance of this {@link StageCrewMember} and assigns it to a given {@link Stage}. | ||
* | ||
* @see {@link StageCrewMember} | ||
* | ||
* @param {Stage} stage - An instance of a {@link Stage} this {@link StageCrewMember} will be assigned to | ||
* @returns {StageCrewMember} - A new instance of this {@link StageCrewMember} | ||
*/ | ||
assignedTo(stage: Stage): StageCrewMember { | ||
return new DebugReporter(this.output, stage); | ||
} | ||
|
||
/** | ||
* @desc | ||
* Handles {@link DomainEvent} objects emitted by the {@link Stage} | ||
* this {@link StageCrewMember} is assigned to. | ||
* | ||
* @see {@link StageCrewMember} | ||
* | ||
* @param {DomainEvent} event | ||
* @returns void | ||
*/ | ||
notifyOf(event: DomainEvent): void { | ||
this.output.write( | ||
JSON.stringify({ type: event.constructor.name, event: event.toJSON() }) + '\n', | ||
); | ||
} | ||
} |