How to use mutantToIdentifyingKey method in stryker-parent

Best JavaScript code snippet using stryker-parent

incremental-differ.ts

Source:incremental-differ.ts Github

copy

Full Screen

...104 let reusedMutantCount = 0;105 const currentMutantKeys = new Set<string>();106 const mutants = currentMutants.map((mutant) => {107 const relativeFileName = toRelativeNormalizedFileName(mutant.fileName);108 const mutantKey = mutantToIdentifyingKey(mutant, relativeFileName);109 currentMutantKeys.add(mutantKey);110 if (!mutant.status && !this.options.force) {111 const oldMutant = reusableMutantsByKey.get(mutantKey);112 if (oldMutant) {113 const coveringTests = testCoverage.forMutant(mutant.id);114 const killedByTestKeys = oldKilledTestKeysByMutantKey.get(mutantKey);115 if (mutantCanBeReused(mutant, oldMutant, mutantKey, coveringTests, killedByTestKeys)) {116 reusedMutantCount++;117 const { status, statusReason, testsCompleted } = oldMutant;118 return {119 ...mutant,120 status,121 statusReason,122 testsCompleted,123 coveredBy: [...(coveringTests ?? [])].map(({ id }) => id),124 killedBy: testKeysToId(killedByTestKeys),125 };126 }127 } else {128 mutantStatisticsCollector.count(relativeFileName, 'added');129 }130 }131 return mutant;132 });133 // Make sure that old mutants that didn't run this time around aren't forgotten134 for (const [mutantKey, oldResult] of reusableMutantsByKey) {135 // Do an additional check to see if the mutant is in mutated range.136 //137 // For example:138 // ```diff139 // - return a || b;140 // + return a && b;141 // ```142 // The conditional expression mutator here decides to _not_ mutate b to `false` the second time around. (even though the text of "b" itself didn't change)143 // Not doing this additional check would result in a sticky mutant that is never removed144 if (!currentMutantKeys.has(mutantKey) && !this.isInMutatedScope(oldResult.relativeFileName, oldResult)) {145 const coverage = oldCoverageTestKeysByMutantKey.get(mutantKey) ?? [];146 const killed = oldKilledTestKeysByMutantKey.get(mutantKey) ?? [];147 const coveredBy = testKeysToId(coverage);148 const killedBy = testKeysToId(killed);149 const reusedMutant = {150 ...oldResult,151 id: mutantKey,152 fileName: path.resolve(oldResult.relativeFileName),153 replacement: oldResult.replacement ?? oldResult.mutatorName,154 coveredBy,155 killedBy,156 };157 mutants.push(reusedMutant);158 testCoverage.addCoverage(reusedMutant.id, coveredBy);159 }160 }161 if (this.logger.isInfoEnabled()) {162 const testInfo = testCoverage.hasCoverage ? `\n\tTests:\t\t${testStatisticsCollector.createTotalsReport()}` : '';163 this.logger.info(164 `Incremental report:\n\tMutants:\t${mutantStatisticsCollector.createTotalsReport()}` +165 testInfo +166 `\n\tResult:\t\t${chalk.yellowBright(reusedMutantCount)} of ${currentMutants.length} mutant result(s) are reused.`167 );168 }169 if (this.logger.isDebugEnabled()) {170 const lineSeparator = '\n\t\t';171 const noChanges = 'No changes';172 const detailedMutantSummary = `${lineSeparator}${mutantStatisticsCollector.createDetailedReport().join(lineSeparator) || noChanges}`;173 const detailedTestsSummary = `${lineSeparator}${testStatisticsCollector.createDetailedReport().join(lineSeparator) || noChanges}`;174 this.logger.debug(`Detailed incremental report:\n\tMutants: ${detailedMutantSummary}\n\tTests: ${detailedTestsSummary}`);175 }176 return mutants;177 function testKeysToId(testKeys: Iterable<string> | undefined) {178 return [...(testKeys ?? [])]179 .map((id) => testInfoByKey.get(id))180 .filter(notEmpty)181 .map(({ test: { id } }) => id);182 }183 function collectReusableMutantsByKey(log: Logger) {184 return new Map(185 Object.entries(files).flatMap(([fileName, oldFile]) => {186 const relativeFileName = toRelativeNormalizedFileName(fileName);187 const currentFileSource = currentRelativeFiles.get(relativeFileName);188 if (currentFileSource) {189 log.trace('Diffing %s', relativeFileName);190 const { results, removeCount } = performFileDiff(oldFile.source, currentFileSource, oldFile.mutants);191 mutantStatisticsCollector.count(relativeFileName, 'removed', removeCount);192 return results.map((m) => [193 mutantToIdentifyingKey(m, relativeFileName),194 {195 ...m,196 relativeFileName,197 },198 ]);199 }200 mutantStatisticsCollector.count(relativeFileName, 'removed', oldFile.mutants.length);201 // File has since been deleted, these mutants are not reused202 return [];203 })204 );205 }206 function collectReusableTestInfo(log: Logger) {207 const byId = new Map<string, { relativeFileName: string; test: TestDefinition; key: string }>();208 const byKey = new Map<string, TestInfo>();209 Object.entries(testFiles ?? {}).forEach(([fileName, oldTestFile]) => {210 const relativeFileName = toRelativeNormalizedFileName(fileName);211 const currentFileSource = currentRelativeFiles.get(relativeFileName);212 if (currentFileSource !== undefined && oldTestFile.source !== undefined) {213 log.trace('Diffing %s', relativeFileName);214 const locatedTests = closeLocations(oldTestFile);215 const { results, removeCount } = performFileDiff(oldTestFile.source, currentFileSource, locatedTests);216 testStatisticsCollector.count(relativeFileName, 'removed', removeCount);217 results.forEach((test) => {218 const key = testToIdentifyingKey(test, relativeFileName);219 const testInfo = { key, test, relativeFileName };220 byId.set(test.id, testInfo);221 byKey.set(key, testInfo);222 });223 } else {224 // No sources to compare, we should do our best with the info we do have225 oldTestFile.tests.map((test) => {226 const key = testToIdentifyingKey(test, relativeFileName);227 const testInfo = { key, test, relativeFileName };228 byId.set(test.id, testInfo);229 byKey.set(key, testInfo);230 });231 }232 });233 return { byId, byKey };234 }235 function collectOldKilledAndCoverageMatrix() {236 const oldCoverageByMutantKey = new Map<string, Set<string>>();237 const oldKilledByMutantKey = new Map<string, Set<string>>();238 for (const [key, mutant] of reusableMutantsByKey) {239 const killedRow = new Set(mutant.killedBy?.map((testId) => oldTestsById.get(testId)?.key).filter(notEmpty));240 const coverageRow = new Set(mutant.coveredBy?.map((testId) => oldTestsById.get(testId)?.key).filter(notEmpty));241 killedRow.forEach((killed) => coverageRow.add(killed));242 oldCoverageByMutantKey.set(key, coverageRow);243 oldKilledByMutantKey.set(key, killedRow);244 }245 return { oldCoverageByMutantKey, oldKilledByMutantKey };246 }247 function collectCurrentTestInfo() {248 const byTestKey = new Map<string, { relativeFileName: string; test: TestResult }>();249 for (const testResult of testCoverage.testsById.values()) {250 const relativeFileName = toRelativeNormalizedFileName(testResult.fileName);251 const key = testToIdentifyingKey(testResult, relativeFileName);252 const info = { relativeFileName, test: testResult, key: key };253 byTestKey.set(key, info);254 }255 return byTestKey;256 }257 function mutantCanBeReused(258 mutant: Mutant,259 oldMutant: schema.MutantResult,260 mutantKey: string,261 coveringTests: ReadonlySet<TestResult> | undefined,262 oldKillingTests: Set<string> | undefined263 ): boolean {264 if (!testCoverage.hasCoverage) {265 // This is the best we can do when the test runner didn't report coverage.266 // We assume that all mutant test results can be reused,267 // End users can use --force to force retesting of certain mutants268 return true;269 }270 if (oldMutant.status === MutantStatus.Ignored) {271 // Was previously ignored, but not anymore, we need to run it now272 return false;273 }274 const testsDiff = diffTestCoverage(mutant.id, oldCoverageTestKeysByMutantKey.get(mutantKey), coveringTests);275 if (oldMutant.status === MutantStatus.Killed) {276 if (oldKillingTests) {277 for (const killingTest of oldKillingTests) {278 if (testsDiff.get(killingTest) === 'same') {279 return true;280 }281 }282 }283 // Killing tests has changed or no longer exists284 return false;285 }286 for (const action of testsDiff.values()) {287 if (action === 'added') {288 // A non-killed mutant got a new test, we need to run it289 return false;290 }291 }292 // A non-killed mutant did not get new tests, no need to rerun it293 return true;294 }295 /**296 * Determines if there is a diff between old test coverage and new test coverage.297 */298 function diffTestCoverage(299 mutantId: string,300 oldCoveringTestKeys: Set<string> | undefined,301 newCoveringTests: ReadonlySet<TestResult> | undefined302 ): Map<string, DiffAction> {303 const result = new Map<string, DiffAction>();304 if (newCoveringTests) {305 for (const newTest of newCoveringTests) {306 const key = testToIdentifyingKey(newTest, toRelativeNormalizedFileName(newTest.fileName));307 result.set(key, oldCoveringTestKeys?.has(key) ? 'same' : 'added');308 }309 }310 if (oldCoveringTestKeys) {311 const isStatic = testCoverage.hasStaticCoverage(mutantId);312 for (const oldTestKey of oldCoveringTestKeys) {313 if (!result.has(oldTestKey)) {314 // Static mutants might not have covering tests, but the test might still exist315 if (isStatic && newTestKeys.has(oldTestKey)) {316 result.set(oldTestKey, 'same');317 } else {318 result.set(oldTestKey, 'removed');319 }320 }321 }322 }323 return result;324 }325 }326}327/**328 * Finds the diff of mutants and tests. Removes mutants / tests that no longer exist (changed or removed). Updates locations of mutants or tests that do still exist.329 * @param oldCode The old code to use for the diff330 * @param newCode The new (current) code to use for the diff331 * @param items The mutants or tests to be looked . These will be treated as immutable.332 * @returns A list of items with updated locations, without items that are changed.333 */334function performFileDiff<T extends { location: Location }>(oldCode: string, newCode: string, items: T[]): { results: T[]; removeCount: number } {335 const oldSourceNormalized = normalizeLineEndings(oldCode);336 const currentSrcNormalized = normalizeLineEndings(newCode);337 const diffChanges = diffMatchPatch.diff_main(oldSourceNormalized, currentSrcNormalized);338 const toDo = new Set(items.map((m) => ({ ...m, location: deepClone(m.location) })));339 const [added, removed] = [1, -1];340 const done: T[] = [];341 const currentPosition: Position = { column: 0, line: 0 };342 let removeCount = 0;343 for (const [change, text] of diffChanges) {344 if (toDo.size === 0) {345 // There are more changes, but nothing left to update.346 break;347 }348 const offset = calculateOffset(text);349 if (change === added) {350 for (const test of toDo) {351 const { location } = test;352 if (gte(currentPosition, location.start) && gte(location.end, currentPosition)) {353 // This item cannot be reused, code was added here354 removeCount++;355 toDo.delete(test);356 } else {357 locationAdd(location, offset, currentPosition.line === location.start.line);358 }359 }360 positionMove(currentPosition, offset);361 } else if (change === removed) {362 for (const item of toDo) {363 const {364 location: { start },365 } = item;366 const endOffset = positionMove({ ...currentPosition }, offset);367 if (gte(endOffset, start)) {368 // This item cannot be reused, the code it covers has changed369 removeCount++;370 toDo.delete(item);371 } else {372 locationAdd(item.location, negate(offset), currentPosition.line === start.line);373 }374 }375 } else {376 positionMove(currentPosition, offset);377 toDo.forEach((item) => {378 const { end } = item.location;379 if (gte(currentPosition, end)) {380 // We're done with this item, it can be reused381 toDo.delete(item);382 done.push(item);383 }384 });385 }386 }387 done.push(...toDo);388 return { results: done, removeCount };389}390/**391 * A greater-than-equals implementation for positions392 */393function gte(a: Position, b: Position) {394 return a.line > b.line || (a.line === b.line && a.column >= b.column);395}396function locationIncluded(haystack: Location, needle: Location) {397 const startIncluded = gte(needle.start, haystack.start);398 const endIncluded = gte(haystack.end, needle.end);399 return startIncluded && endIncluded;400}401function deepClone(loc: Location): Location {402 return { start: { ...loc.start }, end: { ...loc.end } };403}404/**405 * Reduces a mutant to a string that identifies the mutant across reports.406 * Consists of the relative file name, mutator name, replacement, and location407 */408function mutantToIdentifyingKey(409 { mutatorName, replacement, location: { start, end } }: Pick<Mutant, 'location' | 'mutatorName'> & { replacement?: string },410 relativeFileName: string411) {412 return `${relativeFileName}@${start.line}:${start.column}-${end.line}:${end.column}\n${mutatorName}: ${replacement}`;413}414function testToIdentifyingKey(415 { name, location, startPosition }: Pick<schema.TestDefinition, 'location' | 'name'> & Pick<TestResult, 'startPosition'>,416 relativeFileName: string | undefined417) {418 startPosition = startPosition ?? location?.start ?? { line: 0, column: 0 };419 return `${relativeFileName}@${startPosition.line}:${startPosition.column}\n${name}`;420}421export function toRelativeNormalizedFileName(fileName: string | undefined): string {422 return normalizeFileName(path.relative(process.cwd(), fileName ?? ''));...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var mutantToIdentifyingKey = require('stryker').mutantToIdentifyingKey;2var mutants = [];3var mutant = {};4mutant.id = 1;5mutant.location = {};6mutant.location.start = {};7mutant.location.start.line = 1;8mutant.location.start.column = 2;9mutant.location.end = {};10mutant.location.end.line = 3;11mutant.location.end.column = 4;12mutant.mutatorName = 'myMutator';13mutant.replacement = 'myReplacement';14mutantToIdentifyingKey(mutant);

