-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create
prefer-expect-resolves
rule
- Loading branch information
Showing
6 changed files
with
188 additions
and
43 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,53 @@ | ||
# Prefer `await expect(...).resolves` over `expect(await ...)` syntax (`prefer-expect-resolves`) | ||
|
||
When working with promises, there are two primary ways you can test the resolved | ||
value: | ||
|
||
1. use the `resolve` modifier on `expect` | ||
(`await expect(...).resolves.<matcher>` style) | ||
2. `await` the promise and assert against its result | ||
(`expect(await ...).<matcher>` style) | ||
|
||
While the second style is arguably less dependent on `jest`, if the promise | ||
rejects it will be treated as a general error, resulting in less predictable | ||
behaviour and output from `jest`. | ||
|
||
Additionally, favoring the first style ensures consistency with its `rejects` | ||
counterpart, as there is no way of "awaiting" a rejection. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if an `await` is done within an `expect`, and | ||
recommends using `resolves` instead. | ||
|
||
Examples of **incorrect** code for this rule | ||
|
||
```js | ||
it('passes', async () => { | ||
expect(await someValue()).toBe(true); | ||
}); | ||
|
||
it('is true', async () => { | ||
const myPromise = Promise.resolve(true); | ||
|
||
expect(await myPromise).toBe(true); | ||
}); | ||
``` | ||
|
||
Examples of **correct** code for this rule | ||
|
||
```js | ||
it('passes', async () => { | ||
await expect(someValue()).resolves.toBe(true); | ||
}); | ||
|
||
it('is true', async () => { | ||
const myPromise = Promise.resolve(true); | ||
|
||
await expect(myPromise).resolves.toBe(true); | ||
}); | ||
|
||
it('errors', async () => { | ||
await expect(Promise.rejects('oh noes!')).rejects.toThrow('oh noes!'); | ||
}); | ||
``` |
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,53 @@ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import dedent from 'dedent'; | ||
import resolveFrom from 'resolve-from'; | ||
import rule from '../prefer-expect-resolves'; | ||
|
||
const ruleTester = new TSESLint.RuleTester({ | ||
parser: resolveFrom(require.resolve('eslint'), 'espree'), | ||
parserOptions: { | ||
ecmaVersion: 2017, | ||
}, | ||
}); | ||
|
||
ruleTester.run('prefer-expect-resolves', rule, { | ||
valid: [ | ||
dedent` | ||
it('passes', async () => { | ||
await expect(someValue()).resolves.toBe(true); | ||
}); | ||
`, | ||
dedent` | ||
it('is true', async () => { | ||
const myPromise = Promise.resolve(true); | ||
await expect(myPromise).resolves.toBe(true); | ||
}); | ||
`, | ||
dedent` | ||
it('errors', async () => { | ||
await expect(Promise.rejects('oh noes!')).rejects.toThrow('oh noes!'); | ||
}); | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: dedent` | ||
it('passes', async () => { | ||
expect(await someValue()).toBe(true); | ||
}); | ||
`, | ||
errors: [{ endColumn: 27, column: 10, messageId: 'expectResolves' }], | ||
}, | ||
{ | ||
code: dedent` | ||
it('is true', async () => { | ||
const myPromise = Promise.resolve(true); | ||
expect(await myPromise).toBe(true); | ||
}); | ||
`, | ||
errors: [{ endColumn: 25, column: 10, 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,37 @@ | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import { createRule, isExpectCall } from './utils'; | ||
|
||
export default createRule({ | ||
name: __filename, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: | ||
'Prefer `await expect(...).resolves` over `expect(await ...)` syntax', | ||
recommended: false, | ||
}, | ||
messages: { | ||
expectResolves: 'Use `await expect(...).resolves instead.', | ||
}, | ||
schema: [], | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [], | ||
create: context => ({ | ||
CallExpression(node: TSESTree.CallExpression) { | ||
if ( | ||
isExpectCall(node) && | ||
node.arguments.length && | ||
node.arguments[0].type === AST_NODE_TYPES.AwaitExpression | ||
) { | ||
context.report({ | ||
node: node.arguments[0], | ||
messageId: 'expectResolves', | ||
}); | ||
} | ||
}, | ||
}), | ||
}); |