UNPKG

expect-webdriverio

Version:

WebdriverIO Assertion Library

81 lines (79 loc) 2.91 kB
import { printDiffOrStringify, printExpected, printReceived } from 'jest-matcher-utils'; import { equals } from '../jasmineUtils.js'; import { isElementArray } from './elementsUtil.js'; export const getSelector = (el) => { let result = typeof el.selector === 'string' ? el.selector : '<fn>'; if (Array.isArray(el) && el.props.length > 0) { result += ', <props>'; } return result; }; export const getSelectors = (el) => { const selectors = []; let parent; if (isElementArray(el)) { selectors.push(`${(el).foundWith}(\`${getSelector(el)}\`)`); parent = el.parent; } else if (!Array.isArray(el)) { parent = el; } while (parent && 'selector' in parent) { const selector = getSelector(parent); const index = parent.index ? `[${parent.index}]` : ''; selectors.push(`${parent.index ? '$' : ''}$(\`${selector}\`)${index}`); parent = parent.parent; } return selectors.reverse().join('.'); }; const not = (isNot) => `${isNot ? 'not ' : ''}`; export const enhanceError = (subject, expected, actual, context, verb, expectation, expectedValueArgument2 = '', { message = '', containing = false } = {}) => { const { isNot = false, useNotInLabel = true } = context; subject = typeof subject === 'string' ? subject : getSelectors(subject); let contain = ''; if (containing) { contain = ' containing'; } if (verb) { verb += ' '; } const label = { expected: isNot && useNotInLabel ? 'Expected [not]' : 'Expected', received: isNot && useNotInLabel ? 'Received ' : 'Received' }; const diffString = equals(actual, expected) ? `\ ${label.expected}: ${printExpected(expected)} ${label.received}: ${printReceived(actual)}` : printDiffOrStringify(expected, actual, label.expected, label.received, true); if (message) { message += '\n'; } if (expectedValueArgument2) { expectedValueArgument2 = ` ${expectedValueArgument2}`; } const msg = `\ ${message}Expect ${subject} ${not(isNot)}to ${verb}${expectation}${expectedValueArgument2}${contain} ${diffString}`; return msg; }; export const enhanceErrorBe = (subject, context, options) => { const { isNot, verb, expectation } = context; const expected = `${not(isNot)}${expectation}`; const actual = `${not(!isNot)}${expectation}`; return enhanceError(subject, expected, actual, { ...context, useNotInLabel: false }, verb, expectation, '', options); }; export const numberError = (options = {}) => { if (typeof options.eq === 'number') { return options.eq; } if (options.gte && options.lte) { return `>= ${options.gte} && <= ${options.lte}`; } if (options.gte) { return `>= ${options.gte}`; } if (options.lte) { return ` <= ${options.lte}`; } return 'no params'; };