Full Screen

Using AI Code Generation

copy

Full Screen

1const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;2const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;3const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;4const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;5const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;6const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;7const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;8const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;9const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;10const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;11const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;12const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;13const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;14const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;

Full Screen

Using AI Code Generation

copy

Full Screen

1const strykerParent = require('stryker-parent');2const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;3const strykerParent = require('stryker-parent');4const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;5const strykerParent = require('stryker-parent');6const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;7const strykerParent = require('stryker-parent');8const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;9const strykerParent = require('stryker-parent');10const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;11const strykerParent = require('stryker-parent');12const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;13const strykerParent = require('stryker-parent');14const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;15const strykerParent = require('stryker-parent');16const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;17const strykerParent = require('stryker-parent');18const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;19const strykerParent = require('stryker-parent');20const mutantToIdentifyingKey = strykerParent.mutantToIdentifyingKey;21const strykerParent = require('stryker-parent');

Full Screen

Using AI Code Generation

copy

Full Screen

1const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;2const mutant = {3};4console.log(mutantToIdentifyingKey(mutant));5const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;6const mutant = {7};8console.log(mutantToIdentifyingKey(mutant));

Full Screen

Using AI Code Generation

copy

Full Screen

1const mutantToIdentifyingKey = require('stryker-parent').mutantToIdentifyingKey;2const mutantId = mutantToIdentifyingKey({id: 1, location: {start: {line: 1, column: 2}, end: {line: 1, column: 3}}, mutatorName: 'foo', replacement: 'bar'});3const mutantToIdentifyingKey = require('stryker').mutantToIdentifyingKey;4const mutantId = mutantToIdentifyingKey({id: 1, location: {start: {line: 1, column: 2}, end: {line: 1, column: 3}}, mutatorName: 'foo', replacement: 'bar'});5const mutantToIdentifyingKey = require('stryker').mutantToIdentifyingKey;6const mutantId = mutantToIdentifyingKey({id: 1, location: {start: {line: 1, column: 2}, end: {line: 1, column: 3}}, mutatorName: 'foo', replacement: 'bar'});7const mutantToIdentifyingKey = require('stryker').mutantToIdentifyingKey;8const mutantId = mutantToIdentifyingKey({id: 1, location: {start: {line: 1, column: 2}, end: {line: 1, column: 3}}, mutatorName: 'foo', replacement: 'bar'});9const mutantToIdentifyingKey = require('stryker').mutantToIdentifyingKey;10const mutantId = mutantToIdentifyingKey({id: 1, location: {start: {line: 1, column: 2}, end: {line: 1, column: 3}}, mutatorName: 'foo', replacement: 'bar'});

