diff --git a/README.md b/README.md index a210a01..c026362 100644 --- a/README.md +++ b/README.md @@ -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 | | 🌐 | | | | diff --git a/docs/rules/valid-expect-in-promise.md b/docs/rules/valid-expect-in-promise.md new file mode 100644 index 0000000..2b08aef --- /dev/null +++ b/docs/rules/valid-expect-in-promise.md @@ -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. + + + +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]); +}); + +``` \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index a648614..584a13e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -128,7 +128,7 @@ const allRules = { [preferComparisonMatcherName]: 'warn', [preferToContainName]: 'warn', [preferExpectAssertionsName]: 'warn', - [usePreferTobe]: 'warn', + [usePreferToBe]: 'warn', [paddingAroundAfterAllBlocksName]: 'warn', [paddingAroundAfterEachBlocksName]: 'warn', [paddingAroundAllName]: 'warn', @@ -172,7 +172,7 @@ const plugin = { [noConditionalTests]: noConditionalTest, [expectedExpect]: expectExpect, [useConsistentTestIt]: consistentTestIt, - [usePreferTobe]: preferToBe, + [usePreferToBe]: preferToBe, [noHooksName]: noHooks, [noRestrictedViMethodsName]: noRestrictedViMethods, [useConsistentTestFilename]: consistentTestFilename, diff --git a/src/rules/valid-expect.ts b/src/rules/valid-expect.ts index 1931b89..9fa1e39 100644 --- a/src/rules/valid-expect.ts +++ b/src/rules/valid-expect.ts @@ -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