0

We are currently updating our linting to match the Front End Developers' idea of linting. Mostly because they want to be able to look at and understand our testing code. We are all working in JavaScript. No big deal... or is it? We have found, in our "old way" of doing WDIO "Describes" and "Its" that we could simply use an unnamed function, and it simply worked. it('No Place Like Home', async function () { We have started updating to the arrow functions, which, on the surface, "look the same(ish)", but don't work the same. Our problem comes with the usage of "this"... We are using a method of "retry", and we set the number of retries in our config file. In some of the tests, we need to know if the current run is the first run, or the first retry. To do this, we grab this.wdioRetries which will tell us "0" for primary run, or "1" for first retry, or "2", for the second retry, etc. Wouldn't you know it? The "unnamed function" lets us use "this" without trouble. The linting gives us a warning about "no unnamed functions"... When we convert it to the "arrow function", we get "this.wdioRetries === undefined". "this" doesn't exist in the scope of the arrow function. But the linting LOVES the arrow function, even though the test now fails. We also, at times, need to override certain test values, like default timeout. Which we can't do without access to "this.test"...

So. The question is, how do we get "this" in an arrow function? Is there a way to pass it in? A way to call it in by another way (process, browser, driver...)? Some built in function to make a call (const thing = whatever.getThis())?

Or, is it futile? We should stay with the unnamed functions?

I've tried: it('No Place Like Home', async () => { it('No Place Like Home', async (this) => { it('No Place Like Home', async this => { I've tried simple functions, etc. The problem is the scope, which needs to be at the TEST level, not GLOBAL. Hoping someone else has blazed this trail before me and has found the solution.

  • `this` works differently in arrow functions than traditional functions. So you shouldn't do this conversion for functions that use `this`. – Barmar May 12 '23 at 17:00
  • The solution is simple - change the linting rules. You can do it for a subdirectory or otherwise just for the testing code wherever it lies. Trying to somehow force arrow functions to work not how they are described by the spec is [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – VLAZ May 12 '23 at 17:06
  • That's quite a first paragraph. It's not clear **why** you need `this`; is it required by whatever wdio is? Or is it because you've set up your tests that way? – Dave Newton May 12 '23 at 17:36
  • @DaveNewton testing libraries regularly use `this` to share test data and configuration. The setup might be done in a before/before each and some data set on `this` then each test can access it. – VLAZ May 12 '23 at 17:38
  • @VLAZ Hm, is there an advantage to that over using `let`s? I'd have to check but I'm not sure I've ever used `this` in Mocha or whatever that other JS testing lib is I've used. – Dave Newton May 12 '23 at 17:45
  • @VLAZ Just looked in one of our JS apps and another dev used `this` instead of `let`s so I guess I'm technically a liar now :D – Dave Newton May 12 '23 at 17:46
  • Advantage - you don't leak out internal data for some test code just to share it around. Also advantage: it's built into the test system. Also consider that you are not *required* to write all your code in the same file and thus have direct access to all the shared scope. You can also write your tests as functions elsewhere, and just import them. – VLAZ May 12 '23 at 17:57

0 Answers0