Full Screen

Using AI Code Generation

copy

Full Screen

1const { mutantToIdentifyingKey } = require('@stryker-mutator/api/core');2const mutant = {3};4console.log(mutantToIdentifyingKey(mutant));5const { identifyingKeyToMutant } = require('@stryker-mutator/api/core');6const mutant = identifyingKeyToMutant("123:FooMutator:foo.js:[1,2]:bar");7console.log(mutant);8const { Mutant } = require('@stryker-mutator/api/core');9const mutant = new Mutant(123, 'FooMutator', 'foo.js', [1, 2], 'bar');10console.log(mutant);11const { MutantStatus } = require('@stryker-mutator/api/core');12console.log(MutantStatus);13const { MutantResult } = require('@stryker-mutator/api/core');14const mutantResult = new MutantResult(123, MutantStatus.Killed, 'foo.js');15console.log(mutantResult);16const { MutantTestCoverage } = require('@stryker-mutator/api/core');17const mutantTestCoverage = new MutantTestCoverage(

Full Screen

Using AI Code Generation

copy

Full Screen

1const { mutantToIdentifyingKey } = require('stryker-parent');2console.log(mutantToIdentifyingKey({ id: 1, fileName: 'foo.js', range: [0, 1] }));3const { mutantToIdentifyingKey } = require('stryker-parent');4console.log(mutantToIdentifyingKey({ id: 1, fileName: 'foo.js', range: [0, 1] }));5const { mutantToIdentifyingKey } = require('stryker-parent');6console.log(mutantToIdentifyingKey({ id: 1, fileName: 'foo.js', range: [0, 1] }));7const { mutantToIdentifyingKey } = require('stryker-parent');8console.log(mutantToIdentifyingKey({ id: 1, fileName: 'foo.js', range: [0, 1] }));9const { mutantToIdentifyingKey } = require('stryker-parent');10console.log(mutantToIdentifyingKey({ id: 1, fileName: 'foo.js', range: [0, 1] }));11const { mutantToIdentifyingKey } = require('stryker-parent');12console.log(mutantToIdentifyingKey({ id: 1, fileName: 'foo.js', range: [0, 1] }));13const { mutantToIdentifyingKey } = require('stryker-parent');14console.log(mutantToIdentifyingKey({ id: 1, fileName: 'foo.js', range: [0, 1] }));

Full Screen

Using AI Code Generation

copy

Full Screen

1const mutantToIdentifyingKey = require("stryker-parent").mutantToIdentifyingKey;2const mutant = {id: 1, mutatorName: "myMutator", fileName: "test.js", replacement: "true", range: [0, 3]};3console.log(mutantToIdentifyingKey(mutant));4const identifyingKeyToMutant = require("stryker-parent").identifyingKeyToMutant;5const identifyingKey = "1_myMutator_test.js_true_0_3";6console.log(identifyingKeyToMutant(identifyingKey));7const identifyingKeyToMutant = require("stryker-parent").identifyingKeyToMutant;8const identifyingKey = "1_myMutator_test.js_true_0_3";9console.log(identifyingKeyToMutant(identifyingKey));10const getMutantId = require("stryker-parent").getMutantId;11const mutant = {id: 1, mutatorName: "myMutator", fileName: "test.js", replacement: "true", range: [0, 3]};12console.log(getMutantId(mutant));13const getMutantId = require("stryker-parent").getMutantId;14const mutant = {id: 1, mutatorName: "myMutator", fileName: "test.js", replacement: "true", range: [0, 3]};15console.log(getMutantId(mutant));

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run stryker-parent automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful