0

I am trying to get WebdriverIO configured to use Chai as a global variable and using TypeScript.

In the WebdriverIO documentation we see that we can make Chai expect a global variable by using the wdio.conf.js file, like so:

// wdio.conf.js
before: () => {
    require('expect-webdriverio');
    global.wdioExpect = global.expect;
    const chai = require('chai');
    global.expect = chai.expect;
}

this will give us the variable expect without having to have a line importing chai into each spec file. I can do this easily using Javascript.

But, I can't seem to get this working in TypeScript.

I have the following in my wdio.conf.js file in TypeScript:

const expect = require('chai').expect;

    jasmineNodeOpts: {
        requires: ['ts-node/register'],
        defaultTimeoutInterval: 60000,
        stopSpecOnExpectationFailure: true
    },

        before: () => {
            global.expect = expect;
        }

but that doesn't seem to work in my spec file. I have the following statement in my spec file: expect(true).to.eql(true); and it does not recognize the .to

error received from running the spec:

 (node:23708) UnhandledPromiseRejectionWarning: TSError: ⨯ Unable to compile TypeScript:
src/specs/sometest.test.ts(55,22): error TS2339: Property 'to' does not exist on type 'Matchers<boolean>'.

I have installed both chai and @types/chai packages

Here is my tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",                          
    "module": "commonjs",    
    "strict": true,
    "paths": {
      "*": [ "./*" ],
      "src/*": ["./src/*"]
    },
    "typeRoots": ["./node_modules/@types"],                       
    "types": ["node", "@wdio/sync", "@wdio/jasmine-framework", "chai"],                           
    "forceConsistentCasingInFileNames": true 
  },
  "include": [
    "./**/*.ts",
    "src/specs/*.ts", "./**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

Any clues on what else I should try? And also, can I attach or associate anything to global as in global.foo and be able to call foo anywhere in the spec file?

adbarads
  • 1,253
  • 2
  • 20
  • 43
  • `Matchers` is coming from `@types/jest` ([ref](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts#L715)), any chance you have that package installed? – superhawk610 Mar 25 '21 at 21:09
  • To have `global.expect` correctly typed, you'll need to extend the global namespace, see [this SO answer](https://stackoverflow.com/a/59626972/885098) for how to go about doing so. – superhawk610 Mar 25 '21 at 21:11

0 Answers0