diff --git a/packages/driver/src/cypress/error_messages.js b/packages/driver/src/cypress/error_messages.js index 1526047fd308..40c481b19ad7 100644 --- a/packages/driver/src/cypress/error_messages.js +++ b/packages/driver/src/cypress/error_messages.js @@ -1,1775 +1,1795 @@ -/* - * decaffeinate suggestions: - * DS102: Remove unnecessary code created because of implicit returns - * DS207: Consider shorter variations of null checks - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md - */ -const _ = require("lodash"); -const capitalize = require('underscore.string/capitalize'); - -const divider = (num, char) => Array(num).join(char); - -const format = function(data) { - switch (false) { - case !_.isString(data): - return _.truncate(data, { length: 2000 }); - case !_.isObject(data): - return JSON.stringify(data, null, 2); - default: - return data; +const _ = require('lodash') +const { stripIndent } = require('common-tags') +const capitalize = require('underscore.string/capitalize') + +const divider = (num, char) => { + return Array(num).join(char) +} + +const format = (data) => { + if (_.isString(data)) { + return _.truncate(data, { length: 2000 }) } -}; -const formatConfigFile = function(configFile) { - if (configFile === false) { - return "`cypress.json` (currently disabled by --config-file=false)"; + if (_.isObject(data)) { + return JSON.stringify(data, null, 2) } - return `\`${format(configFile)}\``; -}; + return data +} -const formatRedirect = redirect => ` - ${redirect}`; +const formatConfigFile = (configFile) => { + if (configFile === false) { + return '`cypress.json` (currently disabled by --config-file=false)' + } -const formatRedirects = (redirects = []) => _.map(redirects, formatRedirect); + return `\`${format(configFile)}\`` +} -const formatProp = function(memo, field) { - const {key, value} = field; +const formatRedirects = (redirects = [], listIndentSize) => { + return _.map(redirects, (redirect) => { + return `${' '.repeat(listIndentSize)} - ${redirect}` + }) +} + +const formatProp = (memo, field) => { + const { key, value } = field if (value != null) { - memo.push(capitalize(key) + ": " + format(value)); + memo.push(`${capitalize(key)}: ${format(value)}`) } - return memo; -}; -const cmd = function(command, args = "") { - const prefix = command.startsWith("Cypress") ? "" : "cy."; + return memo +} + +const cmd = (command, args = '') => { + const prefix = command.startsWith('Cypress') ? '' : 'cy.' - return `\`${prefix}${command}(${args})\``; -}; + return `\`${prefix}${command}(${args})\`` +} -const getScreenshotDocsPath = function(cmd) { - if (cmd === "Cypress.Screenshot.defaults") { return "screenshot-api"; } else { return "screenshot"; } -}; +const getScreenshotDocsPath = (cmd) => { + if (cmd === 'Cypress.Screenshot.defaults') { + return 'screenshot-api' + } -const getRedirects = function(obj, phrase) { - const redirects = obj.redirects != null ? obj.redirects : []; + return 'screenshot' +} - if (!redirects.length) { return ""; } +const getRedirects = (obj, phrase, listIndentSize) => { + const redirects = obj.redirects ?? [] - const word = redirects.length > 1 ? "times" : "time"; + if (!redirects.length) { + return '' + } - const list = formatRedirects(redirects); + const word = redirects.length > 1 ? 'times' : 'time' - return `\ -${phrase} '${redirects.length}' ${word} to: + const list = formatRedirects(redirects, listIndentSize) -${list.join("\n")}\ -`; -}; + return [ + `${phrase} '${redirects.length}' ${word} to:`, + '', + `${list.join('\n')}`, + ].join('\n') +} -const getHttpProps = (fields = []) => _ -.chain(fields) -.reduce(formatProp, []) -.join("\n") -.value(); +const getHttpProps = (fields = []) => { + return _ + .chain(fields) + .reduce(formatProp, []) + .join('\n') + .value() +} + +const cyStripIndent = (str, indentSize) => { + const indent = ' '.repeat(indentSize) + + return str.split('\n').map((s) => { + return s.startsWith(indent) + ? s.substring(indentSize) + : s + }).join('\n') +} module.exports = { add: { - type_missing: "`Cypress.add(key, fn, type)` must include a type!" + type_missing: '`Cypress.add(key, fn, type)` must include a type!', }, agents: { - deprecated_warning: `${cmd('agents')} is deprecated. Use ${cmd('stub')} and ${cmd('spy')} instead.` + deprecated_warning: `${cmd('agents')} is deprecated. Use ${cmd('stub')} and ${cmd('spy')} instead.`, }, alias: { - invalid: "Invalid alias: `{{name}}`.\nYou forgot the `@`. It should be written as: `@{{displayName}}`.", + invalid: 'Invalid alias: `{{name}}`.\nYou forgot the `@`. It should be written as: `@{{displayName}}`.', not_registered_with_available: `${cmd('{{cmd}}')} could not find a registered alias for: \`@{{displayName}}\`.\nAvailable aliases are: \`{{availableAliases}}\`.`, - not_registered_without_available: `${cmd('{{cmd}}')} could not find a registered alias for: \`@{{displayName}}\`.\nYou have not aliased anything yet.` + not_registered_without_available: `${cmd('{{cmd}}')} could not find a registered alias for: \`@{{displayName}}\`.\nYou have not aliased anything yet.`, }, as: { empty_string: { message: `${cmd('as')} cannot be passed an empty string.`, - docsUrl: "https://on.cypress.io/as" + docsUrl: 'https://on.cypress.io/as', }, invalid_type: { message: `${cmd('as')} can only accept a string.`, - docsUrl: "https://on.cypress.io/as" + docsUrl: 'https://on.cypress.io/as', }, invalid_first_token: { - message: "`{{alias}}` cannot be named starting with the `@` symbol. Try renaming the alias to `{{suggestedName}}`, or something else that does not start with the `@` symbol.", - docsUrl: "https://on.cypress.io/as" + message: '`{{alias}}` cannot be named starting with the `@` symbol. Try renaming the alias to `{{suggestedName}}`, or something else that does not start with the `@` symbol.', + docsUrl: 'https://on.cypress.io/as', }, reserved_word: { message: `${cmd('as')} cannot be aliased as: \`{{alias}}\`. This word is reserved.`, - docsUrl: "https://on.cypress.io/as" - } + docsUrl: 'https://on.cypress.io/as', + }, }, blur: { multiple_elements: { message: `${cmd('blur')} can only be called on a single element. Your subject contained {{num}} elements.`, - docsUrl: "https://on.cypress.io/blur" + docsUrl: 'https://on.cypress.io/blur', }, no_focused_element: { message: `${cmd('blur')} can only be called when there is a currently focused element.`, - docsUrl: "https://on.cypress.io/blur" + docsUrl: 'https://on.cypress.io/blur', }, timed_out: { message: `${cmd('blur')} timed out because your browser did not receive any \`blur\` events. This is a known bug in Chrome when it is not the currently focused window.`, - docsUrl: "https://on.cypress.io/blur" + docsUrl: 'https://on.cypress.io/blur', }, wrong_focused_element: { message: `${cmd('blur')} can only be called on the focused element. Currently the focused element is a: \`{{node}}\``, - docsUrl: "https://on.cypress.io/blur" - } + docsUrl: 'https://on.cypress.io/blur', + }, }, browser: { - invalid_arg: "`Cypress.{{method}}()` must be passed the name of a browser or an object to filter with. You passed: `{{obj}}`" + invalid_arg: '`Cypress.{{method}}()` must be passed the name of a browser or an object to filter with. You passed: `{{obj}}`', }, chai: { - length_invalid_argument: "You must provide a valid number to a `length` assertion. You passed: `{{length}}`", - match_invalid_argument: "`match` requires its argument be a `RegExp`. You passed: `{{regExp}}`", - invalid_jquery_obj(obj) { - return `\ -You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery object. + length_invalid_argument: 'You must provide a valid number to a `length` assertion. You passed: `{{length}}`', + match_invalid_argument: '`match` requires its argument be a `RegExp`. You passed: `{{regExp}}`', + invalid_jquery_obj (obj) { + return stripIndent`\ + You attempted to make a chai-jQuery assertion on an object that is neither a DOM object or a jQuery object. -The chai-jQuery assertion you used was: + The chai-jQuery assertion you used was: - > ${obj.assertion} + > ${obj.assertion} -The invalid subject you asserted on was: + The invalid subject you asserted on was: - > ${obj.subject} + > ${obj.subject} -To use chai-jQuery assertions your subject must be valid. + To use chai-jQuery assertions your subject must be valid. -This can sometimes happen if a previous assertion changed the subject.\ -`; - } + This can sometimes happen if a previous assertion changed the subject.` + }, }, check_uncheck: { invalid_element: { message: `${cmd('{{cmd}}')} can only be called on \`:checkbox\`{{phrase}}. Your subject {{word}} a: \`{{node}}\``, - docsUrl: "https://on.cypress.io/{{cmd}}" - } + docsUrl: 'https://on.cypress.io/{{cmd}}', + }, }, clear: { invalid_element: { - message: `\ -${cmd('clear')} failed because it requires a valid clearable element. - -The element cleared was: - - > \`{{node}}\` - -A clearable element matches one of the following selectors: - 'a[href]' - 'area[href]' - 'input' - 'select' - 'textarea' - 'button' - 'iframe' - '[tabindex]' - '[contenteditable]'\ -`, - docsUrl: "https://on.cypress.io/clear" - } + message: stripIndent`\ + ${cmd('clear')} failed because it requires a valid clearable element. + + The element cleared was: + + > \`{{node}}\` + + A clearable element matches one of the following selectors: + 'a[href]' + 'area[href]' + 'input' + 'select' + 'textarea' + 'button' + 'iframe' + '[tabindex]' + '[contenteditable]'`, + docsUrl: 'https://on.cypress.io/clear', + }, }, clearCookie: { invalid_argument: { message: `${cmd('clearCookie')} must be passed a string argument for name.`, - docsUrl: "https://on.cypress.io/clearcookie" - } + docsUrl: 'https://on.cypress.io/clearcookie', + }, }, clearLocalStorage: { invalid_argument: { message: `${cmd('clearLocalStorage')} must be called with either a string or regular expression.`, - docsUrl: "https://on.cypress.io/clearlocalstorage" - } + docsUrl: 'https://on.cypress.io/clearlocalstorage', + }, }, click: { multiple_elements: { message: `${cmd('{{cmd}}')} can only be called on a single element. Your subject contained {{num}} elements. Pass \`{ multiple: true }\` if you want to serially click each element.`, - docsUrl: "https://on.cypress.io/click" + docsUrl: 'https://on.cypress.io/click', }, on_select_element: { message: `${cmd('{{cmd}}')} cannot be called on a \`\`. Your subject is a: \`{{node}}\``, - docsUrl: "https://on.cypress.io/select" + docsUrl: 'https://on.cypress.io/select', }, invalid_multiple: { message: `${cmd('select')} was called with an array of arguments but does not have a \`multiple\` attribute set.`, - docsUrl: "https://on.cypress.io/select" + docsUrl: 'https://on.cypress.io/select', }, multiple_elements: { message: `${cmd('select')} can only be called on a single \`