Skip to content

Rule: disallow boilerplate test options #4

Open
@not-an-aardvark

Description

@not-an-aardvark

Disallows repeating boilerplate options like { parserOptions: { ecmaVersion: 6 } } in rule tests, in favor of specifying defaults in the RuleTester constructor instead.

Incorrect code for this rule:

/* eslint eslint-plugin/no-boilerplate-test-options: error */

const ruleTester = new RuleTester();
ruleTester.run("foo", rule, {
  valid: [
    {
      code: "foo", 
      parserOptions: { ecmaVersion: 6 }
    }
  ],
  invalid: [
    {
      code: "bar", 
      parserOptions: { ecmaVersion: 6 },
      errors: ["baz"]
    }
  ]
});

Correct code for this rule:

/* eslint eslint-plugin/no-boilerplate-test-options: error */

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
ruleTester.run("foo", rule, {
  valid: [
    {
      code: "foo",
    }
  ],
  invalid: [
    {
      code: "bar", 
      errors: ["baz"]
    }
  ]
});

There are a few open design questions:

  • How many identical uses of parserOptions should the rule need to detect in order to report an error? (2? Some percentage of the total number of test cases? All of the test cases?)
  • Should the rule reason about maximum ecmaVersion? (For example, if some tests have ecmaVersion: 6 and others have ecmaVersion: 7, should the rule conclude that the user can put ecmaVersion: 7 in the constructor?)
  • Aside from parserOptions, what other duplicate options should the rule report? Should it report RuleTester-specific options such as errors? Should it report all configuration options (e.g. env)?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions