-1

In my project, I don't wanna use construction function () {}, instead of that I wanna use () => {} or () => (). Code below. In first example I can see this, in second case this undefined. So I wonder, Is it possible way to make () => {} see this? I tried to use () => {}.bind(this) did not work and ({ context: this }) => {} did not work either.

// Example 1
describe('Currency converter', () => {
  it('should be shown on the page', function () {
    console.log(this); // { bla: ..., blabla: ... }
    return this.browser
      .url('/')
      .keys(['currence', '\uE007'])
      .isExisting('.converter-form')
      .then((exists) => {
        assert.ok(exists, 'currence did not show');
      });
  });
});

// Example 2
describe('Currency converter', () => {
  it('should be shown on the page', () => {
    console.log(this) // undefinded
    return this.browser
      .url('/')
      .keys(['currence', '\uE007'])
      .isExisting('.converter-form')
      .then((exists) => {
        assert.ok(exists, 'currence did not show');
      });
  });
});
Rami Chasygov
  • 2,714
  • 7
  • 25
  • 37

1 Answers1

1

No! Arrow functions don't have a their own value for this, even if you try to bind them. You have to use the long form.

Ben West
  • 4,398
  • 1
  • 16
  • 16