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

Tips for initializing and updating a string array using the useState hook in TypeScript:1. Begin by importing the useState hook from the

Currently, I am working on a React project that involves implementing a multi-select function for avatars. The goal is to allow users to select and deselect multiple avatars simultaneously. Here is what I have so far: export interface IStoreRecommendation ...

Leveraging Font Awesome and Material Icons within ngFor in Angular 2

I have a unique situation where I need to display a dynamic list of icons in a right side bar. These icons are added to the sidebar based on user actions and are displayed using the ngFor directive. The challenge here is that some icons come from Font Awe ...

Combining strings with JQuery

Looking for help with a code snippet <script> function goToDetails($i){ $(function(){ var transValue = $('#trans'+$i).html(); var mileageValue = $('#mileage'+$i).html(); var engineValue = $('#eng'+$i).html ...

Tips for validating a text input field depending on the selected value in a dropdown menu using AngularJS

When a user selects from the dropdown menu, they can choose between Number and Decimalnumber. If the user selects Number, the text box should only allow whole numbers (e.g. 22, 33, 444, 345436). The user should not be able to enter decimal values like 22 ...

Turn off a feature

I'm having trouble disabling a tooltip that is being utilized from this specific website. The code for setting and calling the tooltip looks like this: this.tooltip = function(){....} $(document).ready(function(){ tooltip(); }); I've attempte ...

Using computed properties with Nuxt's `head` property can result in error messages being displayed

While utilizing Nuxt.js, I am using head() { } method to configure SEO metadata. However, when accessing computed properties within this method, Vetur displays the following message: Property 'domain' does not exist on type 'CombinedVueInst ...

Error encountered while trying to retrieve JSON data

After running the following code, I encountered an issue I received an error message stating: Uncaught TypeError: Cannot read property 'searchname' of undefined What could be causing this error and how can I fix it? var selectedVal = "calend ...

How can I attach an event listener to each row in a table that is dynamically generated?

I am currently working on a table that is being populated using data from a JSON file. The number of entries in the JSON file can vary, resulting in different lengths for the table rows. Each row includes a bootstrap dropdown button with links for actions ...

Issues with Ajax functionality in Rails

I believe my lack of knowledge in Ajax might be the reason for this issue. My goal is to continuously make ajax calls to my server as I am creating a demo app for learning purposes. Below is the code snippet: Code from job_status/index.html.erb file &l ...

Jhipster has issues with the functionality of the Bootstrap dropdown

<div class="form-group"> <div class="dropdown-example"> <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example ...

Ways to obtain the file path of the compiled worker.js loaded from the worker loader, along with its hash

Currently, I am working on a TypeScript React application that utilizes Babel and Webpack for compilation. I have implemented a rule to load my worker with the following configuration: config.module.rules.unshift({ test: /gif\.worker\.js$/, ...

Develop a duplication of the selected text without the need for the clipboard

Looking to implement an internal clipboard with a history feature for my application. Unfortunately, using the clipboard API will require user permission which is not feasible. I want to ensure that formatting such as bold, italics, and strikethrough is p ...

What is the method for retrieving the name of a canceled file?

When a user clicks on the "Cancel" button during an upload, I am attempting to retrieve the filename of the file that was canceled. However, it seems like the current method I am using (var image_file_name) is not successfully retrieving the name of the ca ...

[ERROR] There was a problem encountered during the execution of the ionic-app-scripts subprocess

I encountered an error while running my Ionic project. Below is the error message: [ERROR] ionic-app-scripts has unexpectedly closed (exit code 1). The Ionic CLI will exit. Please check any output above for error details. ionic3-firebase-shopping-car ...

Toggle the element within the selected row using Jquery

In order to create a client list, users must click on rows to select them and then submit the selections to the database for a cron job to send out emails. Instead of using checkboxes, I am attempting to highlight rows by including two hidden elements - o ...

Storing data from a collection of interface objects in a string array

Take a look at the following code snippet: import React, {FC} from 'react'; import {useFetchErrors} from "../Api/Api"; import {useLocation} from "react-router-dom"; interface ExecutionTableProps { project_id: number } const ...

Having trouble getting any results from a JSON response using the jQuery Tokeninput plugin in PHP?

I am currently trying to implement the JQuery Tokeninput feature on my website. However, when I enter text into the search field, nothing appears and it just continues searching indefinitely... After reviewing the documentation, I noticed that the expecte ...

Exploring the Power of Angular 2+: Leveraging the canLoad Feature

I'm having trouble getting the canLoad function to work with routes. It seems like it's not functioning properly. I'm not sure why, maybe it's incompatible with canActivate or something, but I'm hoping someone here might know. Wh ...

Troubleshooting: NextJS Typescript getInitialProps returning null value

I am currently working with NextJS 'latest' and TypeScript to extract the token from the URL, but I am encountering an issue where it returns undefined. To achieve this, I am utilizing the getInitialProps method. The URL in question looks like th ...

What is the best way to add selected values from a multi-select dropdown into an array?

Attempting to populate an array from a multiple select dropdown, I've encountered an issue. Despite using splice to set the order of values being pushed into the array, they end up in a different order based on the selection in the dropdown. For insta ...