Declare Nodejs global variables in “before” hook in WebdriverIO using TypeScript


I'm trying to port my JS WDIO project ot TypeScript

I have faced the issue when during the development TypeScript is not recognizing my Nodejs global variables declared in before hook in my WDIO config:

... let chai = require('chai'); ... before: async function (capabilities, specs) { //setting global variables global.foo = "bar" global.expect= chai.expect; global.helpers = require("../helpers/helpers"); // ... etc. // ... etc. },

I came across different SO topics but seems like they are not relevant since the approach here is bit different (because of the before hook)...

I even have manged to get it working at some point by creating global.d.ts with something inside like:

declare module NodeJS { interface Global { foo: string } }

But after this typescript stopped recognizing WDIO types lik browser, $ etc. And also with this approach I had to use global.foo in my tests meaning I have to change hundreds of occurrences of foo.

How can I migrate my project to TypeScript and continue using my global variables from the before hook?


You actually need to augment both the NodeJS.Global interface and global scope

Your global.d.ts will look like this

import chai from "chai"; // we need to wrap our global declarations in a `declare global` block // because importing chai makes this file a module. // declare global modifies the global scope from within a module declare global { const foo: string; const expect: typeof chai.expect; const helpers: typeof import("../helpers/helpers"); namespace NodeJS { interface Global { foo: typeof foo; expect: typeof expect; helpers: typeof helpers; } } }

Note that I declared the actual globals const because you only set them by referencing global in your before hook.



  • Google Analytics reports API - Insufficient Permission 403
  • JAXB unmarshalling returning Null
  • C++ throwing compilation error on sizeof() comparison in preprocessor #if
  • How to sort comma separated values in bash?
  • Need to apply different formulas based on the row number in the dataframe
  • Debugging Jest Tests in VS Code: Breakpoints Move
  • Is conversion to gray scale a necessary step in Image preprocessing?
  • How to remove shadow from JavaFX tabs?
  • How to bind from a ContentTemplate to the surrounding custom Control?
  • Dictionary in C++ using a Map with no values, only keys
  • WCF Error : 'It is likely that certificate 'my cert' may not have a private key that
  • making a bot respond to an image using discord.py
  • Generate image of GraphViz graph given dot text c++
  • Lending Club API with R
  • Java Processbuilder Stream to Python-Script
  • Hyperledger fabric's ChannelCreationPolicy
  • How to get Attachment value from “$File” Item? using C# (Lotus Notes)
  • Http Requests not getting routed to Https NodeJs
  • Need reference code for SMO in C# SQL Server 2008
  • GLConsumer is already attached to a context for new SurfaceTexture
  • How to smoothly connect two signals in matlab [closed]
  • MVC3 Extension for ValidatorMessage
  • How to create mirrored image effect with CSS single element
  • Authorize Attribute Authentication with Postman in Web Api
  • Update all WooCommerce product prices to 2 decimals in database
  • VBScript InputBox and Help Files
  • readmore button or show/hide for php
  • Disable account chooser FirebaseUI React
  • How convert html to BBcode in C#
  • reshape/remould data frame to create normalized bar chart and pie chart
  • How to handle div that is created dynamically in a table
  • ssh remote server login script
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • Passing value from popup window to parent form's TextBox
  • WPF custom control and direct content support
  • ReferenceError: TextEncoder is not defined
  • Create/delete users from text file using Bash script
  • JavaScript RegExp Replace
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?