https://cypress.io logo
An uncaught error was detected outside of a test
# i-need-help
w
When I run all my component cypress tests locally on a Macbook pro on a react-vite project with around ~10 tests, I get the following error:
Copy code
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:5173/__cypress/src/cypress/support/component.ts

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.
the error is not Consistant and doesn't show up on every run. It also throws on a random test every run. How can I solve this?
cypress version: 12.0.4
b
@wonderful-match-15836 any insight here?
w
update: I think a possible lead could be that I import files on my project with the absolute paths pattern. For example: import {comp1, comp2} from 'components' where as components is configured in my tsconfig.ts file
things i've tried: update to Vite 4, comment out skipped tests, converting all imports in the *.spec.cy.tsx files from absolute to relative
w
Thanks for the update. I don't have anything to add, inconsistent errors are the worst. I will try to take a closer look soon, but in the mean time somebody else might chime in with ideas. Please keep sharing updates if you try more things. Also, what is the command you used to run all your component tests? Is it just
cypress run --component
? Do you ever have problems in open app? From the looks of things this seems like it's related to that component support file, your import paths may be fine.
w
Copy code
"cy:run": "cypress run --component --browser chrome --config-file ./cypress.config.ts",
Copy code
import { defineConfig } from 'cypress';

export default defineConfig({
    component: {
        devServer: {
            framework: 'react',
            bundler: 'vite',
        },
        viewportHeight: 1080,
        viewportWidth: 1920,
        video: false,
        screenshotOnRunFailure: false,
    },
    env: {
        VITE_DISABLE_LOGIN: 'true',
    },
});
here is my config file
Copy code
// ***********************************************************
// This example support/component.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************
// Import commands.js using ES2015 syntax:
// Alternatively you can use CommonJS syntax:
// require('./commands')
import { mount } from 'cypress/react18';

import './commands';

// Augment the Cypress namespace to include type definitions for
// your custom command.
// Alternatively, can be defined in cypress/support/component.d.ts
// with a <reference path="./component" /> at the top of your spec.
declare global {
    namespace Cypress {
        interface Chainable {
            mount: typeof mount;
        }
    }
}

Cypress.on('uncaught:exception', (err, runnable) => {
    console.error({ err, runnable });

    // returning false here prevents Cypress from
    // failing the test
    return false;
});

Cypress.Commands.add('mount', mount);

// Example use:
// cy.mount(<MyComponent />)
here is my component.ts
commands:
Copy code
import { CypressWrapper } from '../../src/Wrapper';

/// <reference types="cypress" />
// ***********************************************
// This example commands.ts shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
//

declare global {
    namespace Cypress {
        interface Chainable {
            getByTestId(selector: string | RegExp, ...args: any): Chainable<JQuery<HTMLElement>>;
            getByTestIds(selectors: string[], ...args: any): Chainable<JQuery<HTMLElement>>;
            wrappedMount: typeof cy.mount;
        }
    }
}

Cypress.Commands.add('getByTestId', (selector: string | RegExp, ...args: any) => {
    return cy.get(`[data-testid=${selector}]`, ...args);
});
Cypress.Commands.add('getByTestIds', (selectors: string[], ...args: any) => {
    const selector = selectors.map((id) => `[data-testid=${id}]`).join(' ');
    return cy.get(selector, ...args);
});

Cypress.Commands.add('wrappedMount', (component: any) => {
    // Wrap any parent components needed
    // ie: return mount(<MyProvider>{component}</MyProvider>, options)
    return cy.mount(<CypressWrapper>{component}</CypressWrapper>);
});
I've also noticed that when I run cy:run it sometimes freezes
running the tests sometimes work and sometimes doesn't... we can maybe have a zoom call I can let u guys take a closer look
w
Thanks for the offer of the zoom, I think the next step is just to move this conversation to github as a bug report, would you mind opening an issue https://github.com/cypress-io/cypress/issues/new?assignees=&labels=&template=1-bug-report.yml
5 Views