Skip to content

Commit

Permalink
🏗 Add lint rule to enforce that e2e test expects are awaited (#21801
Browse files Browse the repository at this point in the history
)

* Add lint rule to enforce that e2e test `expects` are `await`ed

* Cleanup

* Add bad/good comment
  • Loading branch information
cvializ authored Apr 10, 2019
1 parent 680dd18 commit 17c865a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dist.3p
dist.tools
out
examples
firebase
third_party
test/coverage
**/*.extern.js
Expand Down
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
}
},
"rules": {
"amphtml-internal/await-expect": 2,
"amphtml-internal/closure-type-primitives": 2,
"amphtml-internal/dict-string-keys": 2,
"amphtml-internal/enforce-private-props": 2,
Expand Down Expand Up @@ -171,7 +172,7 @@
"files": [
"test/**/*.js",
"extensions/**/test/**/*.js",
"extensions/**/test-e2e/*.js",
"extensions/**/test-e2e/*.js",
"ads/**/test/**/*.js",
"testing/**/*.js"
],
Expand Down
69 changes: 69 additions & 0 deletions build-system/eslint-rules/await-expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2019 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

/**
* Forces `expect` calls to be preceded by `await` in end-to-end tests.
*
* Bad:
* expect(actual).to.equal(expected);
* Good:
* await expect(actual).to.equal(expected);
*/
module.exports = function(context) {
return {
CallExpression(node) {
const filename = context.getFilename();
if (!/test-e2e|\/test\/e2e\//.test(filename)) {
return;
}

const {callee} = node;
if (callee.type !== 'Identifier') {
return;
}

if (callee.name !== 'expect') {
return;
}

if (hasAwaitParent(node)) {
return;
}

context.report({
node,
message: '`expect` in end-to-end tests must use `await`.',
});
},
};
};

/**
* Returns true if the given espree AST node is a child of an `AwaitExpression`.
* @param {!Object} node
* @return {boolean}
*/
function hasAwaitParent(node) {
while (node) {
if (node.type == 'AwaitExpression') {
return true;
}
node = node.parent;
}
return false;
}

0 comments on commit 17c865a

Please sign in to comment.