How to use printBasicValue method in Jest

Best JavaScript code snippet using jest

index.js

Source:index.js Github

copy

Full Screen

...81/**82 * The first port of call for printing an object, handles most of the83 * data-types in JS.84 */85function printBasicValue(val, printFunctionName, escapeRegex, escapeString) {86 if (val === true || val === false) {87 return '' + val;88 }89 if (val === undefined) {90 return 'undefined';91 }92 if (val === null) {93 return 'null';94 }95 const typeOf = typeof val;96 if (typeOf === 'number') {97 return printNumber(val);98 }99 if (typeOf === 'bigint') {100 return printBigInt(val);101 }102 if (typeOf === 'string') {103 if (escapeString) {104 return '"' + val.replace(/"|\\/g, '\\$&') + '"';105 }106 return '"' + val + '"';107 }108 if (typeOf === 'function') {109 return printFunction(val, printFunctionName);110 }111 if (typeOf === 'symbol') {112 return printSymbol(val);113 }114 const toStringed = toString.call(val);115 if (toStringed === '[object WeakMap]') {116 return 'WeakMap {}';117 }118 if (toStringed === '[object WeakSet]') {119 return 'WeakSet {}';120 }121 if (122 toStringed === '[object Function]' ||123 toStringed === '[object GeneratorFunction]'124 ) {125 return printFunction(val, printFunctionName);126 }127 if (toStringed === '[object Symbol]') {128 return printSymbol(val);129 }130 if (toStringed === '[object Date]') {131 return isNaN(+val) ? 'Date { NaN }' : toISOString.call(val);132 }133 if (toStringed === '[object Error]') {134 return printError(val);135 }136 if (toStringed === '[object RegExp]') {137 if (escapeRegex) {138 // https://github.com/benjamingr/RegExp.escape/blob/master/polyfill.js139 return regExpToString.call(val).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');140 }141 return regExpToString.call(val);142 }143 if (val instanceof Error) {144 return printError(val);145 }146 return null;147}148/**149 * Handles more complex objects ( such as objects with circular references.150 * maps and sets etc )151 */152function printComplexValue(153 val,154 config,155 indentation,156 depth,157 refs,158 hasCalledToJSON159) {160 if (refs.indexOf(val) !== -1) {161 return '[Circular]';162 }163 refs = refs.slice();164 refs.push(val);165 const hitMaxDepth = ++depth > config.maxDepth;166 const min = config.min;167 if (168 config.callToJSON &&169 !hitMaxDepth &&170 val.toJSON &&171 typeof val.toJSON === 'function' &&172 !hasCalledToJSON173 ) {174 return printer(val.toJSON(), config, indentation, depth, refs, true);175 }176 const toStringed = toString.call(val);177 if (toStringed === '[object Arguments]') {178 return hitMaxDepth179 ? '[Arguments]'180 : (min ? '' : 'Arguments ') +181 '[' +182 (0, _collections.printListItems)(183 val,184 config,185 indentation,186 depth,187 refs,188 printer189 ) +190 ']';191 }192 if (isToStringedArrayType(toStringed)) {193 return hitMaxDepth194 ? '[' + val.constructor.name + ']'195 : (min ? '' : val.constructor.name + ' ') +196 '[' +197 (0, _collections.printListItems)(198 val,199 config,200 indentation,201 depth,202 refs,203 printer204 ) +205 ']';206 }207 if (toStringed === '[object Map]') {208 return hitMaxDepth209 ? '[Map]'210 : 'Map {' +211 (0, _collections.printIteratorEntries)(212 val.entries(),213 config,214 indentation,215 depth,216 refs,217 printer,218 ' => '219 ) +220 '}';221 }222 if (toStringed === '[object Set]') {223 return hitMaxDepth224 ? '[Set]'225 : 'Set {' +226 (0, _collections.printIteratorValues)(227 val.values(),228 config,229 indentation,230 depth,231 refs,232 printer233 ) +234 '}';235 } // Avoid failure to serialize global window object in jsdom test environment.236 // For example, not even relevant if window is prop of React element.237 return hitMaxDepth || isWindow(val)238 ? '[' + getConstructorName(val) + ']'239 : (min ? '' : getConstructorName(val) + ' ') +240 '{' +241 (0, _collections.printObjectProperties)(242 val,243 config,244 indentation,245 depth,246 refs,247 printer248 ) +249 '}';250}251function isNewPlugin(plugin) {252 return plugin.serialize != null;253}254function printPlugin(plugin, val, config, indentation, depth, refs) {255 let printed;256 try {257 printed = isNewPlugin(plugin)258 ? plugin.serialize(val, config, indentation, depth, refs, printer)259 : plugin.print(260 val,261 valChild => printer(valChild, config, indentation, depth, refs),262 str => {263 const indentationNext = indentation + config.indent;264 return (265 indentationNext +266 str.replace(NEWLINE_REGEXP, '\n' + indentationNext)267 );268 },269 {270 edgeSpacing: config.spacingOuter,271 min: config.min,272 spacing: config.spacingInner273 },274 config.colors275 );276 } catch (error) {277 throw new PrettyFormatPluginError(error.message, error.stack);278 }279 if (typeof printed !== 'string') {280 throw new Error(281 `pretty-format: Plugin must return type "string" but instead returned "${typeof printed}".`282 );283 }284 return printed;285}286function findPlugin(plugins, val) {287 for (let p = 0; p < plugins.length; p++) {288 try {289 if (plugins[p].test(val)) {290 return plugins[p];291 }292 } catch (error) {293 throw new PrettyFormatPluginError(error.message, error.stack);294 }295 }296 return null;297}298function printer(val, config, indentation, depth, refs, hasCalledToJSON) {299 const plugin = findPlugin(config.plugins, val);300 if (plugin !== null) {301 return printPlugin(plugin, val, config, indentation, depth, refs);302 }303 const basicResult = printBasicValue(304 val,305 config.printFunctionName,306 config.escapeRegex,307 config.escapeString308 );309 if (basicResult !== null) {310 return basicResult;311 }312 return printComplexValue(313 val,314 config,315 indentation,316 depth,317 refs,318 hasCalledToJSON319 );320}321const DEFAULT_THEME = {322 comment: 'gray',323 content: 'reset',324 prop: 'yellow',325 tag: 'cyan',326 value: 'green'327};328const DEFAULT_THEME_KEYS = Object.keys(DEFAULT_THEME);329const DEFAULT_OPTIONS = {330 callToJSON: true,331 escapeRegex: false,332 escapeString: true,333 highlight: false,334 indent: 2,335 maxDepth: Infinity,336 min: false,337 plugins: [],338 printFunctionName: true,339 theme: DEFAULT_THEME340};341function validateOptions(options) {342 Object.keys(options).forEach(key => {343 if (!DEFAULT_OPTIONS.hasOwnProperty(key)) {344 throw new Error(`pretty-format: Unknown option "${key}".`);345 }346 });347 if (options.min && options.indent !== undefined && options.indent !== 0) {348 throw new Error(349 'pretty-format: Options "min" and "indent" cannot be used together.'350 );351 }352 if (options.theme !== undefined) {353 if (options.theme === null) {354 throw new Error(`pretty-format: Option "theme" must not be null.`);355 }356 if (typeof options.theme !== 'object') {357 throw new Error(358 `pretty-format: Option "theme" must be of type "object" but instead received "${typeof options.theme}".`359 );360 }361 }362}363const getColorsHighlight = options =>364 DEFAULT_THEME_KEYS.reduce((colors, key) => {365 const value =366 options.theme && options.theme[key] !== undefined367 ? options.theme[key]368 : DEFAULT_THEME[key];369 const color = value && _ansiStyles.default[value];370 if (371 color &&372 typeof color.close === 'string' &&373 typeof color.open === 'string'374 ) {375 colors[key] = color;376 } else {377 throw new Error(378 `pretty-format: Option "theme" has a key "${key}" whose value "${value}" is undefined in ansi-styles.`379 );380 }381 return colors;382 }, Object.create(null));383const getColorsEmpty = () =>384 DEFAULT_THEME_KEYS.reduce((colors, key) => {385 colors[key] = {386 close: '',387 open: ''388 };389 return colors;390 }, Object.create(null));391const getPrintFunctionName = options =>392 options && options.printFunctionName !== undefined393 ? options.printFunctionName394 : DEFAULT_OPTIONS.printFunctionName;395const getEscapeRegex = options =>396 options && options.escapeRegex !== undefined397 ? options.escapeRegex398 : DEFAULT_OPTIONS.escapeRegex;399const getEscapeString = options =>400 options && options.escapeString !== undefined401 ? options.escapeString402 : DEFAULT_OPTIONS.escapeString;403const getConfig = options => ({404 callToJSON:405 options && options.callToJSON !== undefined406 ? options.callToJSON407 : DEFAULT_OPTIONS.callToJSON,408 colors:409 options && options.highlight410 ? getColorsHighlight(options)411 : getColorsEmpty(),412 escapeRegex: getEscapeRegex(options),413 escapeString: getEscapeString(options),414 indent:415 options && options.min416 ? ''417 : createIndent(418 options && options.indent !== undefined419 ? options.indent420 : DEFAULT_OPTIONS.indent421 ),422 maxDepth:423 options && options.maxDepth !== undefined424 ? options.maxDepth425 : DEFAULT_OPTIONS.maxDepth,426 min: options && options.min !== undefined ? options.min : DEFAULT_OPTIONS.min,427 plugins:428 options && options.plugins !== undefined429 ? options.plugins430 : DEFAULT_OPTIONS.plugins,431 printFunctionName: getPrintFunctionName(options),432 spacingInner: options && options.min ? ' ' : '\n',433 spacingOuter: options && options.min ? '' : '\n'434});435function createIndent(indent) {436 return new Array(indent + 1).join(' ');437}438/**439 * Returns a presentation string of your `val` object440 * @param val any potential JavaScript object441 * @param options Custom settings442 */443function prettyFormat(val, options) {444 if (options) {445 validateOptions(options);446 if (options.plugins) {447 const plugin = findPlugin(options.plugins, val);448 if (plugin !== null) {449 return printPlugin(plugin, val, getConfig(options), '', 0, []);450 }451 }452 }453 const basicResult = printBasicValue(454 val,455 getPrintFunctionName(options),456 getEscapeRegex(options),457 getEscapeString(options)458 );459 if (basicResult !== null) {460 return basicResult;461 }462 return printComplexValue(val, getConfig(options), '', 0, []);463}464prettyFormat.plugins = {465 AsymmetricMatcher: _AsymmetricMatcher.default,466 ConvertAnsi: _ConvertAnsi.default,467 DOMCollection: _DOMCollection.default,...

Full Screen

Full Screen

format.js

Source:format.js Github

copy

Full Screen

...58/**59 * The first port of call for printing an object, handles most of the60 * data-types in JS.61 */62function printBasicValue(63// eslint-disable-next-line @typescript-eslint/no-explicit-any64val, { printFunctionName, escapeRegex, escapeString }) {65 if (val === true || val === false) {66 return String(val);67 }68 if (val === undefined) {69 return "undefined";70 }71 if (val === null) {72 return "null";73 }74 const typeOf = typeof val;75 if (typeOf === "number") {76 return printNumber(val);77 }78 if (typeOf === "string") {79 if (escapeString) {80 return `"${val.replace(/"|\\/g, "\\$&")}"`;81 }82 return `"${val}"`;83 }84 if (typeOf === "function") {85 return printFunction(val, printFunctionName);86 }87 if (typeOf === "symbol") {88 return printSymbol(val);89 }90 const toStringed = toString.call(val);91 if (toStringed === "[object WeakMap]") {92 return "WeakMap {}";93 }94 if (toStringed === "[object WeakSet]") {95 return "WeakSet {}";96 }97 if (toStringed === "[object Function]" ||98 toStringed === "[object GeneratorFunction]") {99 return printFunction(val, printFunctionName);100 }101 if (toStringed === "[object Symbol]") {102 return printSymbol(val);103 }104 if (toStringed === "[object Date]") {105 return isNaN(+val) ? "Date { NaN }" : toISOString.call(val);106 }107 if (toStringed === "[object Error]") {108 return printError(val);109 }110 if (toStringed === "[object RegExp]") {111 if (escapeRegex) {112 // https://github.com/benjamingr/RegExp.escape/blob/master/polyfill.js113 return regExpToString.call(val).replace(/[\\^$*+?.()|[\]{}]/g, "\\$&");114 }115 return regExpToString.call(val);116 }117 if (val instanceof Error) {118 return printError(val);119 }120 return null;121}122function printer(123// eslint-disable-next-line @typescript-eslint/no-explicit-any124val, config, indentation, depth, refs, hasCalledToJSON) {125 const basicResult = printBasicValue(val, config);126 if (basicResult !== null) {127 return basicResult;128 }129 // eslint-disable-next-line @typescript-eslint/no-use-before-define130 return printComplexValue(val, config, indentation, depth, refs, hasCalledToJSON);131}132/**133 * Return items (for example, of an array)134 * with spacing, indentation, and comma135 * without surrounding punctuation (for example, brackets)136 */137function printListItems(138// eslint-disable-next-line @typescript-eslint/no-explicit-any139list, config, indentation, depth, refs, printer) {140 let result = "";141 if (list.length) {142 result += config.spacingOuter;143 const indentationNext = indentation + config.indent;144 for (let i = 0; i < list.length; i++) {145 result +=146 indentationNext +147 printer(list[i], config, indentationNext, depth, refs);148 if (i < list.length - 1) {149 result += "," + config.spacingInner;150 }151 else if (!config.min) {152 result += ",";153 }154 }155 result += config.spacingOuter + indentation;156 }157 return result;158}159/**160 * Return entries (for example, of a map)161 * with spacing, indentation, and comma162 * without surrounding punctuation (for example, braces)163 */164function printIteratorEntries(165// eslint-disable-next-line @typescript-eslint/no-explicit-any166iterator, config, indentation, depth, refs, printer, 167// Too bad, so sad that separator for ECMAScript Map has been ' => '168// What a distracting diff if you change a data structure to/from169// ECMAScript Object or Immutable.Map/OrderedMap which use the default.170separator = ": ") {171 let result = "";172 let current = iterator.next();173 if (!current.done) {174 result += config.spacingOuter;175 const indentationNext = indentation + config.indent;176 while (!current.done) {177 const name = printer(current.value[0], config, indentationNext, depth, refs);178 const value = printer(current.value[1], config, indentationNext, depth, refs);179 result += indentationNext + name + separator + value;180 current = iterator.next();181 if (!current.done) {182 result += "," + config.spacingInner;183 }184 else if (!config.min) {185 result += ",";186 }187 }188 result += config.spacingOuter + indentation;189 }190 return result;191}192/**193 * Return values (for example, of a set)194 * with spacing, indentation, and comma195 * without surrounding punctuation (braces or brackets)196 */197function printIteratorValues(198// eslint-disable-next-line @typescript-eslint/no-explicit-any199iterator, config, indentation, depth, refs, printer) {200 let result = "";201 let current = iterator.next();202 if (!current.done) {203 result += config.spacingOuter;204 const indentationNext = indentation + config.indent;205 while (!current.done) {206 result +=207 indentationNext +208 printer(current.value, config, indentationNext, depth, refs);209 current = iterator.next();210 if (!current.done) {211 result += "," + config.spacingInner;212 }213 else if (!config.min) {214 result += ",";215 }216 }217 result += config.spacingOuter + indentation;218 }219 return result;220}221const getKeysOfEnumerableProperties = (object) => {222 const keys = Object.keys(object).sort();223 if (Object.getOwnPropertySymbols) {224 Object.getOwnPropertySymbols(object).forEach((symbol) => {225 const d = Object.getOwnPropertyDescriptor(object, symbol);226 assert(d != null);227 if (d.enumerable) {228 keys.push(symbol);229 }230 });231 }232 return keys;233};234/**235 * Return properties of an object236 * with spacing, indentation, and comma237 * without surrounding punctuation (for example, braces)238 */239function printObjectProperties(val, config, indentation, depth, refs, printer) {240 let result = "";241 const keys = getKeysOfEnumerableProperties(val);242 if (keys.length) {243 result += config.spacingOuter;244 const indentationNext = indentation + config.indent;245 for (let i = 0; i < keys.length; i++) {246 const key = keys[i];247 const name = printer(key, config, indentationNext, depth, refs);248 const value = printer(val[key], config, indentationNext, depth, refs);249 result += indentationNext + name + ": " + value;250 if (i < keys.length - 1) {251 result += "," + config.spacingInner;252 }253 else if (!config.min) {254 result += ",";255 }256 }257 result += config.spacingOuter + indentation;258 }259 return result;260}261/**262 * Handles more complex objects ( such as objects with circular references.263 * maps and sets etc )264 */265function printComplexValue(266// eslint-disable-next-line @typescript-eslint/no-explicit-any267val, config, indentation, depth, refs, hasCalledToJSON) {268 if (refs.indexOf(val) !== -1) {269 return "[Circular]";270 }271 refs = refs.slice();272 refs.push(val);273 const hitMaxDepth = ++depth > config.maxDepth;274 const { min, callToJSON } = config;275 if (callToJSON &&276 !hitMaxDepth &&277 val.toJSON &&278 typeof val.toJSON === "function" &&279 !hasCalledToJSON) {280 return printer(val.toJSON(), config, indentation, depth, refs, true);281 }282 const toStringed = toString.call(val);283 if (toStringed === "[object Arguments]") {284 return hitMaxDepth285 ? "[Arguments]"286 : (min ? "" : "Arguments ") +287 "[" +288 printListItems(val, config, indentation, depth, refs, printer) +289 "]";290 }291 if (isToStringedArrayType(toStringed)) {292 return hitMaxDepth293 ? `[${val.constructor.name}]`294 : (min ? "" : `${val.constructor.name} `) +295 "[" +296 printListItems(val, config, indentation, depth, refs, printer) +297 "]";298 }299 if (toStringed === "[object Map]") {300 return hitMaxDepth301 ? "[Map]"302 : "Map {" +303 printIteratorEntries(val.entries(), config, indentation, depth, refs, printer, " => ") +304 "}";305 }306 if (toStringed === "[object Set]") {307 return hitMaxDepth308 ? "[Set]"309 : "Set {" +310 printIteratorValues(val.values(), config, indentation, depth, refs, printer) +311 "}";312 }313 // Avoid failure to serialize global window object in jsdom test environment.314 // For example, not even relevant if window is prop of React element.315 return hitMaxDepth || isWindow(val)316 ? "[" + getConstructorName(val) + "]"317 : (min ? "" : getConstructorName(val) + " ") +318 "{" +319 printObjectProperties(val, config, indentation, depth, refs, printer) +320 "}";321}322// TODO this is better done with `.padStart()`323function createIndent(indent) {324 return new Array(indent + 1).join(" ");325}326const getConfig = (options) => ({327 ...options,328 indent: options.min ? "" : createIndent(options.indent),329 spacingInner: options.min ? " " : "\n",330 spacingOuter: options.min ? "" : "\n"331});332/**333 * Returns a presentation string of your `val` object334 * @param val any potential JavaScript object335 * @param options Custom settings336 */337// eslint-disable-next-line @typescript-eslint/no-explicit-any338export function format(val, options = {}) {339 const opts = {340 ...DEFAULT_OPTIONS,341 ...options342 };343 const basicResult = printBasicValue(val, opts);344 if (basicResult !== null) {345 return basicResult;346 }347 return printComplexValue(val, getConfig(opts), "", 0, []);...

Full Screen

Full Screen

pretty-format.js

Source:pretty-format.js Github

copy

Full Screen

...34 }35 function printError(val) {36 return '[' + errorToString.call(val) + ']';37 }38 function printBasicValue(val) {39 if (val === true || val === false) return '' + val;40 if (val === undefined) return 'undefined';41 if (val === null) return 'null';42 var typeOf = typeof val;43 if (typeOf === 'number') return printNumber(val);44 if (typeOf === 'string') return '"' + printString(val) + '"';45 if (typeOf === 'function') return printFunction(val);46 if (typeOf === 'symbol') return printSymbol(val);47 var toStringed = toString.call(val);48 if (toStringed === '[object WeakMap]') return 'WeakMap {}';49 if (toStringed === '[object WeakSet]') return 'WeakSet {}';50 if (toStringed === '[object Function]' || toStringed === '[object GeneratorFunction]') return printFunction(val);51 if (toStringed === '[object Symbol]') return printSymbol(val);52 if (toStringed === '[object Date]') return toISOString.call(val);53 if (toStringed === '[object Error]') return printError(val);54 if (toStringed === '[object RegExp]') return regExpToString.call(val);55 if (toStringed === '[object Arguments]' && val.length === 0) return 'Arguments []';56 if (isToStringedArrayType(toStringed) && val.length === 0) return val.constructor.name + ' []';57 if (val instanceof Error) return printError(val);58 return false;59 }60 function printList(list, indent, prevIndent, refs, maxDepth, currentDepth, plugins) {61 var body = '';62 if (list.length) {63 body += '\n';64 var innerIndent = prevIndent + indent;65 for (var i = 0; i < list.length; i++) {66 body += innerIndent + print(list[i], indent, innerIndent, refs, maxDepth, currentDepth, plugins);67 if (i < list.length - 1) {68 body += ',\n';69 }70 }71 body += '\n' + prevIndent;72 }73 return '[' + body + ']';74 }75 function printArguments(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) {76 return 'Arguments ' + printList(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);77 }78 function printArray(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) {79 return val.constructor.name + ' ' + printList(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);80 }81 function printMap(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) {82 var result = 'Map {';83 var iterator = val.entries();84 var current = iterator.next();85 if (!current.done) {86 result += '\n';87 var innerIndent = prevIndent + indent;88 while (!current.done) {89 var key = print(current.value[0], indent, innerIndent, refs, maxDepth, currentDepth, plugins);90 var value = print(current.value[1], indent, innerIndent, refs, maxDepth, currentDepth, plugins);91 result += innerIndent + key + ' => ' + value;92 current = iterator.next();93 if (!current.done) {94 result += ',\n';95 }96 }97 result += '\n' + prevIndent;98 }99 return result + '}';100 }101 function printObject(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) {102 var constructor = val.constructor ? val.constructor.name + ' ' : 'Object ';103 var result = constructor + '{';104 var keys = Object.keys(val).sort();105 var symbols = getSymbols(val);106 if (symbols.length) {107 keys = keys.filter(function (key) {108 return !(typeof key === 'symbol' || toString.call(key) === '[object Symbol]');109 }).concat(symbols);110 }111 if (keys.length) {112 result += '\n';113 var innerIndent = prevIndent + indent;114 for (var i = 0; i < keys.length; i++) {115 var key = keys[i];116 var name = print(key, indent, innerIndent, refs, maxDepth, currentDepth, plugins);117 var value = print(val[key], indent, innerIndent, refs, maxDepth, currentDepth, plugins);118 result += innerIndent + name + ': ' + value;119 if (i < keys.length - 1) {120 result += ',\n';121 }122 }123 result += '\n' + prevIndent;124 }125 return result + '}';126 }127 function printSet(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) {128 var result = 'Set {';129 var iterator = val.entries();130 var current = iterator.next();131 if (!current.done) {132 result += '\n';133 var innerIndent = prevIndent + indent;134 while (!current.done) {135 result += innerIndent + print(current.value[1], indent, innerIndent, refs, maxDepth, currentDepth, plugins);136 current = iterator.next();137 if (!current.done) {138 result += ',\n';139 }140 }141 result += '\n' + prevIndent;142 }143 return result + '}';144 }145 function printComplexValue(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) {146 refs = refs.slice();147 if (refs.indexOf(val) > -1) {148 return '[Circular]';149 } else {150 refs.push(val);151 }152 currentDepth++;153 var hitMaxDepth = currentDepth > maxDepth;154 if (!hitMaxDepth && val.toJSON && typeof val.toJSON === 'function') {155 return print(val.toJSON(), indent, prevIndent, refs, maxDepth, currentDepth, plugins);156 }157 var toStringed = toString.call(val);158 if (toStringed === '[object Arguments]') {159 return hitMaxDepth ? '[Arguments]' : printArguments(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);160 } else if (isToStringedArrayType(toStringed)) {161 return hitMaxDepth ? '[Array]' : printArray(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);162 } else if (toStringed === '[object Map]') {163 return hitMaxDepth ? '[Map]' : printMap(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);164 } else if (toStringed === '[object Set]') {165 return hitMaxDepth ? '[Set]' : printSet(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);166 } else if (typeof val === 'object') {167 return hitMaxDepth ? '[Object]' : printObject(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);168 }169 }170 function printPlugin(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) {171 var match = false;172 var plugin = void 0;173 for (var p = 0; p < plugins.length; p++) {174 plugin = plugins[p];175 if (plugin.test(val)) {176 match = true;177 break;178 }179 }180 if (!match) {181 return false;182 }183 function boundPrint(val) {184 return print(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);185 }186 function boundIndent(str) {187 var indentation = prevIndent + indent;188 return indentation + str.replace(NEWLINE_REGEXP, '\n' + indentation);189 }190 return plugin.print(val, boundPrint, boundIndent);191 }192 function print(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins) {193 var basic = printBasicValue(val);194 if (basic) return basic;195 var plugin = printPlugin(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);196 if (plugin) return plugin;197 return printComplexValue(val, indent, prevIndent, refs, maxDepth, currentDepth, plugins);198 }199 var DEFAULTS = {200 indent: 2,201 maxDepth: Infinity,202 plugins: []203 };204 function validateOptions(opts) {205 Object.keys(opts).forEach(function (key) {206 if (!DEFAULTS.hasOwnProperty(key)) {207 throw new Error('prettyFormat: Invalid option: ' + key);208 }209 });210 }211 function normalizeOptions(opts) {212 var result = {};213 Object.keys(DEFAULTS).forEach(function (key) {214 return result[key] = opts.hasOwnProperty(key) ? opts[key] : DEFAULTS[key];215 });216 return result;217 }218 function createIndent(indent) {219 return new Array(indent + 1).join(' ');220 }221 function prettyFormat(val, opts) {222 if (!opts) {223 opts = DEFAULTS;224 } else {225 validateOptions(opts);226 opts = normalizeOptions(opts);227 }228 var indent = void 0;229 var refs = void 0;230 var prevIndent = '';231 var currentDepth = 0;232 if (opts && opts.plugins.length) {233 indent = createIndent(opts.indent);234 refs = [];235 var pluginsResult = printPlugin(val, indent, prevIndent, refs, opts.maxDepth, currentDepth, opts.plugins);236 if (pluginsResult) return pluginsResult;237 }238 var basicResult = printBasicValue(val);239 if (basicResult) return basicResult;240 if (!indent) indent = createIndent(opts.indent);241 if (!refs) refs = [];242 return printComplexValue(val, indent, prevIndent, refs, opts.maxDepth, currentDepth, opts.plugins);243 }244 window.prettyFormat = prettyFormat;...

Full Screen

Full Screen

Jest Testing Tutorial

LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.

Chapters

  1. What is Jest Framework
  2. Advantages of Jest - Jest has 3,898,000 GitHub repositories, as mentioned on its official website. Learn what makes Jest special and why Jest has gained popularity among the testing and developer community.
  3. Jest Installation - All the prerequisites and set up steps needed to help you start Jest automation testing.
  4. Using Jest with NodeJS Project - Learn how to leverage Jest framework to automate testing using a NodeJS Project.
  5. Writing First Test for Jest Framework - Get started with code-based tutorial to help you write and execute your first Jest framework testing script.
  6. Jest Vocabulary - Learn the industry renowned and official jargons of the Jest framework by digging deep into the Jest vocabulary.
  7. Unit Testing with Jest - Step-by-step tutorial to help you execute unit testing with Jest framework.
  8. Jest Basics - Learn about the most pivotal and basic features which makes Jest special.
  9. Jest Parameterized Tests - Avoid code duplication and fasten automation testing with Jest using parameterized tests. Parameterization allows you to trigger the same test scenario over different test configurations by incorporating parameters.
  10. Jest Matchers - Enforce assertions better with the help of matchers. Matchers help you compare the actual output with the expected one. Here is an example to see if the object is acquired from the correct class or not. -

|<p>it('check_object_of_Car', () => {</p><p> expect(newCar()).toBeInstanceOf(Car);</p><p> });</p>| | :- |

  1. Jest Hooks: Setup and Teardown - Learn how to set up conditions which needs to be followed by the test execution and incorporate a tear down function to free resources after the execution is complete.
  2. Jest Code Coverage - Unsure there is no code left unchecked in your application. Jest gives a specific flag called --coverage to help you generate code coverage.
  3. HTML Report Generation - Learn how to create a comprehensive HTML report based on your Jest test execution.
  4. Testing React app using Jest Framework - Learn how to test your react web-application with Jest framework in this detailed Jest tutorial.
  5. Test using LambdaTest cloud Selenium Grid - Run your Jest testing script over LambdaTest cloud-based platform and leverage parallel testing to help trim down your test execution time.
  6. Snapshot Testing for React Front Ends - Capture screenshots of your react based web-application and compare them automatically for visual anomalies with the help of Jest tutorial.
  7. Bonus: Import ES modules with Jest - ES modules are also known as ECMAScript modules. Learn how to best use them by importing in your Jest testing scripts.
  8. Jest vs Mocha vs Jasmine - Learn the key differences between the most popular JavaScript-based testing frameworks i.e. Jest, Mocha, and Jasmine.
  9. Jest FAQs(Frequently Asked Questions) - Explore the most commonly asked questions around Jest framework, with their answers.

Run Jest 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