What steps should I take to address conflicting type identifiers between Cypress and jQuery?

Currently, I am tasked with writing TypeScript end-to-end tests for an Angular 11 application. Following the recommended practices of Cypress, my test setup is encountering a conflict due to existing jQuery dependencies (3.5.1) in the app and Cypress (8.4.1) using its own global jQuery (3.3) type definitions. This conflict results in the following runtime error:

error TS6200: Definitions of the following identifiers conflict with those in another file: TypeOrArray, Node, htmlString...

Although the app compiles and runs, every routing request /request leads to the error Cannot get /request. Curiously, forcing a recompile by adding dummy code allows it to function properly after displaying the same pre-runtime error.

Here are some specifics about my setup:

/cypress/tsconfig.json

{
  "extends": "../tsconfig.json",
  "include": [
    "./**/*.ts"
  ],
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "es5",
      "dom"
    ],
    "types": [
      "cypress", // this was supposed to ignore jquery types conflicts as per docs
      "cypress-localstorage-commands",
    ]
  }
}

tsconfig.json

{
  "compileOnSave": true,
  "include": [
    "...", // other stuff
    "**/*.d.ts",
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "exclude": [
    "node_modules/cypress/*",
    "cypress/support/index.d.ts"
  ],
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target"": "es5",
    "typeRoots": [
      "node_modules/@types",
      "./@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

The error message received: https://i.stack.imgur.com/qtmvD.png

Therefore, my questions are:

  1. How can I resolve the conflicting types issue?
  2. What causes it to work on the second attempt but not the first?

My unsuccessful attempts so far include:

  1. Applying skipLibcheck to both or individual tsconfigs.
  2. Configuring tsconfig according to their guidelines to address this problem.
  3. Trying to exclude cypress within the main tsconfig and jQuery within cypress's tsconfig.

Answer №1

After exploring different options, I successfully identified the solution. It turns out that Option 3 was the correct choice, which involved excluding Cypress within the main tsconfig file and jQuery within Cypress's tsconfig.

For the benefit of future individuals facing a similar issue, here are the updated tsconfig files:

cypress/tsconfig.ts

{
  "extends": "../tsconfig.json",
  "include": [
    "./**/*.ts"
  ],
  "exclude": [],
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "ES2015",
      "es5",
      "dom"
    ],
    "types": [
      "cypress"
    ]
  }
}

tsconfig.ts

{
  "compileOnSave": false,
  "include": [
    "...",
    "@types/index.d.ts", // Specified exact file instead of using wildcards to avoid including jQuery.
  ],
  "exclude": [
    "cypress/global.d.ts" // It's crucial to exclude this file.
  ],
  "compilerOptions": {
    ...,
    "typeRoots": [
      "node_modules/@types",
      "./@types"
    ],
  }
}

cypress/global.d.ts

/// <reference types="cypress" />

declare namespace Cypress {
  interface Chainable {

    customCommand1(args: any): Chainable<Element>;

    customCommand2(args: any): Chainable<Element>;
  }
}

Below are the references that were particularly helpful in unraveling this issue:

  1. Cypress GitHub Issue
  2. Namespaces in TypeScript Handbook
  3. TypeScript Configuration Documentation

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

What is the best way to initialize a value asynchronously for React context API in the latest version of NextJS, version

Currently, I'm working on implementing the React context API in my NextJS e-commerce application to manage a user's shopping cart. The challenge I'm facing is how to retrieve the cart contents from MongoDB to initiate the cart context. This ...

Searching for an Angular 7 alternative to Vue.js's "nextTick" function

In my Angular 7 project, I'm facing a situation where I need to automatically scroll to a specific section on the page after it has been fully rendered. The section is initially hidden using ngif and should only be scrolled to once it is visible in th ...

Is there a way to halt the current traversal of visitEachChild in TypeScript Transformer API?

While navigating through each child node of a parent node using visitEachChild, is there a way to stop the process when I no longer wish to visit the subsequent child nodes? For example: Parent node Node 1 Node 2 <-- My target point. Node 3 Node 4 Nod ...

How is it possible to encounter a Javascript unexpected token ] error within an HTML code?

While working on my project, I encountered a JavaScript error in the console of Chrome. The error message stated "Unexpected token ]" and it was pointing to a specific line of raw HTML code. I am puzzled about what could be causing this issue. Unfortunatel ...

