Skip to content

Commit

Permalink
docs: Add Documentation for valid-expect-in-promise Rule (#581)
Browse files Browse the repository at this point in the history
* docs: valid-expect-in-promise

* docs: update README

* refactor: typo and remove
  • Loading branch information
y-hsgw authored Dec 2, 2024
1 parent feb04a5 commit 6ad414f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export default [
| [valid-describe-callback](docs/rules/valid-describe-callback.md) | enforce valid describe callback || 🌐 | | | |
| [valid-expect](docs/rules/valid-expect.md) | enforce valid `expect()` usage || 🌐 | 🔧 | | |
| [valid-title](docs/rules/valid-title.md) | enforce valid titles || 🌐 | 🔧 | | |
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | require promises that have expectations in their chain to be valid | | 🌐 | | | |

<!-- end auto-generated rules list -->

Expand Down
51 changes: 51 additions & 0 deletions docs/rules/valid-expect-in-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Require promises that have expectations in their chain to be valid (`vitest/valid-expect-in-promise`)

⚠️ This rule _warns_ in the 🌐 `all` config.

<!-- end auto-generated rule header -->

This rule flags any promises within the body of a test that include expectations that have either not been returned or awaited.

The following patterns is considered warning:

```js
test('promise test', async () => {
something().then((value) => {
expect(value).toBe('red');
});
});

test('promises test', () => {
const onePromise = something().then((value) => {
expect(value).toBe('red');
});
const twoPromise = something().then((value) => {
expect(value).toBe('blue');
});

return Promise.any([onePromise, twoPromise]);
});
```

The following pattern is not warning:

```js

test('promise test', async () => {
await something().then((value) => {
expect(value).toBe('red');
});
});

test('promises test', () => {
const onePromise = something().then((value) => {
expect(value).toBe('red');
});
const twoPromise = something().then((value) => {
expect(value).toBe('blue');
});

return Promise.all([onePromise, twoPromise]);
});

```
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import noFocusedTests, { RULE_NAME as noFocusedTestsName } from './rules/no-focu
import noConditionalTest, { RULE_NAME as noConditionalTests } from './rules/no-conditional-tests'
import expectExpect, { RULE_NAME as expectedExpect } from './rules/expect-expect'
import consistentTestIt, { RULE_NAME as useConsistentTestIt } from './rules/consistent-test-it'
import preferToBe, { RULE_NAME as usePreferTobe } from './rules/prefer-to-be'
import preferToBe, { RULE_NAME as usePreferToBe } from './rules/prefer-to-be'
import noHooks, { RULE_NAME as noHooksName } from './rules/no-hooks'
import noRestrictedViMethods, { RULE_NAME as noRestrictedViMethodsName } from './rules/no-restricted-vi-methods'
import consistentTestFilename, { RULE_NAME as useConsistentTestFilename } from './rules/consistent-test-filename'
Expand Down Expand Up @@ -128,7 +128,7 @@ const allRules = {
[preferComparisonMatcherName]: 'warn',
[preferToContainName]: 'warn',
[preferExpectAssertionsName]: 'warn',
[usePreferTobe]: 'warn',
[usePreferToBe]: 'warn',
[paddingAroundAfterAllBlocksName]: 'warn',
[paddingAroundAfterEachBlocksName]: 'warn',
[paddingAroundAllName]: 'warn',
Expand Down Expand Up @@ -172,7 +172,7 @@ const plugin = {
[noConditionalTests]: noConditionalTest,
[expectedExpect]: expectExpect,
[useConsistentTestIt]: consistentTestIt,
[usePreferTobe]: preferToBe,
[usePreferToBe]: preferToBe,
[noHooksName]: noHooks,
[noRestrictedViMethodsName]: noRestrictedViMethods,
[useConsistentTestFilename]: consistentTestFilename,
Expand Down
1 change: 0 additions & 1 deletion src/rules/valid-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ export default createEslintRule<[

const isParentArrayExpression
= parentNode.parent.type === AST_NODE_TYPES.ArrayExpression
const orReturned = alwaysAwait ? '' : ' or returned'

const targetNode = getParentIfThenified(parentNode)
const finalNode = findPromiseCallExpressionNode(targetNode) || targetNode
Expand Down

0 comments on commit 6ad414f

Please sign in to comment.