Is it possible for TS to recognize global variables set in Protractor configuration file?

Encountering an issue when attempting to add a global variable in the onPrepare method within the Protractor config. Typescript throws an error "Cannot find name '____'" when trying to use it in a test file.

Here is how I define the global variable in my Protractor config:

protractorConfig.js

onPrepare: function () {
  global.logger = log4js.getLogger( 'log' );
},

And here is how I am using the global variable:

homepageTests.js

  it( '1@tests homepage', function () {
       logger.info( 'password for application: ' + pswd );
  } );

According to a Stack Overflow post, setting global variables in the onPrepare method is a valid approach in Protractor.

Answer №1

When encountering this error, it is due to TypeScript not recognizing the existence of globals and any additions made within that scope. To resolve this issue, you will need to declare globals explicitly or utilize type assertions.

it('1@tests homepage', function () {
   (global as any).logger.info('password for application: ' + pswd);
});

Instead of using any, consider replacing it with a more specific type.

An alternative approach is to create a file named globals.d.ts next to your tsconfig.json file specifically for end-to-end tests, and include the following line:

declare const logger: any; // Replace 'any' with actual type if available

This method allows you to freely access logger in your code.

it('1@tests homepage', function () {
   logger.info('password for application: ' + pswd);
});

For additional solutions, refer to similar discussions on Stack Overflow here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Testing Angular Singleton Service with Unit Tests

I have been tackling a unit test in Angular related to a singleton service. Specifically, I have a UserService that is declared as a singleton in the root module of the application. My goal is to create a unit test that verifies whether the instance of U ...

Tips for dynamically expanding a generic interface depending on the keys of its parent object

Consider the following definitions: interface A { id?: string; name?: string; } interface BaseLabel<B extends Parent, Parent extends A> { keyStr?: keyof B & string; } interface RequiredLabel<B extends Parent, Parent extends A> ...

Update information in a component without having to refresh it when navigating to a different route

I am currently facing a challenge with my components B and A. Component B has a post method that inserts new data and navigates directly to component A, which has a get method to display the inserted data. My issue is how to retrieve the new data without ...

Why is my Angular promise unexpectedly landing in the error callback?

I am facing an issue with my Angular + Typescript client. I have developed a PHP API and need to send a post request to it. Upon receiving the request, the server fills the response body with the correct data (verified through server debugging). However, w ...

Ignoring the no-unused-vars rule from StandardJS even though variables are being used

Within my Typescript React project, I have declared the following: export type NavState = { mounted: boolean } I then proceeded to use this within a component like so: import { NavState } from '../../models/nav' class Nav extends React.Compon ...

Issue: NG0204: Unable to find solutions for all parameters in NzModalRef: (?, ?, ?)

I'm currently working on an Angular project utilizing the NZ-Zorro library, and I'm encountering difficulty understanding which parameters are causing issues with NzModalRef when attempting to run the test code coverage. The error message display ...

CustomFilter in FormGroup Mat-AutoComplete

I have been using Angular CLI to create an autocomplete form that searches through all values (unlike the example on angular.io that only does a "start with" search). While I was able to get it working with [formControl], I encountered an issue when tryin ...

Starting a nested JSON structure with TypeScript and Angular 7

I'm encountering an error when attempting to make a POST request by sending an object TypeError: Can not set property 'ItemCode' of undefined My setup involves Angular 7 and Typescript Here is my initial JSON: objEnvio:any = <an ...

Remove the export statement after transpiling TypeScript to JavaScript

I am new to using TypeScript. I have a project with Knockout TS, and after compiling it (using the Intellij plugin to automatically compile ts to js), this is my sample.ts file: import * as ko from "knockout"; ko; class HelloViewModel { language: Kn ...

What is the process of changing the name of an object's key in JavaScript/Angular?

In my possession is an established entity, this.data = { "part": "aircraft", "subid": "wing", "information.data.keyword": "test", "fuel.keyword": "lt(6)" } My objective is to scrutinize each key and if the key includes .keyword, then eliminat ...

There are no properties associated with this particular data type

As someone who is new to TypeScript, I am encountering two issues with data types. This snippet shows my code: const say: object = { name: "say", aliases: [""], description: "", usage: "", run: (client: ob ...

Utilize an API call to fetch data and seamlessly showcase it on the webpage using Vue.js

I have created an API and defined it in my TypeScript file const Book = { async getBookType(intID: string): Promise<Book[]> { // const intId = API.Product.getCurrentId(); const intId = ''; const response = await h ...

The names of properties in Typescript are determined by the values of the outer type properties

In my project, I have various interfaces (or types) defined as follows: export type simpleValue = string | number | boolean | Date | null; export interface Options { inline?: OptionsItem[] | unknown[]; promptField?: string; selectedValues?: unknown[ ...

Is there a way to determine if two distinct selectors are targeting the same element on a webpage?

Consider the webpage shown below <div id="something"> <div id="selected"> </div> </div> Within playwright, I am using two selectors as follows.. selectorA = "#something >> div >> nth=1&q ...

How to retrieve object keys using angularFire and Firebase

Within my list, there is an object called multiplayer multiplayers 0YYbHk6Y1QORGEvOjvKW7DZATJs2 opponentId: "" score: 3 ErgRlNCMtrVdlZW8hwdz44HiuHl1 opponentId: "" score: 0 WlWCnCLca4TjpoBGodxXH3dkHxi1 opponentId: "ErgRlNCMtrVdlZW8hwdz44HiuHl1" score: 0 ...

Having trouble with Angular2 + Webpack DefinePlugin integration?

Attempting to launch the Angular2 app following the tutorial on angular.io has been quite the journey for me. Angular2 with Webpack Tutorial on angular.io The installation process went smoothly, despite a few minor obstacles, considering I'm using U ...

Error message: Unable to instantiate cp in Angular 17 application while building with npm run in docker container

After creating a Dockerfile to containerize my application, I encountered an issue. When I set ng serve as the entrypoint in the Dockerfile, everything works fine. However, the problem arises when I try to execute npm run build. Below is the content of my ...

Guide to transforming text fields into input elements in Angular 4

Explore My HTML Seeking to incorporate a one-click edit feature that converts all fields into input boxes easily. Check out my HTML snippet below: <div class="col-md-3"> <div class="add-details"> <span>LemonCandy, ...

Retrieve the chosen option from a dropdown list in Angular by utilizing reactive forms

I am facing an issue where I need to pass a hard coded value from a dropdown along with the input field values in my form. The problem arises because I am using formControlName to capture the input field values, but since there are no input fields in the d ...

Tips for incorporating the closeAutocomplete function into ng4-geoautocomplete

Hey there! I've incorporated the ng4-autocomplete component into my custom component and now I'm trying to figure out how to detect when the autocomplete dropdown closes. Can you help me out with implementing the "closeAutocomplete" method? Let& ...