Error message: Cypress Vue component test fails due to the inability to import the Ref type (export named 'Ref' is missing)

Recently, I created a Cypress component test for my Vue component by mounting it and verifying its existence. The component utilizes Vue's Ref type and is structured as a TypeScript component.

However, during the test execution, Cypress encountered an error:

An export named 'Ref' was not found in the requested module '/__cypress/src/node_modules/.vite/deps/vue.js?v=861dc0d7'

I've already updated the types in my Cypress tsconfig.json to include 'vue', but haven't been able to resolve this issue.

tsconfig.json
---
{

  "compilerOptions": {
    "target": "es5",
    "lib": ["es5", "dom"],
    "types": ["cypress", "vue", "node"],
    "baseUrl": "./",
    "allowJs": true,
    "paths": {
      "~": ["."]
    },
  },
  "include": ["**/*.ts", "**/*.js"]
}

Below is my Cypress configuration:

cypress.config.js
---
const { defineConfig } = require("cypress")

module.exports = defineConfig({
    component: {
        supportFile: "cypress/support/component.ts",
        devServer: {
            framework: "vue",
            bundler: "vite",
        },
    },
})

The component code snippet:

MyComponent.vue
---
<template>
    <div class="my-component">
        {{ counter }}
    </div>
</template>

<script setup lang="ts">
import type { Ref } from "vue"

const counter: Ref<number> = ref(1)
</script>

Here's how the test is implemented:

MyComponent.cy.ts
---
import MyComponent from "./MyComponent.vue"

beforeEach(() => {
    cy.mount(MyComponent)
})

it("Test My Component", () => {
    cy.get(".my-component").should("exist")
})

Answer №1

Within your Typescript project, the presence of cypress.config.js suggests that Cypress may not be configured to use Typescript (import type {Ref} is invalid syntax in javascript).

If I were to incorporate your component into a running Typescript project with cypress.config.ts and appropriate adjustments, it would then recognize the type import.

(Please note that I am using an older version of Vue)

import { defineConfig } from "cypress";

export default defineConfig({
  component: {
    devServer: {
      framework: "vue-cli",
      bundler: "webpack",
    },
  },
})

In addition, you will need to include an import for the specific ref within the component

<script setup lang="ts">
import type { Ref } from "vue"
import { ref } from "vue"            <-- add this 

const counter: Ref<number> = ref(1) 
</script>

Answer №2

After investigating further, I identified the root cause of the problem. It appears that in my tsconfig.json configuration file, I was extending from Nuxt's tsconfig (

"extends": "./.nuxt/tsconfig.json"
)

By removing this line from the configuration file, the issue was successfully resolved.

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

Completion of TypeScript code is not working as expected, the variable that is dependent upon is not

