-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new rule consistent-interface
- Loading branch information
Showing
6 changed files
with
154 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Enforces consistent use of mocha interfaces (`mocha/consistent-interface`) | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Mocha has several interfaces such as `BDD`, `TDD`, `Exports`, `QUnit` etc. Usually mocha works by injecting the variables and functions of the selected interface into the global scope. However when using the `Exports` interface one can import functions of any interface. This rule helps to enforce a consistent use of the same interface when using `Exports`. | ||
|
||
## Options | ||
|
||
Example of a custom rule configuration: | ||
|
||
```js | ||
rules: { | ||
"mocha/consistent-interface": ["error", { interface: 'BDD' }] | ||
}, | ||
``` | ||
|
||
where: | ||
|
||
- `interface` can be set to either `TDD` or `BDD` | ||
|
||
## When Not To Use It | ||
|
||
If you are not using the `Exports` interface then this rule doesn’t provide any value. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createMochaVisitors } from '../ast/mochaVisitors.js'; | ||
|
||
export const consistentInterfaceRule = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Enforces consistent use of mocha interfaces', | ||
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/consistent-interface.md' | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
interface: { | ||
type: 'string', | ||
enum: ['BDD', 'TDD'], | ||
default: 'BDD' | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
create(context) { | ||
const interfaceToUse = context.options[0].interface; | ||
|
||
return createMochaVisitors(context, { | ||
anyTestEntity(visitorContext) { | ||
if (visitorContext.interface !== interfaceToUse) { | ||
context.report({ | ||
node: visitorContext.node, | ||
message: `Unexpected use of ${visitorContext.interface} interface instead of ${interfaceToUse}` | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { RuleTester } from 'eslint'; | ||
import { consistentInterfaceRule } from '../../lib/rules/consistent-interface.js'; | ||
|
||
const ruleTester = new RuleTester({ languageOptions: { ecmaVersion: 2020, sourceType: 'module' } }); | ||
|
||
ruleTester.run('consistent-interface', consistentInterfaceRule, { | ||
valid: [ | ||
{ | ||
code: `describe('foo', () => { | ||
it('bar', () => {}); | ||
});`, | ||
options: [{ interface: 'BDD' }], | ||
settings: { mocha: { interface: 'BDD' } } | ||
}, | ||
{ | ||
code: `suite('foo', () => { | ||
test('bar', () => {}); | ||
});`, | ||
options: [{ interface: 'TDD' }], | ||
settings: { mocha: { interface: 'TDD' } } | ||
}, | ||
{ | ||
code: `import {suite, test} from 'mocha'; suite('foo', () => { | ||
test('bar', () => {}); | ||
});`, | ||
options: [{ interface: 'TDD' }], | ||
settings: { mocha: { interface: 'exports' } } | ||
}, | ||
{ | ||
code: `import {describe, it} from 'mocha'; describe('foo', () => { | ||
it('bar', () => {}); | ||
});`, | ||
options: [{ interface: 'BDD' }], | ||
settings: { mocha: { interface: 'exports' } } | ||
}, | ||
{ | ||
code: `import {describe as foo, it as bar} from 'mocha'; foo('foo', () => { | ||
bar('bar', () => {}); | ||
});`, | ||
options: [{ interface: 'BDD' }], | ||
settings: { mocha: { interface: 'exports' } } | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: `describe('foo', () => { | ||
test('bar', () => {}); | ||
});`, | ||
options: [{ interface: 'BDD' }], | ||
settings: { mocha: { interface: 'BDD' } }, | ||
errors: [{ line: 2, column: 17, message: 'Unexpected use of TDD interface instead of BDD' }] | ||
}, | ||
{ | ||
code: `describe('foo', () => { | ||
test('bar', () => {}); | ||
});`, | ||
options: [{ interface: 'TDD' }], | ||
settings: { mocha: { interface: 'TDD' } }, | ||
errors: [{ line: 1, column: 1, message: 'Unexpected use of BDD interface instead of TDD' }] | ||
}, | ||
{ | ||
code: `import {suite, test} from 'mocha'; suite('foo', () => { | ||
test('bar', () => {}); | ||
});`, | ||
options: [{ interface: 'BDD' }], | ||
settings: { mocha: { interface: 'exports' } }, | ||
errors: [ | ||
{ line: 1, column: 36, message: 'Unexpected use of TDD interface instead of BDD' }, | ||
{ line: 2, column: 17, message: 'Unexpected use of TDD interface instead of BDD' } | ||
] | ||
}, | ||
{ | ||
code: `import {describe, it} from 'mocha'; describe('foo', () => { | ||
it('bar', () => {}); | ||
});`, | ||
options: [{ interface: 'TDD' }], | ||
settings: { mocha: { interface: 'exports' } }, | ||
errors: [ | ||
{ line: 1, column: 37, message: 'Unexpected use of BDD interface instead of TDD' }, | ||
{ line: 2, column: 17, message: 'Unexpected use of BDD interface instead of TDD' } | ||
] | ||
} | ||
] | ||
}); |