Best JavaScript code snippet using stryker-parent
stryker-formatter.ts
Source:stryker-formatter.ts
1/* eslint-disable @typescript-eslint/no-non-null-assertion */2import type { IFormatterOptions } from '@cucumber/cucumber';3import {4 Envelope,5 GherkinDocument,6 TestCase,7 Pickle,8 TestCaseStarted,9 TestCaseFinished,10 Timestamp,11 Scenario,12 TestStepFinished,13 TestStepResultStatus,14 Location,15 TableRow,16} from '@cucumber/messages';17import {18 CoverageAnalysis,19 InstrumenterContext,20 Position,21} from '@stryker-mutator/api/core';22import { TestResult, TestStatus } from '@stryker-mutator/api/test-runner';23import { Formatter } from './cucumber-wrapper';24interface DescribedScenario extends Scenario {25 fileName: string;26 fullName: string;27}28// eslint-disable-next-line import/no-default-export29export default class StrykerFormatter extends Formatter {30 constructor(options: IFormatterOptions) {31 super(options);32 StrykerFormatter.instance = this;33 options.eventBroadcaster.on('envelope', this.handleEnvelope.bind(this));34 }35 private readonly documents: GherkinDocument[] = [];36 private readonly scenarios: DescribedScenario[] = [];37 private readonly pickles: Pickle[] = [];38 private readonly testCases: TestCase[] = [];39 private readonly testCasesStarted: TestCaseStarted[] = [];40 private readonly testStepsFinished: TestStepFinished[] = [];41 public reportedTestResults: TestResult[] = [];42 private handleEnvelope(envelope: Envelope) {43 if (envelope.testCase) {44 this.testCases.push(envelope.testCase);45 } else if (envelope.gherkinDocument) {46 this.documents.push(envelope.gherkinDocument);47 this.scenarios.push(...collectScenarios(envelope.gherkinDocument));48 } else if (envelope.testCaseStarted) {49 this.testCasesStarted.push(envelope.testCaseStarted);50 const { example, scenario } = this.findDetails(51 envelope.testCaseStarted.id52 );53 if (StrykerFormatter.coverageAnalysis === 'perTest') {54 StrykerFormatter.instrumenterContext.currentTestId = determineTestId(55 scenario,56 example57 );58 }59 } else if (envelope.testCaseFinished) {60 this.reportTestCase(envelope.testCaseFinished);61 StrykerFormatter.instrumenterContext.currentTestId = undefined;62 } else if (envelope.pickle) {63 this.pickles.push(envelope.pickle);64 } else if (envelope.testStepFinished) {65 this.testStepsFinished.push(envelope.testStepFinished);66 }67 }68 public static instrumenterContext: InstrumenterContext;69 public static instance: StrykerFormatter | undefined;70 public static coverageAnalysis: CoverageAnalysis;71 private reportTestCase(testCaseFinished: TestCaseFinished) {72 const { scenario, example, currentTestCaseStarted, testSteps } =73 this.findDetails(testCaseFinished.testCaseStartedId);74 const testAttributes = {75 id: determineTestId(scenario, example),76 name: determineName(scenario, example),77 timeSpentMs: timeDiffMs(78 currentTestCaseStarted.timestamp,79 testCaseFinished.timestamp80 ),81 fileName: scenario.fileName,82 startPosition: determinePosition(scenario, example),83 };84 const status = determineTestStatus(testSteps);85 if (status === TestStatus.Failed) {86 this.reportedTestResults.push({87 status,88 failureMessage: determineFailureMessage(testSteps),89 ...testAttributes,90 });91 } else {92 this.reportedTestResults.push({93 status,94 ...testAttributes,95 });96 }97 }98 private findDetails(testCaseStartedId: string) {99 const currentTestCaseStarted = this.testCasesStarted.find(100 (testCase) => testCase.id === testCaseStartedId101 )!;102 const currentTestCase = this.testCases.find(103 (testCase) => testCase.id === currentTestCaseStarted.testCaseId104 )!;105 const currentPickle = this.pickles.find(106 (pickle) => pickle.id === currentTestCase.pickleId107 )!;108 const currentScenario = this.scenarios.find((scenario) =>109 currentPickle.astNodeIds.includes(scenario.id)110 )!;111 const testSteps = this.testStepsFinished.filter(112 (testStep) => testStep.testCaseStartedId === currentTestCaseStarted.id113 );114 const currentExample = currentScenario.examples115 .flatMap((example) => example.tableBody)116 .find((example) => currentPickle.astNodeIds.includes(example.id));117 return {118 scenario: currentScenario,119 example: currentExample,120 currentTestCaseStarted,121 testSteps,122 };123 }124}125const failureStatusList = Object.freeze([126 TestStepResultStatus.FAILED,127 TestStepResultStatus.AMBIGUOUS,128]);129function determineTestId(130 scenario: DescribedScenario,131 example: TableRow | undefined132) {133 return `${scenario.fileName}:${134 (example?.location ?? scenario.location).line135 }`;136}137function determineTestStatus(testSteps: TestStepFinished[]): TestStatus {138 if (139 !testSteps.some(140 (testStep) =>141 testStep.testStepResult.status !== TestStepResultStatus.PASSED142 )143 ) {144 return TestStatus.Success;145 }146 if (147 testSteps.some((testStep) =>148 failureStatusList.includes(testStep.testStepResult.status)149 )150 ) {151 return TestStatus.Failed;152 }153 return TestStatus.Skipped;154}155function determineFailureMessage(testSteps: TestStepFinished[]): string {156 const failureStep = testSteps.find(157 (testStep) =>158 failureStatusList.includes(testStep.testStepResult.status) &&159 testStep.testStepResult.message160 );161 return failureStep?.testStepResult.message ?? 'Failed';162}163function timeDiffMs(start: Timestamp, end: Timestamp) {164 return (165 (end.seconds - start.seconds) * 1000 + (end.nanos - start.nanos) / 1_000_000166 );167}168function* collectScenarios(169 gherkinDocument: GherkinDocument170): Iterable<DescribedScenario> {171 if (gherkinDocument.feature?.children) {172 for (const child of gherkinDocument.feature?.children) {173 if (child.scenario) {174 yield {175 fileName: gherkinDocument.uri!,176 fullName: `Feature: ${gherkinDocument.feature.name} -- ${child.scenario.keyword}: ${child.scenario.name}`,177 ...child.scenario,178 };179 }180 if (child.rule) {181 for (const ruleChild of child.rule.children) {182 if (ruleChild.scenario) {183 yield {184 fileName: gherkinDocument.uri!,185 fullName: `Feature: ${gherkinDocument.feature.name} -- Rule: ${child.rule.name} -- ${ruleChild.scenario.keyword}: ${ruleChild.scenario.name}`,186 ...ruleChild.scenario,187 };188 }189 }190 }191 }192 }193}194function determinePosition(195 scenario: DescribedScenario,196 example: TableRow | undefined197): Position {198 return toPosition(example?.location ?? scenario.location);199}200function toPosition(location: Location): Position {201 return {202 line: location.line - 1, // StrykerJS works 0- based internally203 column: location.column ?? 0,204 };205}206function determineName(207 scenario: DescribedScenario,208 example: TableRow | undefined209) {210 if (example) {211 return `${scenario.fullName} [Example L${example.location.line}]`;212 }213 return scenario.fullName;...
Using AI Code Generation
1const reporter = require('stryker-parent-reporter');2reporter.currentTestCaseStarted('test case name');3const reporter = require('stryker-parent-reporter');4reporter.currentTestCaseStarted('test case name 2');5const reporter = require('stryker-parent-reporter');6reporter.currentTestCaseStarted('test case name 3');7const reporter = require('stryker-parent-reporter');8reporter.currentTestCaseStarted('test case name 4');9const reporter = require('stryker-parent-reporter');10reporter.currentTestCaseStarted('test case name 5');11const reporter = require('stryker-parent-reporter');12reporter.currentTestCaseStarted('test case name 6');13const reporter = require('stryker-parent-reporter');14reporter.currentTestCaseStarted('test case name 7');15const reporter = require('stryker-parent-reporter');16reporter.currentTestCaseStarted('test case name 8');17const reporter = require('stryker-parent-reporter');18reporter.currentTestCaseStarted('test case name 9');19const reporter = require('stryker-parent-reporter');20reporter.currentTestCaseStarted('test case name 10');21const reporter = require('stryker-parent-reporter');22reporter.currentTestCaseStarted('test case name 11');
Using AI Code Generation
1var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;2var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;3module.exports = function () {4 currentTestCaseStarted('test1');5 currentTestCaseStarted('test2');6};7var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;8var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;9module.exports = function () {10 currentTestCaseStarted('test3');11 currentTestCaseStarted('test4');12};13var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;14var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;15module.exports = function () {16 currentTestCaseStarted('test5');17 currentTestCaseStarted('test6');18};19var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;20var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;21module.exports = function () {22 currentTestCaseStarted('test7');23 currentTestCaseStarted('test8');24};25var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;26var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;27module.exports = function () {28 currentTestCaseStarted('test9');29 currentTestCaseStarted('test10');30};31var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;32var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;33module.exports = function () {34 currentTestCaseStarted('test11');35 currentTestCaseStarted('test12');36};37var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;38var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;39module.exports = function () {40 currentTestCaseStarted('test13');41 currentTestCaseStarted('test14');42};
Using AI Code Generation
1module.exports = function (stryker) {2 stryker.currentTestCaseStarted();3 stryker.currentTestCaseFinished();4}5module.exports = function (stryker) {6 stryker.currentTestCaseStarted = function () {7 };8 stryker.currentTestCaseFinished = function () {9 };10};
Using AI Code Generation
1var strykerParent = require('stryker-parent');2var currentTestCase = strykerParent.currentTestCaseStarted();3currentTestCase.name = "test name";4currentTestCase.file = "test file";5currentTestCase.killed = 1;6currentTestCase.survived = 1;7currentTestCase.timedOut = 1;8currentTestCase.noCoverage = 1;9currentTestCase.coverage = 1;10currentTestCase.status = "status";11var strykerParent = require('stryker-parent');12var currentMutant = strykerParent.currentMutant();13currentMutant.mutantId = "mutant id";14currentMutant.mutatorName = "mutator name";15currentMutant.fileName = "file name";16currentMutant.line = 1;17currentMutant.column = 1;18currentMutant.killed = 1;19currentMutant.survived = 1;20currentMutant.timedOut = 1;21currentMutant.noCoverage = 1;22currentMutant.coverage = 1;23currentMutant.status = "status";24var strykerParent = require('stryker-parent');25var currentMutant = strykerParent.currentMutant();26currentMutant.mutantId = "mutant id";27currentMutant.mutatorName = "mutator name";28currentMutant.fileName = "file name";29currentMutant.line = 1;30currentMutant.column = 1;31currentMutant.killed = 1;32currentMutant.survived = 1;33currentMutant.timedOut = 1;34currentMutant.noCoverage = 1;35currentMutant.coverage = 1;36currentMutant.status = "status";37var strykerParent = require('stryker-parent');38var currentMutant = strykerParent.currentMutant();39currentMutant.mutantId = "mutant id";40currentMutant.mutatorName = "mutator name";41currentMutant.fileName = "file name";42currentMutant.line = 1;43currentMutant.column = 1;44currentMutant.killed = 1;45currentMutant.survived = 1;46currentMutant.timedOut = 1;
Using AI Code Generation
1var currentTestCaseStarted = require('stryker-parent').currentTestCaseStarted;2var stryker = require('stryker-api/core');3stryker.currentTestCaseStarted = currentTestCaseStarted;4stryker.currentTestName = currentTestCaseStarted.name;5var currentTestCaseFinished = require('stryker-parent').currentTestCaseFinished;6var stryker = require('stryker-api/core');7stryker.currentTestCaseFinished = currentTestCaseFinished;8stryker.currentTestStatus = currentTestCaseFinished.status;9var currentTestRunFinished = require('stryker-parent').currentTestRunFinished;10var stryker = require('stryker-api/core');11stryker.currentTestRunFinished = currentTestRunFinished;12stryker.currentRunStatus = currentTestRunFinished.status;13var currentTestRunStarted = require('stryker-parent').currentTestRunStarted;14var stryker = require('stryker-api/core');15stryker.currentTestRunStarted = currentTestRunStarted;16stryker.currentRunStarted = currentTestRunStarted.name;17var currentTestRunStarted = require('stryker-parent').currentTestRunStarted;18var stryker = require('stryker-api/core');19stryker.currentTestRunStarted = currentTestRunStarted;20stryker.currentRunStarted = currentTestRunStarted.name;21var currentTestRunStarted = require('stryker-parent').currentTestRunStarted;22var stryker = require('stryker-api/core');23stryker.currentTestRunStarted = currentTestRunStarted;24stryker.currentRunStarted = currentTestRunStarted.name;25var currentTestRunStarted = require('stryker-parent').currentTestRunStarted;26var stryker = require('stryker-api/core');27stryker.currentTestRunStarted = currentTestRunStarted;28stryker.currentRunStarted = currentTestRunStarted.name;
Using AI Code Generation
1this.currentTestCaseStarted({2});3this.currentTestCaseStarted({4});5this.currentTestCaseStarted({6});7this.currentTestCaseStarted({8});9this.currentTestCaseStarted({10});11this.currentTestCaseStarted({12});13this.currentTestCaseStarted({14});15this.currentTestCaseStarted({16});17this.currentTestCaseStarted({18});19this.currentTestCaseStarted({20});21this.currentTestCaseStarted({22});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!