Troubleshooting React TypeScript: Resolving the Error "Argument of type ''""' is not assignable to parameter of type 'SetStateAction<undefined>'"

Currently, I am troubleshooting a React application that was extracted from a live server and now I am attempting to run it on my local machine. However, upon starting up the application locally, I encountered the following error message: Argument of ty ...

What steps can be taken to ensure the visibility and accessibility of two vertically stacked/overlapped Html input elements in HTML?

I have encountered a challenge with my HTML input elements. There are two input elements that overlap each other, and I would like to make both inputs visible while only allowing the top input to be editable. I have tried several approaches involving z-ind ...

Executing Typescript build process in VSCode on Windows 10 using Windows Subsystem for Linux

My configuration for VSCode (workspace settings in my case) is set up to utilize bash as the primary terminal: { "terminal.integrated.shell.windows": "C:\\WINDOWS\\Sysnative\\bash.exe" } This setup allo ...

issue with mark.js scrolling to selected sections

Our document searching functionality utilizes mark.js to highlight text and navigate to the results. You can see an example of this in action here. If you search for "Lorem ipsum" -> the highlighting works perfectly, but the navigation jumps to fragments ...

How do I use jQuery to target a specific list item based on its data attribute and the ID of the list it belongs to?

Can anyone provide me with the correct jQuery syntax for selecting a list item based on a data attribute within a specific list? Below is an example list: <ul id="menu"> <li data-code="soup">Soup</li> <li data-code="salad">Salad< ...

When using a jsonp ajax call, the response is successfully returned as json in Firebug, but unfortunately it

After encountering a jsonp cross domain ajax query, I am facing an issue where the jQuery ajax function's error part is triggered despite receiving a status of 200 OK and a status text of success. Even though I can monitor the response in Firebug and ...

What could be causing Typescript to inaccurately infer the type of an array element?

My issue revolves around the object named RollingStockSelectorParams, which includes arrays. I am attempting to have TypeScript automatically determine the type of elements within the specified array additionalRsParams[title]. The main question: why does ...

"Exploring the method of showcasing a Json array within a tinymce

Here is a JSON array that I have: { "kutip":"<p>Lorem Ipsum is simply dummy text.</p>", "desc":"<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>" } This is the jQuery script to display the ...

Troubleshooting inactive CSS hover animation

Greetings! I'm facing an issue with a CSS hover animation. Here are two demos I've created: In the first DEMO, the animation works perfectly. However, in the second DEMO, it doesn't seem to be functioning. On the second demo, there are two ...

How can you verify the data type of an object without resorting to type guarding

I have a situation where I need to deal with different types of objects. export interface A { links: number name: string } export interface B { cat: boolean name: string } Initially, I considered using this method: const IsTypeB = (obj: any): obj ...

Error encountered in Angular 17 SSR: Importing module "@angular/ssr" does not have the exported member "REQUEST"

I'm having trouble getting cookies on Angular 17 with SSR. In the past, I was able to use the REQUEST pulled from @nguniversal/express-engine/tokens, but now with @angular/ssr, I can't find where to pull REQUEST from. import { REQUEST } from &apo ...

Click to rotate the image - jQuery

I'm attempting to create a functionality where an image rotates upon clicking. Since this feature needs to work for multiple images, I understand the importance of utilizing the $(this) tag in jQuery. However, my initial attempt failed to produce the ...

Updating HTML label values using jQuery in a loop based on their index position

I am having an issue with my ajax call where I want to loop through each label in a class and set their value to the response returned from the server. However, the current code sets all labels to the same value instead of setting individual values based o ...

Troubleshooting Issue with Chrome/chromium/Selenium Integration

Encountered an issue while attempting to build and start the application using "yarn start": ERROR:process_singleton_win.cc(465) Lock file cannot be created! Error code: 3 Discovered this error while working on a cloned electron project on a Windows x64 m ...

Leverage AJAX data to dynamically generate an input field within a Laravel application

. Hey everyone, I'm currently working on implementing ajax for a search functionality. The goal is to display links to the search results' pages along with checkboxes next to each result, allowing users to select orders for printing. Although I ...

Altering CSS styles through JavaScript or jQuery

Exploring Options After investigating the use of .css() to manipulate CSS properties of specific elements, I came across a website showcasing the following CSS: body, table td, select { font-family: Arial Unicode MS, Arial, sans-serif; font-size: ...