Skip to content

Commit

Permalink
Add test-specific ruleset and disable prefer-arrow-callback for tests.
Browse files Browse the repository at this point in the history
Allow use of "as Type" syntax for type assertions in test code.

Dont force symbol descriptors in test code
  • Loading branch information
filmaj committed Aug 23, 2021
1 parent 84afc50 commit 7b6be5d
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ module.exports = {
// Require class properties and methods to explicitly use accessibility modifiers (public, private, protected)
'@typescript-eslint/explicit-member-accessibility': 'error',

// Forbids an object literal to appear in a type assertion expression unless its used as a parameter. This
// allows the typechecker to perform validation on the value as an assignment, instead of allowing the type
// assertion to always win.
// Forbids an object literal to appear in a type assertion expression unless its used as a parameter (we violate
// this rule for test code, to allow for looser property matching for objects - more in the test-specific rules
// section below). This allows the typechecker to perform validation on the value as an assignment, instead of
// allowing the type assertion to always win.
// Requires use of `as Type` instead of `<Type>` for type assertion. Consistency.
'@typescript-eslint/consistent-type-assertions': ['error', {
assertionStyle: 'as',
Expand Down Expand Up @@ -224,7 +225,7 @@ module.exports = {
},
{
'selector': 'objectLiteralProperty',
format: ['camelCase', 'snake_case', 'PascalCase']
format: ['camelCase', 'snake_case', 'PascalCase'],
},
{
selector: ['enumMember'],
Expand Down Expand Up @@ -256,6 +257,36 @@ module.exports = {

},
},
{
files: ['src/**/*.spec.ts'],
rules: {
// Test-specific rules
// ---
// Rules that only apply to Typescript _test_ source files

// With Mocha as a test framework, it is sometimes helpful to assign
// shared state to Mocha's Context object, for example in setup and
// teardown test methods. Assigning stub/mock objects to the Context
// object via `this` is a common pattern in Mocha. As such, using
// `function` over the the arrow notation binds `this` appropriately and
// should be used in tests. So: we turn off the prefer-arrow-callback
// rule.
// See https://github.com/slackapi/bolt-js/pull/1012#pullrequestreview-711232738
// for a case of arrow-vs-function syntax coming up for the team
'prefer-arrow-callback': 'off',

// Unlike non-test-code, where we require use of `as Type` instead of `<Type>` for type assertion,
// in test code using the looser `as Type` syntax leads to easier test writing, since only required
// properties must be adhered to using the `as Type` syntax.
'@typescript-eslint/consistent-type-assertions': ['error', {
assertionStyle: 'as',
objectLiteralTypeAssertions: 'allow',
}],
// In tests, don't force constructing a Symbol with a descriptor, as
// it's probably just for tests
'symbol-description': 'off',
},
},
],
};

Expand Down

0 comments on commit 7b6be5d

Please sign in to comment.