question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue3 + Vite component test import error

See original GitHub issue

Current behavior

When running a Vue3 component test with "@cypress/vite-dev-server": "^2.1.0" and "@cypress/vue": "^3.0.3" I see the following error on startup:

Failed to fetch dynamically imported module: http://localhost:3000/src/app/components/widgets/AdminTitle.cy.ts?import

This occurs about 75% of the time, but oddly works on occasion. It does not occur on hot reload when running with open-ct. The actual tests pass, but the error causes CI to fail and does not appear to be catchable with:

Cypress.on('uncaught:exception', (err, _runnable) => {
    return false
})

The error message from this issue is identical, but I’m not sure if the cause is related: https://github.com/lmiller1990/vue-3-cypress-vite/issues/1

Desired behavior

The error should not be thrown.

Test code to reproduce

It’s difficult for me to provide a demo project since the code is in a complex monorepo, but I will work on a minimal reproduction. I’m posting now in case there’s an obvious solution, or someone else has encountered the same problem.

AdminTitle.cy.ts

import { mount } from '@cypress/vue'
import { i18n, t, stubRouter } from '@cypress-local/support/stubs'
import AdminTitle from './AdminTitle.vue'

const router = stubRouter(['Home'])
const titleKey = 'name'

it('Shows the correct i18n text', () => {
  mount(AdminTitle, { props: { titleKey }, global: { plugins: [i18n, router] } })
    .get('.content-title')
    .contains(t('name'))
  cy.get('.button-back').should('not.exist')
})

it('Shows the back button', () => {
  mount(AdminTitle, {
    props: { back: 'DropsPage', titleKey },
    global: {
      plugins: [i18n, router],
    },
  })
    .get('.button-back')
    .should('exist')
})

AdminTitle.vue

<template>
  <div class="content-title-wrap">
    <ButtonBack v-if="back" :to="back" />
    <div class="content-title">
      {{ t(titleKey) }}
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'

defineProps({
  back: {
    type: String,
    default: null,
  },
  titleKey: {
    type: String,
    required: true,
  },
})

const { t } = useI18n()
const router = useRouter()
</script>

cypress/support/stubs.ts

import { createI18n } from 'vue-i18n'
import { createRouter, createWebHistory, RouteRecordRaw, Router } from 'vue-router'
import en from '../../src/app/translations/en.json'

type MessageSchema = typeof en

export const i18n = createI18n<[MessageSchema], 'en'>({
  legacy: false,
  locale: 'en',
  messages: { en },
})

export const t = i18n.global.t
export const tm = i18n.global.tm

export const stubRoutes = (names: string[]): RouteRecordRaw[] =>
  names.map((name) => ({
    name,
    path: '/',
    component: {
      template: '<div>Test</div>',
    },
  }))

export const stubRouter = (routeNames: string[]): Router =>
  createRouter({
    history: createWebHistory(),
    routes: [...stubRoutes(routeNames)],
  })

cypress/plugins/index.ts

import { startDevServer } from '@cypress/vite-dev-server'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
module.exports = (on: any, config: any) => {
  on('dev-server:start', (options: Cypress.DevServerConfig) =>
    startDevServer({
      options,
    }),
  )

  return config
}

cypress.json

{
  "testFiles": "**/*.cy.{js,ts}",
  "componentFolder": "src/app",
  "viewportWidth": 800,
  "viewportHeight": 500,
  "video": false
}

Cypress Version

8.4.0

Other

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
losnappascommented, Dec 15, 2021

I believe this is happening because vite does some magic and caches imports.

run-ct fails on the first try because there’s no cache and it refreshes(?), but on consecutive goes it succeeds. That’s a problem in CI and in headless mode, but not headful.

Reproducible by deleting node_modules/.vite folder before run-ct.

1reaction
TotomInccommented, May 9, 2022

Hi, the issue still persists with Vite 2.9.8:

{
  "devDependencies": {
    "@cypress/vite-dev-server": "^2.2.2",
    "@cypress/vue": "^3.1.2",
    "@vitejs/plugin-vue": "^2.3.2",
    "cypress": "^9.6.0",
    "typescript": "^4.6.4",
    "vite": "^2.9.8"
  }
}

The following happens on the CI with cypress run-ct:

9:41:47 AM [vite] ✨ new dependencies optimized: vue
9:41:47 AM [vite] ✨ optimized dependencies changed. reloading
  1) An uncaught error was detected outside of a test
  0 passing (251ms)
  1 failing
  1) An uncaught error was detected outside of a test:
     TypeError: The following error originated from your test code, not from Cypress.
  > Failed to fetch dynamically imported module: http://localhost:3000/src/DCta.spec.ts?import
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.

  DCta tests
    βœ“ renders the DCta as a button when no props is passed (64ms)
    βœ“ renders the DCta as a link (56ms)
    βœ“ renders the DCta as a disabled button (40ms)

  (Results)
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Tests:        1                                                                                β”‚
  β”‚ Passing:      0                                                                                β”‚
  β”‚ Failing:      1                                                                                β”‚
  β”‚ Pending:      0                                                                                β”‚
  β”‚ Skipped:      0                                                                                β”‚
  β”‚ Screenshots:  1                                                                                β”‚
  β”‚ Video:        true                                                                             β”‚
  β”‚ Duration:     0 seconds                                                                        β”‚
  β”‚ Spec Ran:     DCta.spec.ts                                                                     β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
====================================================================================================
  (Run Finished)
       Spec                                              Tests  Passing  Failing  Pending  Skipped  
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ βœ–  DCta.spec.ts                             246ms        1        -        1        -        - β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    βœ–  1 of 1 failed (100%)                     246ms        1        -        1        -        -  

As explained by other commenters, the issue is appearing on the first page loading. Then, is seems to resolve the import successfully most probably due to vite caching.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Imported component from local vite+vue library not updating
I encounter this problem today with my package and finally, I found the real culprit is the node_module inside the package that we...
Read more >
Add testing to Vite - DEV Community ‍ ‍
Here is our first unit test placed next to the HelloWorld.vue component file. // src/components/HelloWorld.spec.ts import { mount }Β ...
Read more >
Troubleshooting - Vite
Check current limit $ ulimit -Sn # Change limit (temporary) $ ulimit -Sn 10000 # You might need to change the hard limit...
Read more >
Vue Quickstart - Cypress Documentation
Welcome! This tutorial will walk you through creating a Vue app and using Cypress Component Testing to test it. We assume you are...
Read more >
Getting Started with Vitest | Vue Mastery
How to use Vitest to test your components ... import { defineConfig } from "vite"; import vue from "@vitejs/plugin-vue"; ... error.gifΒ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found