expect-webdriverio icon indicating copy to clipboard operation
expect-webdriverio copied to clipboard

Async matchers have incorrect typings

Open bad-white-cat opened this issue 3 years ago • 8 comments

Environment (please complete the following information):

  • WebdriverIO version: 7.7.4
  • Mode: WDIO Testrunner
  • If WDIO Testrunner, running sync/async: async
  • Node.js version: 14.17.1
  • NPM version: 6.14.13
  • Browser name and version: Chrome 91
  • Platform name and version: e.g. Windows 10
  • Additional wdio packages used (if applicable): "@wdio/mocha-framework": "^7.7.4"

Config of WebdriverIO By Default

Describe the bug Looks like there is a bug in typings in expects toHaveText(text: string | string[], options?: ExpectWebdriverIO.StringOptions): R Shouldn't it be like this toHaveText(text: string | string[], options?: ExpectWebdriverIO.StringOptions): Promise<R>

To Reproduce Steps to reproduce the behavior:

describe('sample spec', () => {
    it('should be logo', async () => {
        browser.url('https://webdriver.io/')
        expect(await $('p.hero__subtitle')).toHaveText('bla bla') //expect is definetely wrong, but the test passes 
    });
});

Expected behavior Test above should failed.

NOTE: I know that there is a workaround await expect($('p.hero__subtitle')).toHaveText('bla bla') //it should fail and it fails but I still think that there is a bug in typings

bad-white-cat avatar Jul 03 '21 13:07 bad-white-cat

Yes, I believe the types are not strict enough, thanks for raising the issue. Any contributions are appreicated.

christian-bromann avatar Jul 12 '21 09:07 christian-bromann

@christian-bromann I'd be happy to look into this. I'll investigate it a bit more to double check any unintended consequences but seems like a minor change to the matchers 👍 . Are you able to assign me to this issue?

TuxedoFish avatar Aug 23 '21 21:08 TuxedoFish

@TuxedoFish any progress? We should also ensure that matchers are properly types when someone uses Jasmine and expectAsync.

christian-bromann avatar Aug 31 '21 09:08 christian-bromann

@TuxedoFish any progress? We should also ensure that matchers are properly types when someone uses Jasmine and expectAsync.

I was struggling a little bit to test it and I was thinking of using a test repository and then linking the repository to test it out. Is there a test repository I can fork?

TuxedoFish avatar Sep 01 '21 06:09 TuxedoFish

You can pick one of the boilerplate projects or just bootstrap something via npx wdio ./test and pick TypeScript in the config wizard.

christian-bromann avatar Sep 01 '21 09:09 christian-bromann

Hmm okay so I have created two tests with npx wdio ./test for jasmine + mocha. After updating the types in expect-webdriverio.d.ts I linked the repo and ran the tests but still observed the bug behaviour,

So then I thought maybe it was something to do with the expect adapter after looking at this codesandbox so I added in the async keyword to the runExpect. I found something like this being done in a code sandbox here: https://codepen.io/chivesrs/pen/zYGEZvP.

export function runJasmineExpect(fn: (...args: any) => any): any {
    // 2nd and 3rd args are `util` and `customEqualityTesters` that are not used
    const context = getContext(this)
    return {
        async compare(...args: any[]): Promise<any> {
            const result = fn.apply({ ...context, isNot: false }, args)
            return jestResultToJasmine(result, false)
        },
        async negativeCompare(...args: any[]): Promise<any> {
            const result = fn.apply({ ...context, isNot: true }, args)
            return jestResultToJasmine(result, true)
        }
    }
}

But I am still seeing the same bug behaviour. I have double checked the linking as well.

I am a bit stuck but I can keep taking a look @christian-bromann

TuxedoFish avatar Sep 05 '21 05:09 TuxedoFish

Any progress on this? With adding typescript linting, I get an error that my await statement is for nothing, while it has to be there....

dietergeerts avatar Oct 26 '21 15:10 dietergeerts

@dietergeerts any contributions and help would be appreciated, thanks!

christian-bromann avatar Oct 26 '21 16:10 christian-bromann

I submitted a PR (#962) for this, assuming all we need to do is the naive change implied by the issue description: change the R return value to Promise<R> in the type file.

Hopefully that's enough to do the trick 🤞

menewman avatar Dec 02 '22 19:12 menewman

I have the same problem with the following configuration:

  • Npm Version: 8.3.2
  • Node Version: 16.13.2
  • wdio: 8.3.9 But the Problem only occurs when you don't use chained promises.

The example : No problem with:

const element= browser.$('#someselector');
await expect(element).toBeDisplayed(); // its ok

But problem with:

const element= await browser.$('#someselector');
await expect(element).toBeDisplayed(); // Not ok

m4hdyar avatar Mar 20 '23 10:03 m4hdyar

@m4hdyar thanks for verifying , can you provide a minimal reproducible example I can look into?

christian-bromann avatar Mar 20 '23 16:03 christian-bromann

@christian-bromann sure just use https://github.com/amiya-pattnaik/webdriverIO-with-mochaBDD and add these two lines of code somewhere in test

const element= await browser.$('#someselector');
await expect(element).toBeDisplayed(); // Not ok

You will then get 'await' has no effect on the type of this expression.ts(80007)

m4hdyar avatar Mar 31 '23 15:03 m4hdyar

Any support on this issue would be appreciated, thanks!

christian-bromann avatar Apr 01 '23 00:04 christian-bromann

@m4hdyar I tested on your boilerplate example and don't get any errors, see:

Screenshot 2023-07-15 at 14 31 56

This issue seems to be connected to v7. I am happy to merge any contributions that fix the issue for v7 but I don't think this still exists in v8. Happy to re-open if anyone can provide a reproducible example. Closing.

christian-bromann avatar Jul 15 '23 21:07 christian-bromann