From e14ccb9131ab0051893e1ff519969599f5543b9b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 17 Apr 2017 15:12:21 +0200 Subject: [PATCH] Handle `resolves` and `rejects` on `expect` --- .../src/rules/__tests__/valid-expect-test.js | 2 ++ packages/eslint-plugin-jest/src/rules/valid-expect.js | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin-jest/src/rules/__tests__/valid-expect-test.js b/packages/eslint-plugin-jest/src/rules/__tests__/valid-expect-test.js index 5c4ade9ce47b..9529fc37e5c4 100644 --- a/packages/eslint-plugin-jest/src/rules/__tests__/valid-expect-test.js +++ b/packages/eslint-plugin-jest/src/rules/__tests__/valid-expect-test.js @@ -23,6 +23,8 @@ ruleTester.run('valid-expect', rules['valid-expect'], { 'expect(true).toBeDefined();', 'expect([1, 2, 3]).toEqual([1, 2, 3]);', 'expect(undefined).not.toBeDefined();', + 'expect(Promise.resolve(2)).resolves.toBeDefined();', + 'expect(Promise.reject(2)).rejects.toBeDefined();', ], invalid: [ diff --git a/packages/eslint-plugin-jest/src/rules/valid-expect.js b/packages/eslint-plugin-jest/src/rules/valid-expect.js index 70da13320c2c..1253b6593746 100644 --- a/packages/eslint-plugin-jest/src/rules/valid-expect.js +++ b/packages/eslint-plugin-jest/src/rules/valid-expect.js @@ -16,6 +16,8 @@ import type {EslintContext, CallExpression} from './types'; +const expectProperties = ['not', 'resolves', 'rejects']; + module.exports = (context: EslintContext) => { return { CallExpression(node: CallExpression) { @@ -44,13 +46,13 @@ module.exports = (context: EslintContext) => { // a property is accessed, get the next node if (grandParentType === 'MemberExpression') { - // `not` is used, just get the next one - if (propertyName === 'not') { + // a modifier is used, just get the next one + if (expectProperties.indexOf(propertyName) > -1) { /* $FlowFixMe */ propertyName = node.parent.parent.property.name; grandParentType = node.parent.parent.parent.type; } else { - // only `not` is allowed + // only a few properties are allowed context.report({ message: `"${propertyName}" is not a valid property of expect.`, node,