Looking for assistance with creating code completion in TypeScript. Variable.Append1 Variable.Append2 Variable.Append3 I have defined the following class: class Variable { Append1(name: string){ if (name == undefined) ...

Most effective methods for validating API data

Currently, I am working on developing an api using nestjs. However, I am facing some confusion when it comes to data validation due to the plethora of options available. For instance, should I validate data at the route level using schema validation (like ...

Encountered a bug in the findUnique function within the services of a Nest JS and Prisma project

I have a question about using Prisma with Nest. I keep encountering this error: src/modules/auth/auth.service.ts:28:63 - error TS2322: Type 'UserWhereUniqueInput' is not assignable to type 'string'. 28 const user = await this.prisma ...

Strategies for managing the integration of mixins, filters, and directives in a sprawling Vue.js project

Hey there! I am currently working on a large Vue app and I've stumbled upon a challenge. Can someone please guide me on how to effectively organize the importing of mixins, filters, and directives? I seem to be struggling with this concept and would g ...

Tips for updating the value.replace function for the "oninput" attribute within Angular 7

I need to modify an input based on a value from a TypeScript variable in the oninput attribute. This modification should only apply to English characters. In my HTML file: <input class="form-control" oninput="value=value.replace(r ...

Getting started with a project using meteor.js and vue.js

As a beginner in meteor.js, I am eager to create a project using both meteor.js and vue.js. However, I am struggling to find the right method for managing files in meteor.js. Could someone please assist me by providing a demo project or video link that c ...

Experiencing "localhost redirect loop due to NextJS Middleware" error

After successfully integrating email/password authentication to my locally hosted NextJS app using NextAuth, I encountered an issue with the middleware I created to secure routes. Every time I tried to sign out, I received an error stating "localhost redir ...

Activate a module within a Vuex action only upon dispatching

Is there a way to import a package for use in a Vuex Action only when the Action is dispatched, in order to reduce the size of my entry bundle? Here's what I've tried: export const actions = { async createNewBin(store, bin) { const fireba ...

What is the best way to incorporate debounce functionality in Vue2?

I'm working on a Vue template and need to incorporate debounce for a simple input box. Here is what I have so far: <input type="text" v-model="filterKey" debounce="500"> However, I've come to realize that the ...

What are the best methods for protecting a soda?

My code is in strict mode, and I am encountering an issue with the following snippet: const a: string[] = []; // logic to populate `a` while (a.length > 0) { const i: string = a.pop(); // This line is causing an error console.log(i); // additio ...

What is the best way to create a dynamic data structure using an array?

I recently discovered that Vuejs does not track changes in data beyond the first level of an array. Is there a way to modify this behavior? new Vue({ el: '#container', data: { value: [], }, beforeMount() { this.value[0] = &apo ...

Set a Vue.js variable when the input value changes

Hey there, I'm having trouble with a form that I need to complete using Vue.JS. Basically, I want the value of the 'price_vat' field to update with some calculations every time the 'price_user' input is updated. Everything was work ...

Angular Validators.pattern() does not seem to function properly, despite yielding successful results in online regex testers

I have developed a regex pattern on Regex101.com and thoroughly tested it. However, when I applied it to my FormControl Validators.pattern method, it is exhibiting unexpected behavior. This regex pattern is meant for validating the Width input of a packag ...

Issue with displaying entire object using Jest and console.dir

I'm having trouble displaying an error in my Jest test because it's not showing all the levels as expected. import util from 'util' describe('Module', () => { it('should display all levels WITHOUT util', () =& ...

Invoking a function on an object of a subclass that derives from an abstract class

In the realm of abstract classes, behold this one: export abstract class BaseStepComponent { /** Behold thy base-step ctor */ constructor() { } abstract getValue(): string; } And lo, here is a component that inherits such abstract glory ...

Restrict quasar input to only accept numbers with a maximum of one digit after the decimal point

Can Quasar’s q-input be configured to only accept integers and floating point numbers with a single decimal place? I have attempted using :decimals="1" and step="0.1", but it still permits the input of any type of number, indicating a lack of form valida ...

Sharing API types from a NestJs backend to a Vue frontend (or any other frontend) application: Best practices

In my development setup, I have a VueJs and Typescript Front End app along with a PHP server that is in the process of being converted to NestJs. Both of these projects reside in the same Monorepo, allowing me to share types for DTOs between them. I am ex ...

Ways to modify state value upon logging in

What is the best way to update the state value upon logging in, in order to utilize the same state on the main page? account.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); export const account = new Vuex.Store({ st ...

Tips for adding styling to HTML in Vue by entering CSS code into the CodeMirror editor

Is there a way to style HTML by entering CSS code into the Codemirror editor? For instance, if we have the HTML code <head> </head>, how can I apply the CSS code, head{ color : red }, typed in the Codemirror editor to stylize this HTML code? mo ...

Assigning namespaces to a property of classes in typescript: A step-by-step guide

As I work on adding a declaration file to a TypeScript package, I encounter some syntax that looks like this: const Sequelize = require('Sequelize'); //... class Application { Sequelize = Sequelize; } To address this, I created a file named ...