diff --git a/protractor/index.js b/protractor/index.js index 02a3f16..2d24e4b 100644 --- a/protractor/index.js +++ b/protractor/index.js @@ -849,6 +849,18 @@ module.exports = function transformer(file, api) { )).replaceWith((path) => { j(path).closest(j.FunctionExpression).replaceWith(makeAsync) j(path).closest(j.BlockStatement).replaceWith(makeAsync) + j(path).closest(j.MethodDefinition, { + key: { name: 'constructor' } + }).forEach((p) => { + throw new TransformError('' + + `With "this.${path.value.property.name}" you are ` + + 'trying to access an element within a constructor. Given that it ' + + 'is not possible to run asynchronous code in this context, it ' + + 'is advised to move this call into a method or getter function.', + path.value, + file + ) + }) return j.awaitExpression(path.value) }) diff --git a/protractor/utils.js b/protractor/utils.js index f04c2df..6dab9b3 100644 --- a/protractor/utils.js +++ b/protractor/utils.js @@ -481,11 +481,14 @@ function sanitizeAsyncCalls (j, root) { function makeAsync ({ value, parentPath }) { if ( - parentPath.value.key && + parentPath.parentPath.value.kind === 'constructor' || ( - parentPath.value.kind === 'get' || - parentPath.value.key.name === 'constructor' || - parentPath.value.key.name.startsWith('async ') + parentPath.value.key && + ( + parentPath.value.kind === 'get' || + parentPath.value.key.name === 'constructor' || + parentPath.value.key.name.startsWith('async ') + ) ) ) { return value diff --git a/test/__fixtures__/protractor/source/failing_constructor.js b/test/__fixtures__/protractor/source/failing_constructor.js new file mode 100644 index 0000000..7cd8bfb --- /dev/null +++ b/test/__fixtures__/protractor/source/failing_constructor.js @@ -0,0 +1,6 @@ +class FriendsPage { + constructor() { + this.binding = $('h2.ng-binding') + this.pageLoaded = this.isClickable(this.binding); + } +} diff --git a/test/runner.js b/test/runner.js index 8c674b5..244159c 100644 --- a/test/runner.js +++ b/test/runner.js @@ -24,7 +24,8 @@ const frameworkTests = { ['./failing_submit.js'], ['./failing_clone.js'], ['./failing_column.js'], - ['./failing_anythingProtractor.js'] + ['./failing_anythingProtractor.js'], + ['./failing_constructor.js'] ], v7: [ ['./spec.js', './spec.js'],