Skip to content

Commit

Permalink
fix: do not validate await expressions for valid_expect_in_promise
Browse files Browse the repository at this point in the history
  • Loading branch information
tushardhole committed Jan 10, 2018
1 parent ab0f43c commit da393e0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
23 changes: 16 additions & 7 deletions rules/__tests__/valid_expect_in_promise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
const RuleTester = require('eslint').RuleTester;
const rules = require('../..').rules;

const ruleTester = new RuleTester();
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 8,
},
});

const expectedMsg =
'Promise should be returned to test its fulfillment or rejection';

Expand All @@ -20,7 +25,6 @@ ruleTester.run('valid-expect-in-promise', rules['valid-expect-in-promise'], {
message: expectedMsg,
},
],
parserOptions: { ecmaVersion: 6 },
},
{
code:
Expand Down Expand Up @@ -104,11 +108,7 @@ ruleTester.run('valid-expect-in-promise', rules['valid-expect-in-promise'], {
],

valid: [
{
code:
'it("it1", () => { return somePromise.then(() => {expect(someThing).toEqual(true)})})',
parserOptions: { sourceType: 'module' },
},
'it("it1", () => { return somePromise.then(() => {expect(someThing).toEqual(true)})})',

'it("it1", function() { return somePromise.catch(' +
'function() {expect(someThing).toEqual(true)})})',
Expand All @@ -131,5 +131,14 @@ ruleTester.run('valid-expect-in-promise', rules['valid-expect-in-promise'], {
'function() { /*rejection*/ expect(someThing).toEqual(true)})})',

'it("it1", function() { return somePromise.then()})',

'it("it1", async () => { await Promise.resolve().then(' +
'function() {expect(someThing).toEqual(true)})})',

'it("it1", async () => ' +
'{ await somePromise.then(() => {expect(someThing).toEqual(true)})})',

'it("it1", async () => { await getSomeThing().getPromise().then(' +
'function() {expect(someThing).toEqual(true)})})',
],
});
10 changes: 6 additions & 4 deletions rules/valid_expect_in_promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const verifyExpectWithReturn = (argument, node, context) => {
if (
argument &&
isFunction(argument.type) &&
//$FlowFixMe
isBodyCallExpression(argument.body)
) {
const calleeInThenOrCatch = argument.body.body[0].expression.callee.object;
Expand All @@ -57,18 +56,21 @@ const verifyExpectWithReturn = (argument, node, context) => {
}
};

const isAwaitExpression = node => {
return node.parent.parent && node.parent.parent.type == 'AwaitExpression';
};

module.exports = context => {
return {
MemberExpression(node) {
if (
node.type == 'MemberExpression' &&
isThenOrCatch(node) &&
node.parent.type == 'CallExpression'
node.parent.type == 'CallExpression' &&
!isAwaitExpression(node)
) {
const parent = node.parent;
// $FlowFixMe
const arg1 = parent.arguments[0];
// $FlowFixMe
const arg2 = parent.arguments[1];

// then block can have two args, fulfillment & rejection
Expand Down

0 comments on commit da393e0

Please # to comment.