Best JavaScript code snippet using stryker-parent
helpers.ts
Source:helpers.ts
1import { promises as fsPromises } from 'fs';2import { mutationTestReportSchema } from '@stryker-mutator/api/report';3import { expect } from 'chai';4import path from 'path';5import { calculateMutationTestMetrics, MetricsResult, Metrics } from 'mutation-testing-metrics';6import { execSync, ExecException } from 'child_process';7interface PipedStdioSyncExecException extends ExecException {8 stdout: Uint8Array;9 stderr: Uint8Array;10 status: number;11}12export interface ExecStrykerResult {13 exitCode: number;14 stdout: string;15 stderr: string;16}17export function execStryker(cmd: string): ExecStrykerResult {18 let result: ExecStrykerResult;19 try {20 const out = execSync(cmd, { stdio: 'pipe' });21 result = {22 exitCode: 0,23 stderr: '',24 stdout: out.toString(),25 };26 } catch (err) {27 const childError = err as PipedStdioSyncExecException;28 result = {29 exitCode: childError.status,30 stderr: childError.stderr.toString(),31 stdout: childError.stdout.toString(),32 };33 }34 if (result.stdout) {35 console.log(result.stdout);36 }37 if (result.stderr) {38 console.error(result.stderr);39 }40 return result;41}42export async function readMutationTestResult(eventResultDirectory = path.resolve('reports', 'mutation', 'events')) {43 const allReportFiles = await fsPromises.readdir(eventResultDirectory);44 const mutationTestReportFile = allReportFiles.find((file) => !!file.match(/.*onMutationTestReportReady.*/));45 expect(mutationTestReportFile).ok;46 const mutationTestReportContent = await fsPromises.readFile(path.resolve(eventResultDirectory, mutationTestReportFile || ''), 'utf8');47 const report = JSON.parse(mutationTestReportContent) as mutationTestReportSchema.MutationTestResult;48 const metricsResult = calculateMutationTestMetrics(report);49 return metricsResult;50}51export async function readMutationTestingJsonResult(jsonReportFile = path.resolve('reports', 'mutation', 'mutation.json')) {52 const mutationTestReportContent = await fsPromises.readFile(jsonReportFile, 'utf8');53 const report = JSON.parse(mutationTestReportContent) as mutationTestReportSchema.MutationTestResult;54 const metricsResult = calculateMutationTestMetrics(report);55 return metricsResult;56}57type WritableMetricsResult = {58 -readonly [K in keyof MetricsResult]: MetricsResult[K];59};60export function readLogFile(fileName = path.resolve('stryker.log')): Promise<string> {61 return fsPromises.readFile(fileName, 'utf8');62}63export async function expectMetricsResult(expectedMetricsResult: Partial<MetricsResult>) {64 const actualMetricsResult = await readMutationTestResult();65 const actualSnippet: Partial<WritableMetricsResult> = {};66 for (const key in expectedMetricsResult) {67 actualSnippet[key as keyof MetricsResult] = actualMetricsResult.systemUnderTestMetrics[key as keyof MetricsResult] as any;68 }69 if (actualSnippet.metrics) {70 if (typeof actualSnippet.metrics.mutationScore === 'number') {71 actualSnippet.metrics.mutationScore = parseFloat(actualSnippet.metrics.mutationScore.toFixed(2));72 }73 if (typeof actualSnippet.metrics.mutationScoreBasedOnCoveredCode === 'number') {74 actualSnippet.metrics.mutationScoreBasedOnCoveredCode = parseFloat(actualSnippet.metrics.mutationScoreBasedOnCoveredCode.toFixed(2));75 }76 }77 expect(actualSnippet).deep.eq(expectedMetricsResult);78}79export async function expectMetricsJson(expectedMetrics: Partial<Metrics>) {80 const actualMetricsResult = await readMutationTestingJsonResult();81 expectActualMetrics(expectedMetrics, actualMetricsResult.systemUnderTestMetrics);82}83/**84 * @deprecated please use expectMetricsJson instead (and activate the json reporter)85 */86export async function expectMetrics(expectedMetrics: Partial<Metrics>) {87 const actualMetricsResult = await readMutationTestResult();88 expectActualMetrics(expectedMetrics, actualMetricsResult.systemUnderTestMetrics);89}90export function expectActualMetrics(expectedMetrics: Partial<Metrics>, actualMetricsResult: MetricsResult) {91 const actualMetrics: Partial<Metrics> = {};92 Object.entries(expectedMetrics).forEach(([key]) => {93 if (key === 'mutationScore' || key === 'mutationScoreBasedOnCoveredCode') {94 actualMetrics[key] = parseFloat(actualMetricsResult.metrics[key].toFixed(2));95 } else {96 actualMetrics[key as keyof Metrics] = actualMetricsResult.metrics[key as keyof Metrics];97 }98 });99 expect(actualMetrics).deep.eq(expectedMetrics);100}101export function produceMetrics(metrics: Partial<Metrics>): Metrics {102 return {103 compileErrors: 0,104 killed: 0,105 mutationScore: 0,106 mutationScoreBasedOnCoveredCode: 0,107 noCoverage: 0,108 runtimeErrors: 0,109 survived: 0,110 timeout: 0,111 ignored: 0,112 totalCovered: 0,113 totalDetected: 0,114 totalInvalid: 0,115 totalMutants: 0,116 totalUndetected: 0,117 totalValid: 0,118 ...metrics,119 };...
Using AI Code Generation
1const strykerParent = require('stryker-parent');2const path = require('path');3const fs = require('fs');4const tempFolder = path.resolve(__dirname, 'temp');5const reportFile = path.resolve(tempFolder, 'mutation-report.json');6const report = strykerParent.mutationTestReportFile(reportFile);7### `mutationTestReportFile(reportFile)`8 {9 {10 "location": { "start": { "line": 1, "column": 0 }, "end": { "line": 1, "column": 1 } },11 "originalLines": "const a = true;",12 "mutatedLines": "const a = false;"13 }14 }15 * `mutationScore` - The mutation score (percentage of mutants that were detected)
Using AI Code Generation
1var MutationTestReportFile = require('stryker-parent').MutationTestReportFile;2var report = new MutationTestReportFile('test', 'test', 'test', 1, 2, 3, 4, 5);3console.log(report.toString());4console.log(report.toJSON());5MutationTestReportFile { name: 'test',6 runtimeErrors: 5 }7{"name":"test","source":"test","mutatorName":"test","killed":1,"survived":2,"timeout":3,"noCoverage":4,"runtimeErrors":5}8var MutationTestResult = require('stryker-parent').MutationTestResult;9var result = new MutationTestResult('test', '1.0', {high: 80, low: 60}, [new MutationTestReportFile('test', 'test', 'test', 1, 2, 3, 4, 5)]);10result.write('test.json');11{"name":"test","schemaVersion":"1.0","thresholds":{"high":80,"low":60},"files":[{"name":"test","source":"test","mutatorName":"test","k
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!!