Skip to content

Commit

Permalink
Chore: add JSDoc types for RuleTester test cases (#12325)
Browse files Browse the repository at this point in the history
* Chore: add JSDoc types for RuleTester test cases

* Improve types

* Add descriptions for the types and move types to top of file

* Fix lint error, object -> Object

* Make `TestCaseError.message` optional

* Add property `env` to types

* Add property `env` to types

* Fix last commit

* Make `InvalidTestCase.errors` accept RegExp elements
  • Loading branch information
golopot authored and kaicataldo committed Jan 3, 2020
1 parent b23ad0d commit e4df7df
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion lib/rule-tester/rule-tester.js
Expand Up @@ -50,6 +50,52 @@ const

const ajv = require("../shared/ajv")({ strictDefaults: true });


//------------------------------------------------------------------------------
// Typedefs
//------------------------------------------------------------------------------

/**
* A test case that is expected to pass lint.
* @typedef {Object} ValidTestCase
* @property {string} code Code for the test case.
* @property {any[]} [options] Options for the test case.
* @property {{ [name: string]: any }} [settings] Settings for the test case.
* @property {string} [filename] The fake filename for the test case. Useful for rules that make assertion about filenames.
* @property {string} [parser] The absolute path for the parser.
* @property {{ [name: string]: any }} [parserOptions] Options for the parser.
* @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables.
* @property {{ [name: string]: boolean }} [env] Environments for the test case.
*/

/**
* A test case that is expected to fail lint.
* @typedef {Object} InvalidTestCase
* @property {string} code Code for the test case.
* @property {number | Array<TestCaseError | string | RegExp>} errors Expected errors.
* @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.
* @property {any[]} [options] Options for the test case.
* @property {{ [name: string]: any }} [settings] Settings for the test case.
* @property {string} [filename] The fake filename for the test case. Useful for rules that make assertion about filenames.
* @property {string} [parser] The absolute path for the parser.
* @property {{ [name: string]: any }} [parserOptions] Options for the parser.
* @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables.
* @property {{ [name: string]: boolean }} [env] Environments for the test case.
*/

/**
* A description of a reported error used in a rule tester test.
* @typedef {Object} TestCaseError
* @property {string | RegExp} [message] Message.
* @property {string} [messageId] Message ID.
* @property {string} [type] The type of the reported AST node.
* @property {{ [name: string]: string }} [data] The data used to fill the message template.
* @property {number} [line] The 1-based line number of the reported start location.
* @property {number} [column] The 1-based column number of the reported start location.
* @property {number} [endLine] The 1-based line number of the reported end location.
* @property {number} [endColumn] The 1-based column number of the reported end location.
*/

//------------------------------------------------------------------------------
// Private Members
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -273,7 +319,10 @@ class RuleTester {
* Adds a new rule test to execute.
* @param {string} ruleName The name of the rule to run.
* @param {Function} rule The rule to test.
* @param {Object} test The collection of tests to run.
* @param {{
* valid: (ValidTestCase | string)[],
* invalid: InvalidTestCase[]
* }} test The collection of tests to run.
* @returns {void}
*/
run(ruleName, rule, test) {
Expand Down

0 comments on commit e4df7df

Please sign in to comment.