1 | /**
|
2 | * @fileoverview Mocha test wrapper
|
3 | * @author Ilya Volodin
|
4 | */
|
5 | ;
|
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 |
|
43 | const
|
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 |
|
52 | const ajv = require("../shared/ajv")({ strictDefaults: true });
|
53 |
|
54 | const 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 | */
|
111 | const testerDefaultConfig = { rules: {} };
|
112 | let defaultConfig = { rules: {} };
|
113 |
|
114 | /*
|
115 | * List every parameters possible on a test case that are not related to eslint
|
116 | * configuration
|
117 | */
|
118 | const RuleTesterParameters = [
|
119 | "code",
|
120 | "filename",
|
121 | "options",
|
122 | "errors",
|
123 | "output"
|
124 | ];
|
125 |
|
126 | /*
|
127 | * All allowed property names in error objects.
|
128 | */
|
129 | const errorObjectParameters = new Set([
|
130 | "message",
|
131 | "messageId",
|
132 | "data",
|
133 | "type",
|
134 | "line",
|
135 | "column",
|
136 | "endLine",
|
137 | "endColumn",
|
138 | "suggestions"
|
139 | ]);
|
140 | const friendlyErrorObjectParameterList = `[${[...errorObjectParameters].map(key => `'${key}'`).join(", ")}]`;
|
141 |
|
142 | /*
|
143 | * All allowed property names in suggestion objects.
|
144 | */
|
145 | const suggestionObjectParameters = new Set([
|
146 | "desc",
|
147 | "messageId",
|
148 | "data",
|
149 | "output"
|
150 | ]);
|
151 | const friendlySuggestionObjectParameterList = `[${[...suggestionObjectParameters].map(key => `'${key}'`).join(", ")}]`;
|
152 |
|
153 | const 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 | */
|
161 | function 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 | */
|
186 | function 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 | */
|
206 | function 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 | */
|
219 | function 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 | */
|
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"));
|
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 | */
|
256 | function 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
|
282 | const DESCRIBE = Symbol("describe");
|
283 | const 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 | */
|
292 | function 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 | */
|
310 | function describeDefaultHandler(text, method) {
|
311 | return method.call(this);
|
312 | }
|
313 |
|
314 | class 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 (!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 (!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 | if (Array.isArray(item.errors) && item.errors.length === 0) {
|
648 | assert.fail("Invalid cases must have at least one error");
|
649 | }
|
650 |
|
651 | const ruleHasMetaMessages = hasOwnProperty(rule, "meta") && hasOwnProperty(rule.meta, "messages");
|
652 | const friendlyIDList = ruleHasMetaMessages ? `[${Object.keys(rule.meta.messages).map(key => `'${key}'`).join(", ")}]` : null;
|
653 |
|
654 | const result = runRuleForItem(item);
|
655 | const messages = result.messages;
|
656 |
|
657 | if (typeof item.errors === "number") {
|
658 |
|
659 | if (item.errors === 0) {
|
660 | assert.fail("Invalid cases must have 'error' value greater than 0");
|
661 | }
|
662 |
|
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)));
|
665 | } else {
|
666 | assert.strictEqual(
|
667 | messages.length, item.errors.length,
|
668 | util.format(
|
669 | "Should have %d error%s but had %d: %s",
|
670 | item.errors.length, item.errors.length === 1 ? "" : "s", messages.length, util.inspect(messages)
|
671 | )
|
672 | );
|
673 |
|
674 | const hasMessageOfThisRule = messages.some(m => m.ruleId === ruleName);
|
675 |
|
676 | for (let i = 0, l = item.errors.length; i < l; i++) {
|
677 | const error = item.errors[i];
|
678 | const message = messages[i];
|
679 |
|
680 | assert(hasMessageOfThisRule, "Error rule name should be the same as the name of the rule being tested");
|
681 |
|
682 | if (typeof error === "string" || error instanceof RegExp) {
|
683 |
|
684 | // Just an error message.
|
685 | assertMessageMatches(message.message, error);
|
686 | } else if (typeof error === "object" && error !== null) {
|
687 |
|
688 | /*
|
689 | * Error object.
|
690 | * This may have a message, messageId, data, node type, line, and/or
|
691 | * column.
|
692 | */
|
693 |
|
694 | Object.keys(error).forEach(propertyName => {
|
695 | assert.ok(
|
696 | errorObjectParameters.has(propertyName),
|
697 | `Invalid error property name '${propertyName}'. Expected one of ${friendlyErrorObjectParameterList}.`
|
698 | );
|
699 | });
|
700 |
|
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")) {
|
706 | assert.ok(
|
707 | ruleHasMetaMessages,
|
708 | "Error can not use 'messageId' if rule under test doesn't define 'meta.messages'."
|
709 | );
|
710 | if (!hasOwnProperty(rule.meta.messages, error.messageId)) {
|
711 | assert(false, `Invalid messageId '${error.messageId}'. Expected one of ${friendlyIDList}.`);
|
712 | }
|
713 | assert.strictEqual(
|
714 | message.messageId,
|
715 | error.messageId,
|
716 | `messageId '${message.messageId}' does not match expected messageId '${error.messageId}'.`
|
717 | );
|
718 | if (hasOwnProperty(error, "data")) {
|
719 |
|
720 | /*
|
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.
|
724 | */
|
725 | const unformattedOriginalMessage = rule.meta.messages[error.messageId];
|
726 | const rehydratedMessage = interpolate(unformattedOriginalMessage, error.data);
|
727 |
|
728 | assert.strictEqual(
|
729 | message.message,
|
730 | rehydratedMessage,
|
731 | `Hydrated message "${rehydratedMessage}" does not match "${message.message}"`
|
732 | );
|
733 | }
|
734 | }
|
735 |
|
736 | assert.ok(
|
737 | hasOwnProperty(error, "data") ? hasOwnProperty(error, "messageId") : true,
|
738 | "Error must specify 'messageId' if 'data' is used."
|
739 | );
|
740 |
|
741 | if (error.type) {
|
742 | assert.strictEqual(message.nodeType, error.type, `Error type should be ${error.type}, found ${message.nodeType}`);
|
743 | }
|
744 |
|
745 | if (hasOwnProperty(error, "line")) {
|
746 | assert.strictEqual(message.line, error.line, `Error line should be ${error.line}`);
|
747 | }
|
748 |
|
749 | if (hasOwnProperty(error, "column")) {
|
750 | assert.strictEqual(message.column, error.column, `Error column should be ${error.column}`);
|
751 | }
|
752 |
|
753 | if (hasOwnProperty(error, "endLine")) {
|
754 | assert.strictEqual(message.endLine, error.endLine, `Error endLine should be ${error.endLine}`);
|
755 | }
|
756 |
|
757 | if (hasOwnProperty(error, "endColumn")) {
|
758 | assert.strictEqual(message.endColumn, error.endColumn, `Error endColumn should be ${error.endColumn}`);
|
759 | }
|
760 |
|
761 | if (hasOwnProperty(error, "suggestions")) {
|
762 |
|
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}"`);
|
767 | }
|
768 | } else {
|
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`);
|
771 |
|
772 | error.suggestions.forEach((expectedSuggestion, index) => {
|
773 | assert.ok(
|
774 | typeof expectedSuggestion === "object" && expectedSuggestion !== null,
|
775 | "Test suggestion in 'suggestions' array must be an object."
|
776 | );
|
777 | Object.keys(expectedSuggestion).forEach(propertyName => {
|
778 | assert.ok(
|
779 | suggestionObjectParameters.has(propertyName),
|
780 | `Invalid suggestion property name '${propertyName}'. Expected one of ${friendlySuggestionObjectParameterList}.`
|
781 | );
|
782 | });
|
783 |
|
784 | const actualSuggestion = message.suggestions[index];
|
785 | const suggestionPrefix = `Error Suggestion at index ${index} :`;
|
786 |
|
787 | if (hasOwnProperty(expectedSuggestion, "desc")) {
|
788 | assert.ok(
|
789 | !hasOwnProperty(expectedSuggestion, "data"),
|
790 | `${suggestionPrefix} Test should not specify both 'desc' and 'data'.`
|
791 | );
|
792 | assert.strictEqual(
|
793 | actualSuggestion.desc,
|
794 | expectedSuggestion.desc,
|
795 | `${suggestionPrefix} desc should be "${expectedSuggestion.desc}" but got "${actualSuggestion.desc}" instead.`
|
796 | );
|
797 | }
|
798 |
|
799 | if (hasOwnProperty(expectedSuggestion, "messageId")) {
|
800 | assert.ok(
|
801 | ruleHasMetaMessages,
|
802 | `${suggestionPrefix} Test can not use 'messageId' if rule under test doesn't define 'meta.messages'.`
|
803 | );
|
804 | assert.ok(
|
805 | hasOwnProperty(rule.meta.messages, expectedSuggestion.messageId),
|
806 | `${suggestionPrefix} Test has invalid messageId '${expectedSuggestion.messageId}', the rule under test allows only one of ${friendlyIDList}.`
|
807 | );
|
808 | assert.strictEqual(
|
809 | actualSuggestion.messageId,
|
810 | expectedSuggestion.messageId,
|
811 | `${suggestionPrefix} messageId should be '${expectedSuggestion.messageId}' but got '${actualSuggestion.messageId}' instead.`
|
812 | );
|
813 | if (hasOwnProperty(expectedSuggestion, "data")) {
|
814 | const unformattedMetaMessage = rule.meta.messages[expectedSuggestion.messageId];
|
815 | const rehydratedDesc = interpolate(unformattedMetaMessage, expectedSuggestion.data);
|
816 |
|
817 | assert.strictEqual(
|
818 | actualSuggestion.desc,
|
819 | rehydratedDesc,
|
820 | `${suggestionPrefix} Hydrated test desc "${rehydratedDesc}" does not match received desc "${actualSuggestion.desc}".`
|
821 | );
|
822 | }
|
823 | } else {
|
824 | assert.ok(
|
825 | !hasOwnProperty(expectedSuggestion, "data"),
|
826 | `${suggestionPrefix} Test must specify 'messageId' if 'data' is used.`
|
827 | );
|
828 | }
|
829 |
|
830 | if (hasOwnProperty(expectedSuggestion, "output")) {
|
831 | const codeWithAppliedSuggestion = SourceCodeFixer.applyFixes(item.code, [actualSuggestion]).output;
|
832 |
|
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}"`);
|
834 | }
|
835 | });
|
836 | }
|
837 | }
|
838 | } else {
|
839 |
|
840 | // Message was an unexpected type
|
841 | assert.fail(`Error should be a string, object, or RegExp, but found (${util.inspect(message)})`);
|
842 | }
|
843 | }
|
844 | }
|
845 |
|
846 | if (hasOwnProperty(item, "output")) {
|
847 | if (item.output === null) {
|
848 | assert.strictEqual(
|
849 | result.output,
|
850 | item.code,
|
851 | "Expected no autofixes to be suggested"
|
852 | );
|
853 | } else {
|
854 | assert.strictEqual(result.output, item.output, "Output is incorrect.");
|
855 | }
|
856 | } else {
|
857 | assert.strictEqual(
|
858 | result.output,
|
859 | item.code,
|
860 | "The rule fixed the code. Please add 'output' property."
|
861 | );
|
862 | }
|
863 |
|
864 | // Rules that produce fixes must have `meta.fixable` property.
|
865 | if (result.output !== item.code) {
|
866 | assert.ok(
|
867 | hasOwnProperty(rule, "meta"),
|
868 | "Fixable rules should export a `meta.fixable` property."
|
869 | );
|
870 |
|
871 | // Linter throws if a rule that produced a fix has `meta` but doesn't have `meta.fixable`.
|
872 | }
|
873 |
|
874 | assertASTDidntChange(result.beforeAST, result.afterAST);
|
875 | }
|
876 |
|
877 | /*
|
878 | * This creates a mocha test suite and pipes all supplied info through
|
879 | * one of the templates above.
|
880 | */
|
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);
|
886 | });
|
887 | });
|
888 | });
|
889 |
|
890 | RuleTester.describe("invalid", () => {
|
891 | test.invalid.forEach(invalid => {
|
892 | RuleTester.it(sanitize(invalid.code), () => {
|
893 | testInvalidTemplate(invalid);
|
894 | });
|
895 | });
|
896 | });
|
897 | });
|
898 | }
|
899 | }
|
900 |
|
901 | RuleTester[DESCRIBE] = RuleTester[IT] = null;
|
902 |
|
903 | module.exports = RuleTester;
|