Open bmish opened 5 years ago
If all test cases specify the same parserOptions, then parserOptions should be set globally in the test file when creating the RuleTester to simplify the individual test cases.
parserOptions
RuleTester
Before:
const rule = require('../../../lib/rules/my-rule'); const RuleTester = require('eslint').RuleTester; const eslintTester = new RuleTester(); eslintTester.run('my-rule', rule, { valid: [ { code: 'someFunction()', parserOptions: { ecmaVersion: 6, sourceType: 'module' }, }, { code: 'otherFunction()', parserOptions: { ecmaVersion: 6, sourceType: 'module' }, }, ], invalid: [ { code: 'finalFunction()', parserOptions: { ecmaVersion: 6, sourceType: 'module' }, output: null, errors: [ { message: 'Some error message', type: 'CallExpression', }, ], }, ], });
After:
const rule = require('../../../lib/rules/my-rule'); const RuleTester = require('eslint').RuleTester; const eslintTester = new RuleTester({ parserOptions: { ecmaVersion: 6, sourceType: 'module' }, }); eslintTester.run('my-rule', rule, { valid: ['someFunction()', 'otherFunction()'], invalid: [ { code: 'finalFunction()', output: null, errors: [ { message: 'Some error message', type: 'CallExpression', }, ], }, ], });
related: https://github.com/eslint-community/eslint-plugin-eslint-plugin/issues/4
It's fine to use the global option when it's the same in all tests. The question is what to do if some options are different.
If all test cases specify the same
parserOptions
, thenparserOptions
should be set globally in the test file when creating theRuleTester
to simplify the individual test cases.Before:
After: