1/** 2 * @fileoverview Mocha test wrapper 3 * @author Ilya Volodin 4 */ 5"use strict"; 6 7/* global describe, it */ 8 9/* 10 * This is a wrapper around mocha to allow for DRY unittests for eslint 11 * Format: 12 * RuleTester.run("{ruleName}", { 13 * valid: [ 14 * "{code}", 15 * { code: "{code}", options: {options}, globals: {globals}, parser: "{parser}", settings: {settings} } 16 * ], 17 * invalid: [ 18 * { code: "{code}", errors: {numErrors} }, 19 * { code: "{code}", errors: ["{errorMessage}"] }, 20 * { code: "{code}", options: {options}, globals: {globals}, parser: "{parser}", settings: {settings}, errors: [{ message: "{errorMessage}", type: "{errorNodeType}"}] } 21 * ] 22 * }); 23 * 24 * Variables: 25 * {code} - String that represents the code to be tested 26 * {options} - Arguments that are passed to the configurable rules. 27 * {globals} - An object representing a list of variables that are 28 * registered as globals 29 * {parser} - String representing the parser to use 30 * {settings} - An object representing global settings for all rules 31 * {numErrors} - If failing case doesn't need to check error message, 32 * this integer will specify how many errors should be 33 * received 34 * {errorMessage} - Message that is returned by the rule on failure 35 * {errorNodeType} - AST node type that is returned by they rule as 36 * a cause of the failure. 37 */ 38 39//------------------------------------------------------------------------------ 40// Requirements 41//------------------------------------------------------------------------------ 42 43const 44 assert = require("assert"), 45 path = require("path"), 46 util = require("util"), 47 lodash = require("lodash"), 48 Traverser = require("../../lib/shared/traverser"), 49 { getRuleOptionsSchema, validate } = require("../shared/config-validator"), 50 { Linter, SourceCodeFixer, interpolate } = require("../linter"); 51 52const ajv = require("../shared/ajv")({ strictDefaults: true }); 53 54const espreePath = require.resolve("espree"); 55 56//------------------------------------------------------------------------------ 57// Typedefs 58//------------------------------------------------------------------------------ 59 60/** @typedef {import("../shared/types").Parser} Parser */ 61 62/** 63 * A test case that is expected to pass lint. 64 * @typedef {Object} ValidTestCase 65 * @property {string} code Code for the test case. 66 * @property {any[]} [options] Options for the test case. 67 * @property {{ [name: string]: any }} [settings] Settings for the test case. 68 * @property {string} [filename] The fake filename for the test case. Useful for rules that make assertion about filenames. 69 * @property {string} [parser] The absolute path for the parser. 70 * @property {{ [name: string]: any }} [parserOptions] Options for the parser. 71 * @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables. 72 * @property {{ [name: string]: boolean }} [env] Environments for the test case. 73 */ 74 75/** 76 * A test case that is expected to fail lint. 77 * @typedef {Object} InvalidTestCase 78 * @property {string} code Code for the test case. 79 * @property {number | Array<TestCaseError | string | RegExp>} errors Expected errors. 80 * @property {string | null} [output] The expected code after autofixes are applied. If set to `null`, the test runner will assert that no autofix is suggested. 81 * @property {any[]} [options] Options for the test case. 82 * @property {{ [name: string]: any }} [settings] Settings for the test case. 83 * @property {string} [filename] The fake filename for the test case. Useful for rules that make assertion about filenames. 84 * @property {string} [parser] The absolute path for the parser. 85 * @property {{ [name: string]: any }} [parserOptions] Options for the parser. 86 * @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables. 87 * @property {{ [name: string]: boolean }} [env] Environments for the test case. 88 */ 89 90/** 91 * A description of a reported error used in a rule tester test. 92 * @typedef {Object} TestCaseError 93 * @property {string | RegExp} [message] Message. 94 * @property {string} [messageId] Message ID. 95 * @property {string} [type] The type of the reported AST node. 96 * @property {{ [name: string]: string }} [data] The data used to fill the message template. 97 * @property {number} [line] The 1-based line number of the reported start location. 98 * @property {number} [column] The 1-based column number of the reported start location. 99 * @property {number} [endLine] The 1-based line number of the reported end location. 100 * @property {number} [endColumn] The 1-based column number of the reported end location. 101 */ 102 103//------------------------------------------------------------------------------ 104// Private Members 105//------------------------------------------------------------------------------ 106 107/* 108 * testerDefaultConfig must not be modified as it allows to reset the tester to 109 * the initial default configuration 110 */ 111const testerDefaultConfig = { rules: {} }; 112let defaultConfig = { rules: {} }; 113 114/* 115 * List every parameters possible on a test case that are not related to eslint 116 * configuration 117 */ 118const RuleTesterParameters = [ 119 "code", 120 "filename", 121 "options", 122 "errors", 123 "output" 124]; 125 126/* 127 * All allowed property names in error objects. 128 */ 129const errorObjectParameters = new Set([ 130 "message", 131 "messageId", 132 "data", 133 "type", 134 "line", 135 "column", 136 "endLine", 137 "endColumn", 138 "suggestions" 139]); 140const friendlyErrorObjectParameterList = `[${[...errorObjectParameters].map(key => `'${key}'`).join(", ")}]`; 141 142/* 143 * All allowed property names in suggestion objects. 144 */ 145const suggestionObjectParameters = new Set([ 146 "desc", 147 "messageId", 148 "data", 149 "output" 150]); 151const friendlySuggestionObjectParameterList = `[${[...suggestionObjectParameters].map(key => `'${key}'`).join(", ")}]`; 152 153const hasOwnProperty = Function.call.bind(Object.hasOwnProperty); 154 155/** 156 * Clones a given value deeply. 157 * Note: This ignores `parent` property. 158 * @param {any} x A value to clone. 159 * @returns {any} A cloned value. 160 */ 161function cloneDeeplyExcludesParent(x) { 162 if (typeof x === "object" && x !== null) { 163 if (Array.isArray(x)) { 164 return x.map(cloneDeeplyExcludesParent); 165 } 166 167 const retv = {}; 168 169 for (const key in x) { 170 if (key !== "parent" && hasOwnProperty(x, key)) { 171 retv[key] = cloneDeeplyExcludesParent(x[key]); 172 } 173 } 174 175 return retv; 176 } 177 178 return x; 179} 180 181/** 182 * Freezes a given value deeply. 183 * @param {any} x A value to freeze. 184 * @returns {void} 185 */ 186function freezeDeeply(x) { 187 if (typeof x === "object" && x !== null) { 188 if (Array.isArray(x)) { 189 x.forEach(freezeDeeply); 190 } else { 191 for (const key in x) { 192 if (key !== "parent" && hasOwnProperty(x, key)) { 193 freezeDeeply(x[key]); 194 } 195 } 196 } 197 Object.freeze(x); 198 } 199} 200 201/** 202 * Replace control characters by `\u00xx` form. 203 * @param {string} text The text to sanitize. 204 * @returns {string} The sanitized text. 205 */ 206function sanitize(text) { 207 return text.replace( 208 /[\u0000-\u0009\u000b-\u001a]/gu, // eslint-disable-line no-control-regex 209 c => `\\u${c.codePointAt(0).toString(16).padStart(4, "0")}` 210 ); 211} 212 213/** 214 * Define `start`/`end` properties as throwing error. 215 * @param {string} objName Object name used for error messages. 216 * @param {ASTNode} node The node to define. 217 * @returns {void} 218 */ 219function defineStartEndAsError(objName, node) { 220 Object.defineProperties(node, { 221 start: { 222 get() { 223 throw new Error(`Use ${objName}.range[0] instead of ${objName}.start`); 224 }, 225 configurable: true, 226 enumerable: false 227 }, 228 end: { 229 get() { 230 throw new Error(`Use ${objName}.range[1] instead of ${objName}.end`); 231 }, 232 configurable: true, 233 enumerable: false 234 } 235 }); 236} 237 238/** 239 * Define `start`/`end` properties of all nodes of the given AST as throwing error. 240 * @param {ASTNode} ast The root node to errorize `start`/`end` properties. 241 * @param {Object} [visitorKeys] Visitor keys to be used for traversing the given ast. 242 * @returns {void} 243 */ 244function defineStartEndAsErrorInTree(ast, visitorKeys) { 245 Traverser.traverse(ast, { visitorKeys, enter: defineStartEndAsError.bind(null, "node") }); 246 ast.tokens.forEach(defineStartEndAsError.bind(null, "token")); 247 ast.comments.forEach(defineStartEndAsError.bind(null, "token")); 248} 249 250/** 251 * Wraps the given parser in order to intercept and modify return values from the `parse` and `parseForESLint` methods, for test purposes. 252 * In particular, to modify ast nodes, tokens and comments to throw on access to their `start` and `end` properties. 253 * @param {Parser} parser Parser object. 254 * @returns {Parser} Wrapped parser object. 255 */ 256function wrapParser(parser) { 257 if (typeof parser.parseForESLint === "function") { 258 return { 259 parseForESLint(...args) { 260 const ret = parser.parseForESLint(...args); 261 262 defineStartEndAsErrorInTree(ret.ast, ret.visitorKeys); 263 return ret; 264 } 265 }; 266 } 267 return { 268 parse(...args) { 269 const ast = parser.parse(...args); 270 271 defineStartEndAsErrorInTree(ast); 272 return ast; 273 } 274 }; 275} 276 277//------------------------------------------------------------------------------ 278// Public Interface 279//------------------------------------------------------------------------------ 280 281// default separators for testing 282const DESCRIBE = Symbol("describe"); 283const IT = Symbol("it"); 284 285/** 286 * This is `it` default handler if `it` don't exist. 287 * @this {Mocha} 288 * @param {string} text The description of the test case. 289 * @param {Function} method The logic of the test case. 290 * @returns {any} Returned value of `method`. 291 */ 292function itDefaultHandler(text, method) { 293 try { 294 return method.call(this); 295 } catch (err) { 296 if (err instanceof assert.AssertionError) { 297 err.message += ` (${util.inspect(err.actual)} ${err.operator} ${util.inspect(err.expected)})`; 298 } 299 throw err; 300 } 301} 302 303/** 304 * This is `describe` default handler if `describe` don't exist. 305 * @this {Mocha} 306 * @param {string} text The description of the test case. 307 * @param {Function} method The logic of the test case. 308 * @returns {any} Returned value of `method`. 309 */ 310function describeDefaultHandler(text, method) { 311 return method.call(this); 312} 313 314class RuleTester { 315 316 /** 317 * Creates a new instance of RuleTester. 318 * @param {Object} [testerConfig] Optional, extra configuration for the tester 319 */ 320 constructor(testerConfig) { 321 322 /** 323 * The configuration to use for this tester. Combination of the tester 324 * configuration and the default configuration. 325 * @type {Object} 326 */ 327 this.testerConfig = lodash.merge( 328 329 // we have to clone because merge uses the first argument for recipient 330 lodash.cloneDeep(defaultConfig), 331 testerConfig, 332 { rules: { "rule-tester/validate-ast": "error" } } 333 ); 334 335 /** 336 * Rule definitions to define before tests. 337 * @type {Object} 338 */ 339 this.rules = {}; 340 this.linter = new Linter(); 341 } 342 343 /** 344 * Set the configuration to use for all future tests 345 * @param {Object} config the configuration to use. 346 * @returns {void} 347 */ 348 static setDefaultConfig(config) { 349 if (typeof config !== "object") { 350 throw new TypeError("RuleTester.setDefaultConfig: config must be an object"); 351 } 352 defaultConfig = config; 353 354 // Make sure the rules object exists since it is assumed to exist later 355 defaultConfig.rules = defaultConfig.rules || {}; 356 } 357 358 /** 359 * Get the current configuration used for all tests 360 * @returns {Object} the current configuration 361 */ 362 static getDefaultConfig() { 363 return defaultConfig; 364 } 365 366 /** 367 * Reset the configuration to the initial configuration of the tester removing 368 * any changes made until now. 369 * @returns {void} 370 */ 371 static resetDefaultConfig() { 372 defaultConfig = lodash.cloneDeep(testerDefaultConfig); 373 } 374 375 376 /* 377 * If people use `mocha test.js --watch` command, `describe` and `it` function 378 * instances are different for each execution. So `describe` and `it` should get fresh instance 379 * always. 380 */ 381 static get describe() { 382 return ( 383 this[DESCRIBE] || 384 (typeof describe === "function" ? describe : describeDefaultHandler) 385 ); 386 } 387 388 static set describe(value) { 389 this[DESCRIBE] = value; 390 } 391 392 static get it() { 393 return ( 394 this[IT] || 395 (typeof it === "function" ? it : itDefaultHandler) 396 ); 397 } 398 399 static set it(value) { 400 this[IT] = value; 401 } 402 403 /** 404 * Define a rule for one particular run of tests. 405 * @param {string} name The name of the rule to define. 406 * @param {Function} rule The rule definition. 407 * @returns {void} 408 */ 409 defineRule(name, rule) { 410 this.rules[name] = rule; 411 } 412 413 /** 414 * Adds a new rule test to execute. 415 * @param {string} ruleName The name of the rule to run. 416 * @param {Function} rule The rule to test. 417 * @param {{ 418 * valid: (ValidTestCase | string)[], 419 * invalid: InvalidTestCase[] 420 * }} test The collection of tests to run. 421 * @returns {void} 422 */ 423 run(ruleName, rule, test) { 424 425 const testerConfig = this.testerConfig, 426 requiredScenarios = ["valid", "invalid"], 427 scenarioErrors = [], 428 linter = this.linter; 429 430 if (lodash.isNil(test) || typeof test !== "object") { 431 throw new TypeError(`Test Scenarios for rule ${ruleName} : Could not find test scenario object`); 432 } 433 434 requiredScenarios.forEach(scenarioType => { 435 if (lodash.isNil(test[scenarioType])) { 436 scenarioErrors.push(`Could not find any ${scenarioType} test scenarios`); 437 } 438 }); 439 440 if (scenarioErrors.length > 0) { 441 throw new Error([ 442 `Test Scenarios for rule ${ruleName} is invalid:` 443 ].concat(scenarioErrors).join("\n")); 444 } 445 446 447 linter.defineRule(ruleName, Object.assign({}, rule, { 448 449 // Create a wrapper rule that freezes the `context` properties. 450 create(context) { 451 freezeDeeply(context.options); 452 freezeDeeply(context.settings); 453 freezeDeeply(context.parserOptions); 454 455 return (typeof rule === "function" ? rule : rule.create)(context); 456 } 457 })); 458 459 linter.defineRules(this.rules); 460 461 /** 462 * Run the rule for the given item 463 * @param {string|Object} item Item to run the rule against 464 * @returns {Object} Eslint run result 465 * @private 466 */ 467 function runRuleForItem(item) { 468 let config = lodash.cloneDeep(testerConfig), 469 code, filename, output, beforeAST, afterAST; 470 471 if (typeof item === "string") { 472 code = item; 473 } else { 474 code = item.code; 475 476 /* 477 * Assumes everything on the item is a config except for the 478 * parameters used by this tester 479 */ 480 const itemConfig = lodash.omit(item, RuleTesterParameters); 481 482 /* 483 * Create the config object from the tester config and this item 484 * specific configurations. 485 */ 486 config = lodash.merge( 487 config, 488 itemConfig 489 ); 490 } 491 492 if (item.filename) { 493 filename = item.filename; 494 } 495 496 if (hasOwnProperty(item, "options")) { 497 assert(Array.isArray(item.options), "options must be an array"); 498 config.rules[ruleName] = [1].concat(item.options); 499 } else { 500 config.rules[ruleName] = 1; 501 } 502 503 const schema = getRuleOptionsSchema(rule); 504 505 /* 506 * Setup AST getters. 507 * The goal is to check whether or not AST was modified when 508 * running the rule under test. 509 */ 510 linter.defineRule("rule-tester/validate-ast", () => ({ 511 Program(node) { 512 beforeAST = cloneDeeplyExcludesParent(node); 513 }, 514 "Program:exit"(node) { 515 afterAST = node; 516 } 517 })); 518 519 if (typeof config.parser === "string") { 520 assert(path.isAbsolute(config.parser), "Parsers provided as strings to RuleTester must be absolute paths"); 521 } else { 522 config.parser = espreePath; 523 } 524 525 linter.defineParser(config.parser, wrapParser(require(config.parser))); 526 527 if (schema) { 528 ajv.validateSchema(schema); 529 530 if (ajv.errors) { 531 const errors = ajv.errors.map(error => { 532 const field = error.dataPath[0] === "." ? error.dataPath.slice(1) : error.dataPath; 533 534 return `\t${field}: ${error.message}`; 535 }).join("\n"); 536 537 throw new Error([`Schema for rule ${ruleName} is invalid:`, errors]); 538 } 539 540 /* 541 * `ajv.validateSchema` checks for errors in the structure of the schema (by comparing the schema against a "meta-schema"), 542 * and it reports those errors individually. However, there are other types of schema errors that only occur when compiling 543 * the schema (e.g. using invalid defaults in a schema), and only one of these errors can be reported at a time. As a result, 544 * the schema is compiled here separately from checking for `validateSchema` errors. 545 */ 546 try { 547 ajv.compile(schema); 548 } catch (err) { 549 throw new Error(`Schema for rule ${ruleName} is invalid: ${err.message}`); 550 } 551 } 552 553 validate(config, "rule-tester", id => (id === ruleName ? rule : null)); 554 555 // Verify the code. 556 const messages = linter.verify(code, config, filename); 557 const fatalErrorMessage = messages.find(m => m.fatal); 558 559 assert(!fatalErrorMessage, `A fatal parsing error occurred: ${fatalErrorMessage && fatalErrorMessage.message}`); 560 561 // Verify if autofix makes a syntax error or not. 562 if (messages.some(m => m.fix)) { 563 output = SourceCodeFixer.applyFixes(code, messages).output; 564 const errorMessageInFix = linter.verify(output, config, filename).find(m => m.fatal); 565 566 assert(!errorMessageInFix, [ 567 "A fatal parsing error occurred in autofix.", 568 `Error: ${errorMessageInFix && errorMessageInFix.message}`, 569 "Autofix output:", 570 output 571 ].join("\n")); 572 } else { 573 output = code; 574 } 575 576 return { 577 messages, 578 output, 579 beforeAST, 580 afterAST: cloneDeeplyExcludesParent(afterAST) 581 }; 582 } 583 584 /** 585 * Check if the AST was changed 586 * @param {ASTNode} beforeAST AST node before running 587 * @param {ASTNode} afterAST AST node after running 588 * @returns {void} 589 * @private 590 */ 591 function assertASTDidntChange(beforeAST, afterAST) { 592 if (!lodash.isEqual(beforeAST, afterAST)) { 593 assert.fail("Rule should not modify AST."); 594 } 595 } 596 597 /** 598 * Check if the template is valid or not 599 * all valid cases go through this 600 * @param {string|Object} item Item to run the rule against 601 * @returns {void} 602 * @private 603 */ 604 function testValidTemplate(item) { 605 const result = runRuleForItem(item); 606 const messages = result.messages; 607 608 assert.strictEqual(messages.length, 0, util.format("Should have no errors but had %d: %s", 609 messages.length, util.inspect(messages))); 610 611 assertASTDidntChange(result.beforeAST, result.afterAST); 612 } 613 614 /** 615 * Asserts that the message matches its expected value. If the expected 616 * value is a regular expression, it is checked against the actual 617 * value. 618 * @param {string} actual Actual value 619 * @param {string|RegExp} expected Expected value 620 * @returns {void} 621 * @private 622 */ 623 function assertMessageMatches(actual, expected) { 624 if (expected instanceof RegExp) { 625 626 // assert.js doesn't have a built-in RegExp match function 627 assert.ok( 628 expected.test(actual), 629 `Expected '${actual}' to match ${expected}` 630 ); 631 } else { 632 assert.strictEqual(actual, expected); 633 } 634 } 635 636 /** 637 * Check if the template is invalid or not 638 * all invalid cases go through this. 639 * @param {string|Object} item Item to run the rule against 640 * @returns {void} 641 * @private 642 */ 643 function testInvalidTemplate(item) { 644 assert.ok(item.errors || item.errors === 0, 645 `Did not specify errors for an invalid test of ${ruleName}`); 646 647 const ruleHasMetaMessages = hasOwnProperty(rule, "meta") && hasOwnProperty(rule.meta, "messages"); 648 const friendlyIDList = ruleHasMetaMessages ? `[${Object.keys(rule.meta.messages).map(key => `'${key}'`).join(", ")}]` : null; 649 650 const result = runRuleForItem(item); 651 const messages = result.messages; 652 653 if (typeof item.errors === "number") { 654 assert.strictEqual(messages.length, item.errors, util.format("Should have %d error%s but had %d: %s", 655 item.errors, item.errors === 1 ? "" : "s", messages.length, util.inspect(messages))); 656 } else { 657 assert.strictEqual( 658 messages.length, item.errors.length, 659 util.format( 660 "Should have %d error%s but had %d: %s", 661 item.errors.length, item.errors.length === 1 ? "" : "s", messages.length, util.inspect(messages) 662 ) 663 ); 664 665 const hasMessageOfThisRule = messages.some(m => m.ruleId === ruleName); 666 667 for (let i = 0, l = item.errors.length; i < l; i++) { 668 const error = item.errors[i]; 669 const message = messages[i]; 670 671 assert(hasMessageOfThisRule, "Error rule name should be the same as the name of the rule being tested"); 672 673 if (typeof error === "string" || error instanceof RegExp) { 674 675 // Just an error message. 676 assertMessageMatches(message.message, error); 677 } else if (typeof error === "object" && error !== null) { 678 679 /* 680 * Error object. 681 * This may have a message, messageId, data, node type, line, and/or 682 * column. 683 */ 684 685 Object.keys(error).forEach(propertyName => { 686 assert.ok( 687 errorObjectParameters.has(propertyName), 688 `Invalid error property name '${propertyName}'. Expected one of ${friendlyErrorObjectParameterList}.` 689 ); 690 }); 691 692 if (hasOwnProperty(error, "message")) { 693 assert.ok(!hasOwnProperty(error, "messageId"), "Error should not specify both 'message' and a 'messageId'."); 694 assert.ok(!hasOwnProperty(error, "data"), "Error should not specify both 'data' and 'message'."); 695 assertMessageMatches(message.message, error.message); 696 } else if (hasOwnProperty(error, "messageId")) { 697 assert.ok( 698 ruleHasMetaMessages, 699 "Error can not use 'messageId' if rule under test doesn't define 'meta.messages'." 700 ); 701 if (!hasOwnProperty(rule.meta.messages, error.messageId)) { 702 assert(false, `Invalid messageId '${error.messageId}'. Expected one of ${friendlyIDList}.`); 703 } 704 assert.strictEqual( 705 message.messageId, 706 error.messageId, 707 `messageId '${message.messageId}' does not match expected messageId '${error.messageId}'.` 708 ); 709 if (hasOwnProperty(error, "data")) { 710 711 /* 712 * if data was provided, then directly compare the returned message to a synthetic 713 * interpolated message using the same message ID and data provided in the test. 714 * See https://github.com/eslint/eslint/issues/9890 for context. 715 */ 716 const unformattedOriginalMessage = rule.meta.messages[error.messageId]; 717 const rehydratedMessage = interpolate(unformattedOriginalMessage, error.data); 718 719 assert.strictEqual( 720 message.message, 721 rehydratedMessage, 722 `Hydrated message "${rehydratedMessage}" does not match "${message.message}"` 723 ); 724 } 725 } 726 727 assert.ok( 728 hasOwnProperty(error, "data") ? hasOwnProperty(error, "messageId") : true, 729 "Error must specify 'messageId' if 'data' is used." 730 ); 731 732 if (error.type) { 733 assert.strictEqual(message.nodeType, error.type, `Error type should be ${error.type}, found ${message.nodeType}`); 734 } 735 736 if (hasOwnProperty(error, "line")) { 737 assert.strictEqual(message.line, error.line, `Error line should be ${error.line}`); 738 } 739 740 if (hasOwnProperty(error, "column")) { 741 assert.strictEqual(message.column, error.column, `Error column should be ${error.column}`); 742 } 743 744 if (hasOwnProperty(error, "endLine")) { 745 assert.strictEqual(message.endLine, error.endLine, `Error endLine should be ${error.endLine}`); 746 } 747 748 if (hasOwnProperty(error, "endColumn")) { 749 assert.strictEqual(message.endColumn, error.endColumn, `Error endColumn should be ${error.endColumn}`); 750 } 751 752 if (hasOwnProperty(error, "suggestions")) { 753 754 // Support asserting there are no suggestions 755 if (!error.suggestions || (Array.isArray(error.suggestions) && error.suggestions.length === 0)) { 756 if (Array.isArray(message.suggestions) && message.suggestions.length > 0) { 757 assert.fail(`Error should have no suggestions on error with message: "${message.message}"`); 758 } 759 } else { 760 assert.strictEqual(Array.isArray(message.suggestions), true, `Error should have an array of suggestions. Instead received "${message.suggestions}" on error with message: "${message.message}"`); 761 assert.strictEqual(message.suggestions.length, error.suggestions.length, `Error should have ${error.suggestions.length} suggestions. Instead found ${message.suggestions.length} suggestions`); 762 763 error.suggestions.forEach((expectedSuggestion, index) => { 764 assert.ok( 765 typeof expectedSuggestion === "object" && expectedSuggestion !== null, 766 "Test suggestion in 'suggestions' array must be an object." 767 ); 768 Object.keys(expectedSuggestion).forEach(propertyName => { 769 assert.ok( 770 suggestionObjectParameters.has(propertyName), 771 `Invalid suggestion property name '${propertyName}'. Expected one of ${friendlySuggestionObjectParameterList}.` 772 ); 773 }); 774 775 const actualSuggestion = message.suggestions[index]; 776 const suggestionPrefix = `Error Suggestion at index ${index} :`; 777 778 if (hasOwnProperty(expectedSuggestion, "desc")) { 779 assert.ok( 780 !hasOwnProperty(expectedSuggestion, "data"), 781 `${suggestionPrefix} Test should not specify both 'desc' and 'data'.` 782 ); 783 assert.strictEqual( 784 actualSuggestion.desc, 785 expectedSuggestion.desc, 786 `${suggestionPrefix} desc should be "${expectedSuggestion.desc}" but got "${actualSuggestion.desc}" instead.` 787 ); 788 } 789 790 if (hasOwnProperty(expectedSuggestion, "messageId")) { 791 assert.ok( 792 ruleHasMetaMessages, 793 `${suggestionPrefix} Test can not use 'messageId' if rule under test doesn't define 'meta.messages'.` 794 ); 795 assert.ok( 796 hasOwnProperty(rule.meta.messages, expectedSuggestion.messageId), 797 `${suggestionPrefix} Test has invalid messageId '${expectedSuggestion.messageId}', the rule under test allows only one of ${friendlyIDList}.` 798 ); 799 assert.strictEqual( 800 actualSuggestion.messageId, 801 expectedSuggestion.messageId, 802 `${suggestionPrefix} messageId should be '${expectedSuggestion.messageId}' but got '${actualSuggestion.messageId}' instead.` 803 ); 804 if (hasOwnProperty(expectedSuggestion, "data")) { 805 const unformattedMetaMessage = rule.meta.messages[expectedSuggestion.messageId]; 806 const rehydratedDesc = interpolate(unformattedMetaMessage, expectedSuggestion.data); 807 808 assert.strictEqual( 809 actualSuggestion.desc, 810 rehydratedDesc, 811 `${suggestionPrefix} Hydrated test desc "${rehydratedDesc}" does not match received desc "${actualSuggestion.desc}".` 812 ); 813 } 814 } else { 815 assert.ok( 816 !hasOwnProperty(expectedSuggestion, "data"), 817 `${suggestionPrefix} Test must specify 'messageId' if 'data' is used.` 818 ); 819 } 820 821 if (hasOwnProperty(expectedSuggestion, "output")) { 822 const codeWithAppliedSuggestion = SourceCodeFixer.applyFixes(item.code, [actualSuggestion]).output; 823 824 assert.strictEqual(codeWithAppliedSuggestion, expectedSuggestion.output, `Expected the applied suggestion fix to match the test suggestion output for suggestion at index: ${index} on error with message: "${message.message}"`); 825 } 826 }); 827 } 828 } 829 } else { 830 831 // Message was an unexpected type 832 assert.fail(`Error should be a string, object, or RegExp, but found (${util.inspect(message)})`); 833 } 834 } 835 } 836 837 if (hasOwnProperty(item, "output")) { 838 if (item.output === null) { 839 assert.strictEqual( 840 result.output, 841 item.code, 842 "Expected no autofixes to be suggested" 843 ); 844 } else { 845 assert.strictEqual(result.output, item.output, "Output is incorrect."); 846 } 847 } else { 848 assert.strictEqual( 849 result.output, 850 item.code, 851 "The rule fixed the code. Please add 'output' property." 852 ); 853 } 854 855 // Rules that produce fixes must have `meta.fixable` property. 856 if (result.output !== item.code) { 857 assert.ok( 858 hasOwnProperty(rule, "meta"), 859 "Fixable rules should export a `meta.fixable` property." 860 ); 861 862 // Linter throws if a rule that produced a fix has `meta` but doesn't have `meta.fixable`. 863 } 864 865 assertASTDidntChange(result.beforeAST, result.afterAST); 866 } 867 868 /* 869 * This creates a mocha test suite and pipes all supplied info through 870 * one of the templates above. 871 */ 872 RuleTester.describe(ruleName, () => { 873 RuleTester.describe("valid", () => { 874 test.valid.forEach(valid => { 875 RuleTester.it(sanitize(typeof valid === "object" ? valid.code : valid), () => { 876 testValidTemplate(valid); 877 }); 878 }); 879 }); 880 881 RuleTester.describe("invalid", () => { 882 test.invalid.forEach(invalid => { 883 RuleTester.it(sanitize(invalid.code), () => { 884 testInvalidTemplate(invalid); 885 }); 886 }); 887 }); 888 }); 889 } 890} 891 892RuleTester[DESCRIBE] = RuleTester[IT] = null; 893 894module.exports = RuleTester; 895