Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rules): no-expect-resolves (#364)
- Loading branch information
Showing
5 changed files
with
96 additions
and
1 deletion.
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,29 @@ | ||
# Avoid using `expect().resolves` (no-expect-resolves) | ||
|
||
Jest allows you to test a promise resolve value using `await expect().resolves`. | ||
For consistency and readability this rule bans `expect().resolves` in favor of | ||
`expect(await promise)`. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `expect().resolves` is used. | ||
|
||
This rule is disabled by default. | ||
|
||
### Default configuration | ||
|
||
The following patterns is considered warning: | ||
|
||
```js | ||
test('some test', async () => { | ||
await expect(Promise.resolve(1)).resolves.toBe(1); | ||
}); | ||
``` | ||
|
||
The following pattern is not considered warning: | ||
|
||
```js | ||
test('some test', async () => { | ||
expect(await Promise.resolve(1)).toBe(1); | ||
}); | ||
``` |
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,24 @@ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import rule from '../no-expect-resolves'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2017, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-expect-resolves', rule, { | ||
valid: [ | ||
`test('some test', async () => { | ||
expect(await Promise.resolve(1)).toBe(1); | ||
});`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `test('some test', async () => { | ||
await expect(Promise.resolve(1)).resolves.toBe(1); | ||
});`, | ||
errors: [{ endColumn: 55, column: 47, messageId: 'expectResolves' }], | ||
}, | ||
], | ||
}); |
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,40 @@ | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import { createRule, isExpectCall } from './tsUtils'; | ||
|
||
function isIdentifierResolves(node: TSESTree.MemberExpression) { | ||
return ( | ||
node.property.type === AST_NODE_TYPES.Identifier && | ||
node.property.name === 'resolves' | ||
); | ||
} | ||
|
||
function isExpectResolves(node: TSESTree.MemberExpression) { | ||
return isExpectCall(node.object) && isIdentifierResolves(node); | ||
} | ||
|
||
export default createRule({ | ||
name: __filename, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Disallow expect.resolves', | ||
recommended: false, | ||
}, | ||
messages: { | ||
expectResolves: 'Use `expect(await promise)` instead.', | ||
}, | ||
schema: [], | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [], | ||
create: context => ({ | ||
MemberExpression(node) { | ||
if (isExpectResolves(node)) { | ||
context.report({ node: node.property, messageId: 'expectResolves' }); | ||
} | ||
}, | ||
}), | ||
}); |