Best JavaScript code snippet using stryker-parent
factory.ts
Source:factory.ts
1import Ajv from 'ajv';2import {3 File,4 Location,5 MutationScoreThresholds,6 StrykerOptions,7 strykerCoreSchema,8 WarningOptions,9 Mutant,10 MutantTestCoverage,11 MutantResult,12 MutantCoverage,13 schema,14 MutantStatus,15} from '@stryker-mutator/api/core';16import { Logger } from '@stryker-mutator/api/logging';17import { Reporter, SourceFile } from '@stryker-mutator/api/report';18import { calculateMutationTestMetrics, Metrics, MetricsResult, MutationTestMetricsResult } from 'mutation-testing-metrics';19import sinon from 'sinon';20import { Injector } from 'typed-inject';21import { PluginResolver } from '@stryker-mutator/api/plugin';22import {23 MutantRunOptions,24 DryRunOptions,25 DryRunStatus,26 TestRunner,27 SuccessTestResult,28 FailedTestResult,29 SkippedTestResult,30 CompleteDryRunResult,31 ErrorDryRunResult,32 TimeoutDryRunResult,33 KilledMutantRunResult,34 SurvivedMutantRunResult,35 MutantRunStatus,36 TimeoutMutantRunResult,37 ErrorMutantRunResult,38 TestStatus,39 TestResult,40} from '@stryker-mutator/api/test-runner';41import { Checker, CheckResult, CheckStatus, FailedCheckResult } from '@stryker-mutator/api/check';42const ajv = new Ajv({ useDefaults: true, strict: false });43/**44 * This validator will fill in the defaults of stryker options as registered in the schema.45 */46function strykerOptionsValidator(overrides: Partial<StrykerOptions>): asserts overrides is StrykerOptions {47 const ajvValidator = ajv.compile(strykerCoreSchema);48 if (!ajvValidator(overrides)) {49 throw new Error('Unknown stryker options ' + ajv.errorsText(ajvValidator.errors));50 }51}52/**53 * A 1x1 png base64 encoded54 */55export const PNG_BASE64_ENCODED =56 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAMSURBVBhXY/j//z8ABf4C/qc1gYQAAAAASUVORK5CYII=';57/**58 * Use this factory method to create deep test data59 * @param defaults60 */61function factoryMethod<T>(defaultsFactory: () => T) {62 return (overrides?: Partial<T>): T => Object.assign({}, defaultsFactory(), overrides);63}64export const location = factoryMethod<Location>(() => ({ start: { line: 0, column: 0 }, end: { line: 0, column: 0 } }));65export function pluginResolver(): sinon.SinonStubbedInstance<PluginResolver> {66 return {67 resolve: sinon.stub(),68 resolveAll: sinon.stub(),69 resolveValidationSchemaContributions: sinon.stub(),70 };71}72export const warningOptions = factoryMethod<WarningOptions>(() => ({73 unknownOptions: true,74 preprocessorErrors: true,75 unserializableOptions: true,76}));77export const killedMutantResult = (overrides?: Partial<Omit<MutantResult, 'status'>>): MutantResult =>78 mutantResult({ ...overrides, status: MutantStatus.Killed, killedBy: ['45'], testsCompleted: 2 });79export const timeoutMutantResult = (overrides?: Partial<Omit<MutantResult, 'status'>>): MutantResult =>80 mutantResult({ ...overrides, status: MutantStatus.Timeout, statusReason: 'expected error' });81export const runtimeErrorMutantResult = (overrides?: Partial<Omit<MutantResult, 'status'>>): MutantResult =>82 mutantResult({ ...overrides, status: MutantStatus.RuntimeError, statusReason: 'expected error' });83export const ignoredMutantResult = (overrides?: Partial<Omit<MutantResult, 'status'>>): MutantResult =>84 mutantResult({ ...overrides, status: MutantStatus.Ignored, statusReason: 'Ignored by "fooMutator" in excludedMutations' });85export const noCoverageMutantResult = (overrides?: Partial<Omit<MutantResult, 'status'>>): MutantResult =>86 mutantResult({ ...overrides, status: MutantStatus.NoCoverage });87export const mutantResult = factoryMethod<MutantResult>(() => ({88 id: '256',89 location: location(),90 mutatorName: '',91 range: [0, 0],92 replacement: '',93 fileName: 'file.js',94 status: MutantStatus.Survived,95 coveredBy: ['1', '2'],96 testsCompleted: 2,97 static: false,98}));99export const mutationTestReportSchemaMutantResult = factoryMethod<schema.MutantResult>(() => ({100 id: '256',101 location: location(),102 mutatedLines: '',103 mutatorName: '',104 originalLines: '',105 range: [0, 0],106 replacement: '',107 sourceFilePath: '',108 status: MutantStatus.Killed,109 testsRan: [''],110}));111export const mutationTestReportSchemaFileResult = factoryMethod<schema.FileResult>(() => ({112 language: 'javascript',113 mutants: [mutationTestReportSchemaMutantResult()],114 source: 'export function add (a, b) { return a + b; }',115}));116export const mutationTestReportSchemaTestFile = factoryMethod<schema.TestFile>(() => ({117 tests: [],118 source: '',119}));120export const mutationTestReportSchemaTestDefinition = factoryMethod<schema.TestDefinition>(() => ({121 id: '4',122 name: 'foo should be bar',123}));124export const mutationTestReportSchemaMutationTestResult = factoryMethod<schema.MutationTestResult>(() => ({125 files: {126 'fileA.js': mutationTestReportSchemaFileResult(),127 },128 schemaVersion: '1',129 thresholds: {130 high: 81,131 low: 19,132 },133}));134export const mutationTestMetricsResult = factoryMethod<MutationTestMetricsResult>(() =>135 calculateMutationTestMetrics(mutationTestReportSchemaMutationTestResult())136);137export const mutant = factoryMethod<Mutant>(() => ({138 id: '42',139 fileName: 'file',140 mutatorName: 'foobarMutator',141 location: location(),142 replacement: 'replacement',143}));144export const metrics = factoryMethod<Metrics>(() => ({145 compileErrors: 0,146 killed: 0,147 mutationScore: 0,148 mutationScoreBasedOnCoveredCode: 0,149 noCoverage: 0,150 runtimeErrors: 0,151 survived: 0,152 timeout: 0,153 ignored: 0,154 totalCovered: 0,155 totalDetected: 0,156 totalInvalid: 0,157 totalMutants: 0,158 totalUndetected: 0,159 totalValid: 0,160}));161export const metricsResult = factoryMethod<MetricsResult>(() => ({162 childResults: [],163 metrics: metrics({}),164 name: '',165}));166export function logger(): sinon.SinonStubbedInstance<Logger> {167 return {168 debug: sinon.stub(),169 error: sinon.stub(),170 fatal: sinon.stub(),171 info: sinon.stub(),172 isDebugEnabled: sinon.stub(),173 isErrorEnabled: sinon.stub(),174 isFatalEnabled: sinon.stub(),175 isInfoEnabled: sinon.stub(),176 isTraceEnabled: sinon.stub(),177 isWarnEnabled: sinon.stub(),178 trace: sinon.stub(),179 warn: sinon.stub(),180 };181}182export function testRunner(): sinon.SinonStubbedInstance<Required<TestRunner>> {183 return {184 init: sinon.stub(),185 dryRun: sinon.stub(),186 mutantRun: sinon.stub(),187 dispose: sinon.stub(),188 };189}190export function checker(): sinon.SinonStubbedInstance<Checker> {191 return {192 check: sinon.stub(),193 init: sinon.stub(),194 };195}196export const checkResult = factoryMethod<CheckResult>(() => ({197 status: CheckStatus.Passed,198}));199export const failedCheckResult = factoryMethod<FailedCheckResult>(() => ({200 status: CheckStatus.CompileError,201 reason: 'Cannot call "foo" of undefined',202}));203export const testResult = factoryMethod<TestResult>(() => ({204 id: 'spec1',205 name: 'name',206 status: TestStatus.Success,207 timeSpentMs: 10,208}));209export const successTestResult = factoryMethod<SuccessTestResult>(() => ({210 id: 'spec1',211 name: 'foo should be bar',212 status: TestStatus.Success,213 timeSpentMs: 32,214}));215export const failedTestResult = factoryMethod<FailedTestResult>(() => ({216 id: 'spec2',217 name: 'foo should be bar',218 status: TestStatus.Failed,219 timeSpentMs: 32,220 failureMessage: 'foo was baz',221}));222export const skippedTestResult = factoryMethod<SkippedTestResult>(() => ({223 id: 'spec31',224 status: TestStatus.Skipped,225 timeSpentMs: 0,226 name: 'qux should be quux',227}));228export const mutantRunOptions = factoryMethod<MutantRunOptions>(() => ({229 activeMutant: mutant(),230 timeout: 2000,231 sandboxFileName: '.stryker-tmp/sandbox123/file',232 disableBail: false,233}));234export const dryRunOptions = factoryMethod<DryRunOptions>(() => ({235 coverageAnalysis: 'off',236 timeout: 2000,237 disableBail: false,238}));239export const completeDryRunResult = factoryMethod<CompleteDryRunResult>(() => ({240 status: DryRunStatus.Complete,241 tests: [],242}));243export const mutantCoverage = factoryMethod<MutantCoverage>(() => ({244 perTest: {},245 static: {},246}));247export const errorDryRunResult = factoryMethod<ErrorDryRunResult>(() => ({248 status: DryRunStatus.Error,249 errorMessage: 'example error',250}));251export const timeoutDryRunResult = factoryMethod<TimeoutDryRunResult>(() => ({252 status: DryRunStatus.Timeout,253}));254export const killedMutantRunResult = factoryMethod<KilledMutantRunResult>(() => ({255 status: MutantRunStatus.Killed,256 killedBy: ['spec1'],257 failureMessage: 'foo should be bar',258 nrOfTests: 1,259}));260export const survivedMutantRunResult = factoryMethod<SurvivedMutantRunResult>(() => ({261 status: MutantRunStatus.Survived,262 nrOfTests: 2,263}));264export const timeoutMutantRunResult = factoryMethod<TimeoutMutantRunResult>(() => ({265 status: MutantRunStatus.Timeout,266}));267export const errorMutantRunResult = factoryMethod<ErrorMutantRunResult>(() => ({268 status: MutantRunStatus.Error,269 errorMessage: 'Cannot find foo of undefined',270}));271export const mutationScoreThresholds = factoryMethod<MutationScoreThresholds>(() => ({272 break: null,273 high: 80,274 low: 60,275}));276export const strykerOptions = factoryMethod<StrykerOptions>(() => {277 const options: Partial<StrykerOptions> = {};278 strykerOptionsValidator(options);279 return options;280});281export const strykerWithPluginOptions = <T>(pluginOptions: T): StrykerOptions & T => {282 return { ...strykerOptions(), ...pluginOptions };283};284export const ALL_REPORTER_EVENTS: Array<keyof Reporter> = [285 'onSourceFileRead',286 'onAllSourceFilesRead',287 'onAllMutantsMatchedWithTests',288 'onMutantTested',289 'onAllMutantsTested',290 'onMutationTestReportReady',291 'wrapUp',292];293export function reporter(name = 'fooReporter'): sinon.SinonStubbedInstance<Required<Reporter>> {294 const reporters = { name } as any;295 ALL_REPORTER_EVENTS.forEach((event) => (reporters[event] = sinon.stub()));296 return reporters;297}298export const mutantTestCoverage = factoryMethod<MutantTestCoverage>(() => ({299 coveredBy: undefined,300 fileName: '',301 id: '1',302 mutatorName: '',303 static: false,304 replacement: '',305 location: location(),306 estimatedNetTime: 42,307}));308export function injector(): sinon.SinonStubbedInstance<Injector> {309 const injectorMock: sinon.SinonStubbedInstance<Injector> = {310 dispose: sinon.stub(),311 injectClass: sinon.stub(),312 injectFunction: sinon.stub(),313 provideClass: sinon.stub(),314 provideFactory: sinon.stub(),315 provideValue: sinon.stub(),316 resolve: sinon.stub(),317 };318 injectorMock.provideClass.returnsThis();319 injectorMock.provideFactory.returnsThis();320 injectorMock.provideValue.returnsThis();321 return injectorMock;322}323export function file(): File {324 return new File('', '');325}326export const sourceFile = factoryMethod<SourceFile>(() => ({327 content: 'foo.bar()',328 path: 'foo.js',329}));330export function fileNotFoundError(): NodeJS.ErrnoException {331 return createErrnoException('ENOENT');332}333export function fileAlreadyExistsError(): NodeJS.ErrnoException {334 return createErrnoException('EEXIST');335}336export function createIsDirError(): NodeJS.ErrnoException {337 return createErrnoException('EISDIR');338}339function createErrnoException(errorCode: string) {340 const fileNotFoundErrorInstance: NodeJS.ErrnoException = new Error('');341 fileNotFoundErrorInstance.code = errorCode;342 return fileNotFoundErrorInstance;...
Using AI Code Generation
1var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;2var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;3var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;4var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;5var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;6var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;7var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;8var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;9var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;10var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;11var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;12var noCoverageMutantResult = require('stryker-parent').noCoverageMutantResult;
Using AI Code Generation
1const { noCoverageMutantResult } = require('stryker-parent');2const { noCoverageMutantResult } = require('stryker-parent');3const { noCoverageMutantResult } = require('stryker-parent');4const { noCoverageMutantResult } = require('stryker-parent');5const { noCoverageMutantResult } = require('stryker-parent');6const { noCoverageMutantResult } = require('stryker-parent');7const { noCoverageMutantResult } = require('stryker-parent');8const { noCoverageMutantResult } = require('stryker-parent');9const { noCoverageMutantResult } = require('stryker-parent');10const { noCoverageMutantResult } = require('stryker-parent');11const { noCoverageMutantResult } = require('stryker-parent');12const { noCoverageMutantResult } = require('stryker-parent');13const { noCoverageMutantResult } = require('stryker-parent');14const { noCoverageMutantResult } = require('stryker-parent');15const { noCoverageMutantResult } = require('stryker-parent');
Using AI Code Generation
1const { noCoverageMutantResult } = require('stryker-parent');2const { expect } = require('chai');3describe('noCoverageMutantResult', () => {4 it('should return a mutant result with a status of NoCoverage', () => {5 const actual = noCoverageMutantResult();6 expect(actual.status).eq('NoCoverage');7 });8});9module.exports = {10};11function noCoverageMutantResult() {12 return { status: 'NoCoverage' };13}14{15}16{17}18{19}20{21}22module.exports = {23};24function noCoverageMutantResult() {25 return { status: 'NoCoverage' };26}27{28}29{30}31module.exports = {32};33function noCoverageMutantResult() {34 return { status: 'NoCoverage' };35}
Using AI Code Generation
1exports.noCoverageMutantResult = function (mutant) {2 return {3 };4};5{6}7module.exports = function(config){8 config.set({9 });10};11module.exports = function(config){12 config.set({13 commandRunner: {14 }15 });16};
Using AI Code Generation
1const strykerParent = require('stryker-parent');2const noCoverageMutantResult = strykerParent.noCoverageMutantResult;3const mutantResult = noCoverageMutantResult('file.js', 'function foo() { return 1; }', 'function foo() { return 2; }', 'function foo() { return 1; }');4console.log(mutantResult);5module.exports = function(config) {6 config.set({7 mochaOptions: {8 }9 });10};11const strykerParent = require('stryker-parent');12const noCoverageMutantResult = strykerParent.noCoverageMutantResult;13const mutantResult = noCoverageMutantResult('file.js', 'function foo() { return 1; }', 'function foo() { return 2; }', 'function foo() { return 1; }');14console.log(mutantResult);1517:51:49 (12345) INFO MochaTestRunner Starting sandbox [worker #0, session #12345]1617:51:49 (12345) INFO MochaTestRunner Starting sandbox [worker #1, session #12345]1717:51:49 (12345) INFO MochaTestRunner Starting sandbox [worker #2, session #12345]1817:51:49 (12345) INFO MochaTestRunner Starting sandbox [worker #3,
Using AI Code Generation
1const { noCoverageMutantResult } = require('stryker-parent');2const result = noCoverageMutantResult({id:1, mutatorName:'foo'});3console.log(result);4const { noCoverageMutantResult } = require('stryker-parent');5const result = noCoverageMutantResult({id:1, mutatorName:'foo'});6console.log(result);7const { noCoverageMutantResult } = require('stryker-parent');8const result = noCoverageMutantResult({id:1, mutatorName:'foo'});9console.log(result);10const { noCoverageMutantResult } = require('stryker-parent');11const result = noCoverageMutantResult({id:1, mutatorName:'foo'});12console.log(result);13const { noCoverageMutantResult } = require('stryker-parent');14const result = noCoverageMutantResult({id:1, mutatorName:'foo'});15console.log(result);16const { noCoverageMutantResult } = require('stryker-parent');17const result = noCoverageMutantResult({id:1, mutatorName:'foo'});18console.log(result);
Using AI Code Generation
1var mutator = require('stryker-parent').mutator;2var mutantResult = mutator.noCoverageMutantResult('1+1');3console.log(mutantResult);4{ id: '1+1', killed: false, mutatedLines: undefined, mutatorName: 'NoCoverageMutator' }5var mutator = require('stryker-parent').mutator;6var mutantResult = mutator.noCoverageMutantResult('1+1', true);7console.log(mutantResult);8{ id: '1+1', killed: true, mutatedLines: undefined, mutatorName: 'NoCoverageMutator' }9var mutator = require('stryker-parent').mutator;10var mutantResult = mutator.noCoverageMutantResult('1+1', false, [1, 2]);11console.log(mutantResult);12{ id: '1+1', killed: false, mutatedLines: [1, 2], mutatorName: 'NoCoverageMutator' }13var mutator = require('stryker-parent').mutator;14var mutantResult = mutator.noCoverageMutantResult('1+1', true, [1, 2]);15console.log(mutantResult);16{ id: '1+1', killed: true, mutatedLines: [1, 2], mutatorName: 'NoCoverageMutator' }17var mutator = require('stryker-parent').mutator;18var mutantResult = mutator.noCoverageMutantResult('1+1', false, [1, 2], 'NoCoverageMutator');19console.log(mutantResult);20{ id: '1+1', killed: false, mutatedLines: [1, 2], mutatorName: 'NoCoverageMutator' }
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!!