2 * @fileoverview Mocha test wrapper
7 /* global describe, it */
10 * This is a wrapper around mocha to allow for DRY unittests for eslint
12 * RuleTester.run("{ruleName}", {
15 * { code: "{code}", options: {options}, globals: {globals}, parser: "{parser}", settings: {settings} }
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}"}] }
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
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.
39 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
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");
52 const ajv = require("../shared/ajv")({ strictDefaults: true });
54 const espreePath = require.resolve("espree");
56 //------------------------------------------------------------------------------
58 //------------------------------------------------------------------------------
60 /** @typedef {import("../shared/types").Parser} Parser */
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.
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.
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.
103 //------------------------------------------------------------------------------
105 //------------------------------------------------------------------------------
108 * testerDefaultConfig must not be modified as it allows to reset the tester to
109 * the initial default configuration
111 const testerDefaultConfig = { rules: {} };
112 let defaultConfig = { rules: {} };
115 * List every parameters possible on a test case that are not related to eslint
118 const RuleTesterParameters = [
127 * All allowed property names in error objects.
129 const errorObjectParameters = new Set([
140 const friendlyErrorObjectParameterList = `[${[...errorObjectParameters].map(key => `'${key}'`).join(", ")}]`;
143 * All allowed property names in suggestion objects.
145 const suggestionObjectParameters = new Set([
151 const friendlySuggestionObjectParameterList = `[${[...suggestionObjectParameters].map(key => `'${key}'`).join(", ")}]`;
153 const hasOwnProperty = Function.call.bind(Object.hasOwnProperty);
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.
161 function cloneDeeplyExcludesParent(x) {
162 if (typeof x === "object" && x !== null) {
163 if (Array.isArray(x)) {
164 return x.map(cloneDeeplyExcludesParent);
169 for (const key in x) {
170 if (key !== "parent" && hasOwnProperty(x, key)) {
171 retv[key] = cloneDeeplyExcludesParent(x[key]);
182 * Freezes a given value deeply.
183 * @param {any} x A value to freeze.
186 function freezeDeeply(x) {
187 if (typeof x === "object" && x !== null) {
188 if (Array.isArray(x)) {
189 x.forEach(freezeDeeply);
191 for (const key in x) {
192 if (key !== "parent" && hasOwnProperty(x, key)) {
193 freezeDeeply(x[key]);
202 * Replace control characters by `\u00xx` form.
203 * @param {string} text The text to sanitize.
204 * @returns {string} The sanitized text.
206 function sanitize(text) {
208 /[\u0000-\u0009\u000b-\u001a]/gu, // eslint-disable-line no-control-regex
209 c => `\\u${c.codePointAt(0).toString(16).padStart(4, "0")}`
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.
219 function defineStartEndAsError(objName, node) {
220 Object.defineProperties(node, {
223 throw new Error(`Use ${objName}.range[0] instead of ${objName}.start`);
230 throw new Error(`Use ${objName}.range[1] instead of ${objName}.end`);
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.
244 function 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"));
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.
256 function wrapParser(parser) {
257 if (typeof parser.parseForESLint === "function") {
259 parseForESLint(...args) {
260 const ret = parser.parseForESLint(...args);
262 defineStartEndAsErrorInTree(ret.ast, ret.visitorKeys);
269 const ast = parser.parse(...args);
271 defineStartEndAsErrorInTree(ast);
277 //------------------------------------------------------------------------------
279 //------------------------------------------------------------------------------
281 // default separators for testing
282 const DESCRIBE = Symbol("describe");
283 const IT = Symbol("it");
286 * This is `it` default handler if `it` don't exist.
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`.
292 function itDefaultHandler(text, method) {
294 return method.call(this);
296 if (err instanceof assert.AssertionError) {
297 err.message += ` (${util.inspect(err.actual)} ${err.operator} ${util.inspect(err.expected)})`;
304 * This is `describe` default handler if `describe` don't exist.
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`.
310 function describeDefaultHandler(text, method) {
311 return method.call(this);
317 * Creates a new instance of RuleTester.
318 * @param {Object} [testerConfig] Optional, extra configuration for the tester
320 constructor(testerConfig) {
323 * The configuration to use for this tester. Combination of the tester
324 * configuration and the default configuration.
327 this.testerConfig = lodash.merge(
329 // we have to clone because merge uses the first argument for recipient
330 lodash.cloneDeep(defaultConfig),
332 { rules: { "rule-tester/validate-ast": "error" } }
336 * Rule definitions to define before tests.
340 this.linter = new Linter();
344 * Set the configuration to use for all future tests
345 * @param {Object} config the configuration to use.
348 static setDefaultConfig(config) {
349 if (typeof config !== "object") {
350 throw new TypeError("RuleTester.setDefaultConfig: config must be an object");
352 defaultConfig = config;
354 // Make sure the rules object exists since it is assumed to exist later
355 defaultConfig.rules = defaultConfig.rules || {};
359 * Get the current configuration used for all tests
360 * @returns {Object} the current configuration
362 static getDefaultConfig() {
363 return defaultConfig;
367 * Reset the configuration to the initial configuration of the tester removing
368 * any changes made until now.
371 static resetDefaultConfig() {
372 defaultConfig = lodash.cloneDeep(testerDefaultConfig);
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
381 static get describe() {
384 (typeof describe === "function" ? describe : describeDefaultHandler)
388 static set describe(value) {
389 this[DESCRIBE] = value;
395 (typeof it === "function" ? it : itDefaultHandler)
399 static set it(value) {
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.
409 defineRule(name, rule) {
410 this.rules[name] = rule;
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.
418 * valid: (ValidTestCase | string)[],
419 * invalid: InvalidTestCase[]
420 * }} test The collection of tests to run.
423 run(ruleName, rule, test) {
425 const testerConfig = this.testerConfig,
426 requiredScenarios = ["valid", "invalid"],
428 linter = this.linter;
430 if (!test || typeof test !== "object") {
431 throw new TypeError(`Test Scenarios for rule ${ruleName} : Could not find test scenario object`);
434 requiredScenarios.forEach(scenarioType => {
435 if (!test[scenarioType]) {
436 scenarioErrors.push(`Could not find any ${scenarioType} test scenarios`);
440 if (scenarioErrors.length > 0) {
442 `Test Scenarios for rule ${ruleName} is invalid:`
443 ].concat(scenarioErrors).join("\n"));
447 linter.defineRule(ruleName, Object.assign({}, rule, {
449 // Create a wrapper rule that freezes the `context` properties.
451 freezeDeeply(context.options);
452 freezeDeeply(context.settings);
453 freezeDeeply(context.parserOptions);
455 return (typeof rule === "function" ? rule : rule.create)(context);
459 linter.defineRules(this.rules);
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
467 function runRuleForItem(item) {
468 let config = lodash.cloneDeep(testerConfig),
469 code, filename, output, beforeAST, afterAST;
471 if (typeof item === "string") {
477 * Assumes everything on the item is a config except for the
478 * parameters used by this tester
480 const itemConfig = lodash.omit(item, RuleTesterParameters);
483 * Create the config object from the tester config and this item
484 * specific configurations.
486 config = lodash.merge(
493 filename = item.filename;
496 if (hasOwnProperty(item, "options")) {
497 assert(Array.isArray(item.options), "options must be an array");
498 config.rules[ruleName] = [1].concat(item.options);
500 config.rules[ruleName] = 1;
503 const schema = getRuleOptionsSchema(rule);
507 * The goal is to check whether or not AST was modified when
508 * running the rule under test.
510 linter.defineRule("rule-tester/validate-ast", () => ({
512 beforeAST = cloneDeeplyExcludesParent(node);
514 "Program:exit"(node) {
519 if (typeof config.parser === "string") {
520 assert(path.isAbsolute(config.parser), "Parsers provided as strings to RuleTester must be absolute paths");
522 config.parser = espreePath;
525 linter.defineParser(config.parser, wrapParser(require(config.parser)));
528 ajv.validateSchema(schema);
531 const errors = ajv.errors.map(error => {
532 const field = error.dataPath[0] === "." ? error.dataPath.slice(1) : error.dataPath;
534 return `\t${field}: ${error.message}`;
537 throw new Error([`Schema for rule ${ruleName} is invalid:`, errors]);
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.
549 throw new Error(`Schema for rule ${ruleName} is invalid: ${err.message}`);
553 validate(config, "rule-tester", id => (id === ruleName ? rule : null));
556 const messages = linter.verify(code, config, filename);
557 const fatalErrorMessage = messages.find(m => m.fatal);
559 assert(!fatalErrorMessage, `A fatal parsing error occurred: ${fatalErrorMessage && fatalErrorMessage.message}`);
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);
566 assert(!errorMessageInFix, [
567 "A fatal parsing error occurred in autofix.",
568 `Error: ${errorMessageInFix && errorMessageInFix.message}`,
580 afterAST: cloneDeeplyExcludesParent(afterAST)
585 * Check if the AST was changed
586 * @param {ASTNode} beforeAST AST node before running
587 * @param {ASTNode} afterAST AST node after running
591 function assertASTDidntChange(beforeAST, afterAST) {
592 if (!lodash.isEqual(beforeAST, afterAST)) {
593 assert.fail("Rule should not modify AST.");
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
604 function testValidTemplate(item) {
605 const result = runRuleForItem(item);
606 const messages = result.messages;
608 assert.strictEqual(messages.length, 0, util.format("Should have no errors but had %d: %s",
609 messages.length, util.inspect(messages)));
611 assertASTDidntChange(result.beforeAST, result.afterAST);
615 * Asserts that the message matches its expected value. If the expected
616 * value is a regular expression, it is checked against the actual
618 * @param {string} actual Actual value
619 * @param {string|RegExp} expected Expected value
623 function assertMessageMatches(actual, expected) {
624 if (expected instanceof RegExp) {
626 // assert.js doesn't have a built-in RegExp match function
628 expected.test(actual),
629 `Expected '${actual}' to match ${expected}`
632 assert.strictEqual(actual, expected);
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
643 function testInvalidTemplate(item) {
644 assert.ok(item.errors || item.errors === 0,
645 `Did not specify errors for an invalid test of ${ruleName}`);
647 if (Array.isArray(item.errors) && item.errors.length === 0) {
648 assert.fail("Invalid cases must have at least one error");
651 const ruleHasMetaMessages = hasOwnProperty(rule, "meta") && hasOwnProperty(rule.meta, "messages");
652 const friendlyIDList = ruleHasMetaMessages ? `[${Object.keys(rule.meta.messages).map(key => `'${key}'`).join(", ")}]` : null;
654 const result = runRuleForItem(item);
655 const messages = result.messages;
657 if (typeof item.errors === "number") {
659 if (item.errors === 0) {
660 assert.fail("Invalid cases must have 'error' value greater than 0");
663 assert.strictEqual(messages.length, item.errors, util.format("Should have %d error%s but had %d: %s",
664 item.errors, item.errors === 1 ? "" : "s", messages.length, util.inspect(messages)));
667 messages.length, item.errors.length,
669 "Should have %d error%s but had %d: %s",
670 item.errors.length, item.errors.length === 1 ? "" : "s", messages.length, util.inspect(messages)
674 const hasMessageOfThisRule = messages.some(m => m.ruleId === ruleName);
676 for (let i = 0, l = item.errors.length; i < l; i++) {
677 const error = item.errors[i];
678 const message = messages[i];
680 assert(hasMessageOfThisRule, "Error rule name should be the same as the name of the rule being tested");
682 if (typeof error === "string" || error instanceof RegExp) {
684 // Just an error message.
685 assertMessageMatches(message.message, error);
686 } else if (typeof error === "object" && error !== null) {
690 * This may have a message, messageId, data, node type, line, and/or
694 Object.keys(error).forEach(propertyName => {
696 errorObjectParameters.has(propertyName),
697 `Invalid error property name '${propertyName}'. Expected one of ${friendlyErrorObjectParameterList}.`
701 if (hasOwnProperty(error, "message")) {
702 assert.ok(!hasOwnProperty(error, "messageId"), "Error should not specify both 'message' and a 'messageId'.");
703 assert.ok(!hasOwnProperty(error, "data"), "Error should not specify both 'data' and 'message'.");
704 assertMessageMatches(message.message, error.message);
705 } else if (hasOwnProperty(error, "messageId")) {
708 "Error can not use 'messageId' if rule under test doesn't define 'meta.messages'."
710 if (!hasOwnProperty(rule.meta.messages, error.messageId)) {
711 assert(false, `Invalid messageId '${error.messageId}'. Expected one of ${friendlyIDList}.`);
716 `messageId '${message.messageId}' does not match expected messageId '${error.messageId}'.`
718 if (hasOwnProperty(error, "data")) {
721 * if data was provided, then directly compare the returned message to a synthetic
722 * interpolated message using the same message ID and data provided in the test.
723 * See https://github.com/eslint/eslint/issues/9890 for context.
725 const unformattedOriginalMessage = rule.meta.messages[error.messageId];
726 const rehydratedMessage = interpolate(unformattedOriginalMessage, error.data);
731 `Hydrated message "${rehydratedMessage}" does not match "${message.message}"`
737 hasOwnProperty(error, "data") ? hasOwnProperty(error, "messageId") : true,
738 "Error must specify 'messageId' if 'data' is used."
742 assert.strictEqual(message.nodeType, error.type, `Error type should be ${error.type}, found ${message.nodeType}`);
745 if (hasOwnProperty(error, "line")) {
746 assert.strictEqual(message.line, error.line, `Error line should be ${error.line}`);
749 if (hasOwnProperty(error, "column")) {
750 assert.strictEqual(message.column, error.column, `Error column should be ${error.column}`);
753 if (hasOwnProperty(error, "endLine")) {
754 assert.strictEqual(message.endLine, error.endLine, `Error endLine should be ${error.endLine}`);
757 if (hasOwnProperty(error, "endColumn")) {
758 assert.strictEqual(message.endColumn, error.endColumn, `Error endColumn should be ${error.endColumn}`);
761 if (hasOwnProperty(error, "suggestions")) {
763 // Support asserting there are no suggestions
764 if (!error.suggestions || (Array.isArray(error.suggestions) && error.suggestions.length === 0)) {
765 if (Array.isArray(message.suggestions) && message.suggestions.length > 0) {
766 assert.fail(`Error should have no suggestions on error with message: "${message.message}"`);
769 assert.strictEqual(Array.isArray(message.suggestions), true, `Error should have an array of suggestions. Instead received "${message.suggestions}" on error with message: "${message.message}"`);
770 assert.strictEqual(message.suggestions.length, error.suggestions.length, `Error should have ${error.suggestions.length} suggestions. Instead found ${message.suggestions.length} suggestions`);
772 error.suggestions.forEach((expectedSuggestion, index) => {
774 typeof expectedSuggestion === "object" && expectedSuggestion !== null,
775 "Test suggestion in 'suggestions' array must be an object."
777 Object.keys(expectedSuggestion).forEach(propertyName => {
779 suggestionObjectParameters.has(propertyName),
780 `Invalid suggestion property name '${propertyName}'. Expected one of ${friendlySuggestionObjectParameterList}.`
784 const actualSuggestion = message.suggestions[index];
785 const suggestionPrefix = `Error Suggestion at index ${index} :`;
787 if (hasOwnProperty(expectedSuggestion, "desc")) {
789 !hasOwnProperty(expectedSuggestion, "data"),
790 `${suggestionPrefix} Test should not specify both 'desc' and 'data'.`
793 actualSuggestion.desc,
794 expectedSuggestion.desc,
795 `${suggestionPrefix} desc should be "${expectedSuggestion.desc}" but got "${actualSuggestion.desc}" instead.`
799 if (hasOwnProperty(expectedSuggestion, "messageId")) {
802 `${suggestionPrefix} Test can not use 'messageId' if rule under test doesn't define 'meta.messages'.`
805 hasOwnProperty(rule.meta.messages, expectedSuggestion.messageId),
806 `${suggestionPrefix} Test has invalid messageId '${expectedSuggestion.messageId}', the rule under test allows only one of ${friendlyIDList}.`
809 actualSuggestion.messageId,
810 expectedSuggestion.messageId,
811 `${suggestionPrefix} messageId should be '${expectedSuggestion.messageId}' but got '${actualSuggestion.messageId}' instead.`
813 if (hasOwnProperty(expectedSuggestion, "data")) {
814 const unformattedMetaMessage = rule.meta.messages[expectedSuggestion.messageId];
815 const rehydratedDesc = interpolate(unformattedMetaMessage, expectedSuggestion.data);
818 actualSuggestion.desc,
820 `${suggestionPrefix} Hydrated test desc "${rehydratedDesc}" does not match received desc "${actualSuggestion.desc}".`
825 !hasOwnProperty(expectedSuggestion, "data"),
826 `${suggestionPrefix} Test must specify 'messageId' if 'data' is used.`
830 if (hasOwnProperty(expectedSuggestion, "output")) {
831 const codeWithAppliedSuggestion = SourceCodeFixer.applyFixes(item.code, [actualSuggestion]).output;
833 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}"`);
840 // Message was an unexpected type
841 assert.fail(`Error should be a string, object, or RegExp, but found (${util.inspect(message)})`);
846 if (hasOwnProperty(item, "output")) {
847 if (item.output === null) {
851 "Expected no autofixes to be suggested"
854 assert.strictEqual(result.output, item.output, "Output is incorrect.");
860 "The rule fixed the code. Please add 'output' property."
864 // Rules that produce fixes must have `meta.fixable` property.
865 if (result.output !== item.code) {
867 hasOwnProperty(rule, "meta"),
868 "Fixable rules should export a `meta.fixable` property."
871 // Linter throws if a rule that produced a fix has `meta` but doesn't have `meta.fixable`.
874 assertASTDidntChange(result.beforeAST, result.afterAST);
878 * This creates a mocha test suite and pipes all supplied info through
879 * one of the templates above.
881 RuleTester.describe(ruleName, () => {
882 RuleTester.describe("valid", () => {
883 test.valid.forEach(valid => {
884 RuleTester.it(sanitize(typeof valid === "object" ? valid.code : valid), () => {
885 testValidTemplate(valid);
890 RuleTester.describe("invalid", () => {
891 test.invalid.forEach(invalid => {
892 RuleTester.it(sanitize(invalid.code), () => {
893 testInvalidTemplate(invalid);
901 RuleTester[DESCRIBE] = RuleTester[IT] = null;
903 module.exports = RuleTester;