Best JavaScript code snippet using stryker-parent
validation-errors.ts
Source:validation-errors.ts
1import { ErrorObject } from 'ajv';2import groupby from 'lodash.groupby';3/**4 * Convert AJV errors to human readable messages5 * @param allErrors The AJV errors to describe6 */7export function describeErrors(allErrors: ErrorObject[]): string[] {8 const processedErrors = filterRelevantErrors(allErrors);9 return processedErrors.map(describeError);10}11/**12 * Filters the relevant AJV errors for error reporting.13 * Removes meta schema errors, merges type errors for the same `dataPath` and removes type errors for which another error also exist.14 * @param allErrors The raw source AJV errors15 * @example16 * This:17 * ```18 * [19 * {20 * keyword: 'type',21 * dataPath: '.mutator',22 * params: { type: 'string' },23 * [...]24 * },25 * {26 * keyword: 'required',27 * dataPath: '.mutator',28 * params: { missingProperty: 'name' },29 * [...]30 * },31 * {32 * keyword: 'oneOf',33 * dataPath: '.mutator',34 * params: { passingSchemas: null },35 * [...]36 * }37 * ]38 * ```39 *40 * Becomes:41 * ```42 * [43 * {44 * keyword: 'required',45 * dataPath: '.mutator',46 * params: { missingProperty: 'name' },47 * [...]48 * }49 * ]50 * ```51 */52function filterRelevantErrors(allErrors: ErrorObject[]): ErrorObject[] {53 // These are the "meta schema" keywords. A Meta schema is a schema consisting of other schemas. See https://json-schema.org/understanding-json-schema/structuring.html54 const META_SCHEMA_KEYWORDS = Object.freeze(['anyOf', 'allOf', 'oneOf']);55 // Split the meta errors from what I call "single errors" (the real errors)56 const [metaErrors, singleErrors] = split(allErrors, (error) => META_SCHEMA_KEYWORDS.includes(error.keyword));57 // Filter out the single errors we want to show58 const nonShadowedSingleErrors = removeShadowingErrors(singleErrors, metaErrors);59 // We're handling type errors differently, split them out60 const [typeErrors, nonTypeErrors] = split(nonShadowedSingleErrors, (error) => error.keyword === 'type');61 // Filter out the type errors that already have other errors as well.62 // For example when setting `logLevel: 4`, we don't want to see the error specifying that logLevel should be a string,63 // if the other error already specified that it should be one of the enum values.64 const nonShadowingTypeErrors = typeErrors.filter(65 (typeError) => !nonTypeErrors.some((nonTypeError) => nonTypeError.instancePath === typeError.instancePath)66 );67 const typeErrorsMerged = mergeTypeErrorsByPath(nonShadowingTypeErrors);68 return [...nonTypeErrors, ...typeErrorsMerged];69}70/**71 * Remove the single errors that are pointing to the same data path.72 * This can happen when using meta schemas.73 * For example, the "mutator" Stryker option can be either a `string` or a `MutatorDescriptor`.74 * A data object of `{ "foo": "bar" }` would result in 2 errors. One of a missing property "name" missing, and one that mutator itself should be a string.75 * @param singleErrors The 'real' errors76 * @param metaErrors The grouping errors77 */78function removeShadowingErrors(singleErrors: ErrorObject[], metaErrors: ErrorObject[]): ErrorObject[] {79 return singleErrors.filter((error) => {80 if (metaErrors.some((metaError) => error.instancePath.startsWith(metaError.instancePath))) {81 return !singleErrors.some(82 (otherError) => otherError.instancePath.startsWith(error.instancePath) && otherError.instancePath.length > error.instancePath.length83 );84 } else {85 return true;86 }87 });88}89function split<T>(items: T[], splitFn: (item: T) => boolean): [T[], T[]] {90 return [items.filter(splitFn), items.filter((error) => !splitFn(error))];91}92/**93 * Merge type errors that have the same path into 1.94 * @example95 * The 'plugins' Stryker option can have 2 types, null or an array of strings.96 * When setting `plugins: 'my-plugin'` we get 2 type errors, because it isn't an array AND it isn't `null`.97 * @param typeErrors The type errors to merge by path98 */99function mergeTypeErrorsByPath(typeErrors: ErrorObject[]): ErrorObject[] {100 const typeErrorsByPath = groupby(typeErrors, (error) => error.instancePath);101 return Object.values(typeErrorsByPath).map(mergeTypeErrors);102 function mergeTypeErrors(errors: ErrorObject[]): ErrorObject {103 const params = {104 type: errors.map((error) => error.params.type).join(','),105 };106 return {107 ...errors[0],108 params,109 };110 }111}112/**113 * Converts the AJV error object to a human readable error.114 * @param error The error to describe115 */116function describeError(error: ErrorObject): string {117 const errorPrefix = `Config option "${error.instancePath.substr(1)}"`;118 switch (error.keyword) {119 case 'type':120 const expectedTypeDescription = error.params.type.split(',').join(' or ');121 return `${errorPrefix} has the wrong type. It should be a ${expectedTypeDescription}, but was a ${jsonSchemaType(error.data)}.`;122 case 'enum':123 return `${errorPrefix} should be one of the allowed values (${error.params.allowedValues.map(stringify).join(', ')}), but was ${stringify(124 error.data125 )}.`;126 case 'minimum':127 case 'maximum':128 return `${errorPrefix} ${error.message}, was ${error.data}.`;129 default:130 return `${errorPrefix} ${error.message!.replace(/'/g, '"')}`;131 }132}133/**134 * Returns the JSON schema name of the type. JSON schema types are slightly different from actual JS types.135 * @see https://json-schema.org/understanding-json-schema/reference/type.html136 * @param value The value of which it's type should be known137 */138function jsonSchemaType(value: unknown): string {139 if (value === null) {140 return 'null';141 }142 if (value === undefined) {143 return 'undefined';144 }145 if (Array.isArray(value)) {146 return 'array';147 }148 return typeof value;149}150function stringify(value: unknown): string {151 if (typeof value === 'number' && isNaN(value)) {152 return 'NaN';153 } else {154 return JSON.stringify(value);155 }...
Using AI Code Generation
1import { typeErrorsMerged } from 'stryker-parent';2typeErrorsMerged();3import { typeErrorsMerged } from 'stryker-parent';4typeErrorsMerged();5import { typeErrorsMerged } from 'stryker-parent';6typeErrorsMerged();7import { typeErrorsMerged } from 'stryker-parent';8typeErrorsMerged();9import { typeErrorsMerged } from 'stryker-parent';10typeErrorsMerged();11import { typeErrorsMerged } from 'stryker-parent';12typeErrorsMerged();13import { typeErrorsMerged } from 'stryker-parent';14typeErrorsMerged();15import { typeErrorsMerged } from 'stryker-parent';16typeErrorsMerged();17import { typeErrorsMerged } from 'stryker-parent';18typeErrorsMerged();19import { typeErrorsMerged } from 'stryker-parent';20typeErrorsMerged();21import { typeErrorsMerged } from 'stryker-parent';22typeErrorsMerged();23import { typeErrorsMerged } from 'stryker-parent';24typeErrorsMerged();25import { typeErrorsMerged } from 'stryker-parent';26typeErrorsMerged();
Using AI Code Generation
1import { typeErrorsMerged } from 'stryker-parent';2import { typeErrors } from 'stryker-child';3const mergedErrors = typeErrorsMerged();4const errors = typeErrors();5console.log(mergedErrors);6console.log(errors);7import { typeErrorsMerged } from 'stryker-parent';8import { typeErrors } from 'stryker-child';9const mergedErrors = typeErrorsMerged();10const errors = typeErrors();11console.log(mergedErrors);12console.log(errors);13import { typeErrorsMerged } from 'stryker-parent';14import { typeErrors } from 'stryker-child';15const mergedErrors = typeErrorsMerged();16const errors = typeErrors();17console.log(mergedErrors);18console.log(errors);19import { typeErrorsMerged } from 'stryker-parent';20import { typeErrors } from 'stryker-child';21const mergedErrors = typeErrorsMerged();
Using AI Code Generation
1const typeErrorsMerged = require('stryker-parent').typeErrorsMerged;2const assert = require('assert');3describe('typeErrorsMerged', () => {4 it('should merge typeErrors', () => {5 {6 at Object.<anonymous> (C:\\Users\\User\\Desktop\\stryker-parent\\test\\test.js:9:13)7 at Module._compile (module.js:570:32)8 at Object.Module._extensions..js (module.js:579:10)9 at Module.load (module.js:487:32)10 at tryModuleLoad (module.js:446:12)11 at Function.Module._load (module.js:438:3)12 at Function.Module.runMain (module.js:604:10)13 at startup (bootstrap_node.js:158:16)14 },15 {16 at Object.<anonymous> (C:\\Users\\User\\Desktop\\stryker-parent\\test\\test.js:9:13)17 at Module._compile (module.js:570:32)18 at Object.Module._extensions..js (module.js:579:10)19 at Module.load (module.js:487:32)20 at tryModuleLoad (module.js:446:12)21 at Function.Module._load (module.js:438:3)22 at Function.Module.runMain (module.js:604:10)23 at startup (bootstrap_node.js:158:16)24 }25 ];26 {27 at Object.<anonymous> (C:\\Users\\User\\Desktop\\stryker-parent\\test\\test.js:9:13)28 at Module._compile (module.js:570:32)29 at Object.Module._extensions..js (module.js:579:10)30 at Module.load (module.js:
Using AI Code Generation
1const typeErrorsMerged = require('stryker-parent').typeErrorsMerged;2const assert = require('assert');3describe('typeErrorsMerged', () => {4 it('should merge two type errors', () => {5 const actual = typeErrorsMerged(new TypeError('foo'), new TypeError('bar'));6 assert.equal(actual.message, 'foo, bar');7 });8});9const typeErrorsMerged = require('stryker-parent').typeErrorsMerged;10const assert = require('assert');11describe('typeErrorsMerged', () => {12 it('should merge two type errors', () => {13 const actual = typeErrorsMerged(new TypeError('foo'), new TypeError('bar'));14 assert.equal(actual.message, 'foo, bar');15 });16});17const typeErrorsMerged = require('stryker-parent').typeErrorsMerged;18const assert = require('assert');19describe('typeErrorsMerged', () => {20 it('should merge two type errors', () => {21 const actual = typeErrorsMerged(new TypeError('foo'), new TypeError('bar'));22 assert.equal(actual.message, 'foo, bar');23 });24});25const typeErrorsMerged = require('stryker-parent').typeErrorsMerged;26const assert = require('assert');27describe('typeErrorsMerged', () => {28 it('should merge two type errors', () => {29 const actual = typeErrorsMerged(new TypeError('foo'), new TypeError('bar'));30 assert.equal(actual.message, 'foo, bar');31 });32});33const typeErrorsMerged = require('stryker-parent').typeErrorsMerged;34const assert = require('assert');35describe('typeErrorsMerged', () => {36 it('should merge two type errors', () => {37 const actual = typeErrorsMerged(new TypeError('foo'), new TypeError('bar'));38 assert.equal(actual.message, 'foo, bar');39 });40});
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!!