Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component tests failing intermittently with uncaught Vite "failed to fetch dynamically imported module" error in CI #25913

Open
synaptiko opened this issue Feb 22, 2023 · 150 comments
Assignees
Labels
CI General issues involving running in a CI provider CT Issue related to component testing npm: @cypress/vite-dev-server @cypress/vite-dev-server package issues prevent-stale mark an issue so it is ignored by stale[bot] Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team.

Comments

@synaptiko
Copy link

synaptiko commented Feb 22, 2023

Current behavior

We started migrating our project from CRA to Vite and we have almost 300 component and 40 E2E Cypress tests in place. Unfortunately, after fixing all the other issues, we are still not able to stabilize our tests since there is always 1 or 2 failing randomly on "Failed to fetch dynamically imported module" errors.

We noticed that it's somehow related to the load of CI. Under some conditions more tests are failing like this, in other times it succeeds. But it's random. We checked our tests and we are pretty sure it's not caused by any logic we have.

We've checked some of the existing issues on Cypress & Vite, tried various workaround but no luck with any of them.

What we think is happening is that Cypress is not waiting for Vite to "boot up" properly and retries don't help with it, only when new spec is run, it works.

Note: it only happens with component tests. For E2E tests we had similar stability issues but we solved them by building production version and then just serving it with vite preview. This made integration tests faster and very stable. Previously they were also timeouting.

Note 2: we have a lot of components and lot of dependencies in our project, we also use MUI as our base. But with CRA we were able to have stable tests, it was just around two times slower. That's why we want to use Vite now.

Note 3: we are running "Component tests" in parallel, currently in 4 groups.

Desired behavior

No random problems with Cypress + Vite combo.

Test code to reproduce

Unfortunately can't provide this since our project is private. And I'm afraid that it's related to project's complexity and that's why we can't easily create another one for reproduction.

Cypress Version

v12.6.0

Node version

v16.18.1

Operating System

Docker Hub image: cypress/included for v12.6.0 version

Debug Logs

No response

Other

No response

@mschaefer-gresham
Copy link

mschaefer-gresham commented Feb 22, 2023

We are having the exact same issue with component tests ( I was just coming here to open a new issue ) . We just migrated from webpack to vite. The error we get is:

The following error originated from your test code, not from Cypress. > Failed to fetch dynamically imported module: http://localhost:3000/__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.

We are getting this despite the fact that the file does exist. And this occurs randomly.

Vite: 4.1.1
Cypress: 12.5.1
Node: 19.3.0

One related issue: our cypress directory is on the same level as our src directory. When running locally, Cypress correctly expects component.ts to be where we have it in the cypress directory. But when we run the tests in docker, Cypress expects component.ts to be in a cypress directory under the src directory (see above error). Even if we use the 'supportFolder' config setting, Cypress still looks for it in the src directory (src is the vite root folder). So I just copied component.ts to the location cypress expects it to be (but still fails randomly despite this).

So locally this is not reproducable for us (so far). This only occurs in a docker container.

We are also running the tests in parallel.

@synaptiko
Copy link
Author

Update: we found an ugly workaround by using https://docs.cypress.io/guides/guides/module-api#cypressrun and writing our own parallelization mechanism and runner which wraps Cypress. We are checking for errors and we will retry failed tests again.

It works but it requires some "balance" in the number of workers. We also made the first group run first, and only when it's successful, we run the other groups in parallel. This seems to help a lot. But there are still random failures from time to time but because we retry them a few times, it will eventually settle down.

It looks to be somehow related to "background" Vite's compilation & deps optimization because we observed that it usually gets stable once some of Vite's log messages appear.

@lmiller1990
Copy link
Contributor

Is this only in Docker? We use Vite heavily internally - everything is really stable. I hope someone can reproduce it reliably.

I wonder if adding entries to https://vitejs.dev/config/dep-optimization-options.html#optimizedeps-entries helps?

@mschaefer-gresham
Copy link

mschaefer-gresham commented Feb 24, 2023 via email

@synaptiko
Copy link
Author

Is this only in Docker?

Yes, it seems to be only happening on CI, which means Docker. And we tried optimizeDeps.entries but it didn't seem to really help.

It is somehow related to CI being under load. We observed a few more different errors happening. It just seems that loading of some resources is either failing or incomplete causing various strange issues (sometimes with loading the import, sometimes errors on Cypress level, sometimes in app, usually something about missing context). But all these are usually resolved after 1-3 retries, depending on the CI load.

@mschaefer-gresham
Copy link

mschaefer-gresham commented Feb 24, 2023

We are experiencing more or less exactly the same thing. In particular the missing context error from time to time and failing to load component.ts.

@lmiller1990
Copy link
Contributor

lmiller1990 commented Feb 27, 2023

Maybe related: #18124 (comment)

The OP says "Latest Cypress + Vite" - is this suggesting this is a regression? If so, knowing which version introduced this regression would be very useful.

I haven't seen this in our internal suite, but it sounds like I may be able to reproduce by adding lots of large dependencies, like MUI? Or by increasing the load, eg Docker?

@mschaefer-gresham
Copy link

mschaefer-gresham commented Feb 27, 2023

We use MUI. We have split approx 700 component tests into 8 groups of varying sizes which run in parallel in docker containters. The machine is sufficient for the load. Since writing the first post above we had 7 successful runs and then today we experienced the same failure across several of the groups. This time it was component.ts in one group, but also random test files in other groups. I tried the suggestion that @synaptiko gave to run one small group first before running the other tests in parallel. I have since had two successful test runs in a row. Hopefully this will stabalize the build more, but I have seen in the past that making a change can can improve things temporarily only to have a regression.

I have also tried running all the tests sequentially and get the same error btw.

@synaptiko
Copy link
Author

@lmiller1990

The OP says "Latest Cypress + Vite" - is this suggesting this is a regression? If so, knowing which version introduced this regression would be very useful.

We are transitioning from CRA and much older Cypress, so I can really say if it's regression or not. We just started using versions from the last week.

it sounds like I may be able to reproduce by adding lots of large dependencies, like MUI? Or by increasing the load, eg Docker?

This correlates with our observations. We have MUI but also Syncfusion and a few other bigger dependencies. Our CI uses quite powerful machines but we are running a lot of things in parallel, the load changes over the day, which would explain why it happens randomly (but we observed it's like ~50% of cases).

@synaptiko
Copy link
Author

synaptiko commented Feb 27, 2023

For anyone interested, here's our "workaround" solution. We have a script called cypress-ct-runner.mjs (under bin/vite-migration:

#!/usr/bin/env node
import { Chalk } from 'chalk';
import cypress from 'cypress';
import fs from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';

const chalk = new Chalk({ level: 1 });
const groupId = process.argv[2];

if (groupId === undefined) {
  console.error('Please provide a group id parameter');
  process.exit(1);
}

const __dirname = dirname(fileURLToPath(import.meta.url));
const groups = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'component-test-groups.json')));
const group = groups[groupId - 1];
const maxRetries = 5;
let specs = group.join(',');
let shouldFail = false;
let shouldRetry = true;
let retries = 0;

while (shouldRetry) {
  if (retries > 0) {
    console.log(chalk.bgRed(`Retry number: ${retries}`));
    console.log(chalk.bgRed('Retrying failed tests:'));
    console.log(
      chalk.red(
        specs
          .split(',')
          .map((spec) => `- ${spec}`)
          .join('\n')
      )
    );
    console.log();
  }

  const result = await cypress.run({
    spec: specs,
    browser: 'chrome',
    headless: true,
    testingType: 'component',
  });

  const failedTests = result.runs.reduce((failed, { tests, spec }) => {
    return [...failed, ...tests.filter(({ state }) => state === 'failed').map((test) => ({ ...test, spec }))];
  }, []);

  specs = failedTests
    .map(({ spec }) => spec.relative)
    .reduce((result, spec) => {
      if (!result.includes(spec)) result.push(spec);
      return result;
    }, [])
    .join(',');

  if (failedTests.length === 0) {
    shouldRetry = false;
  } else if (retries >= maxRetries) {
    shouldRetry = false;
    shouldFail = true;
  } else {
    retries++;
  }
}

if (shouldFail) {
  console.log(chalk.bgRed("Couldn't recover these failing tests:"));
  console.log(
    chalk.red(
      specs
        .split(',')
        .map((spec) => `- ${spec}`)
        .join('\n')
    )
  );
  process.exit(1);
} else {
  console.log(chalk[retries === 0 ? 'bgBlue' : 'bgRed'](`Finished running tests with ${retries} retries`));
}

Under the same path we also have another script which collects paths of all our component test specs and "groups" them randomly, it looks like this:

#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const glob = require('glob');

const groupCount = process.argv[2];

if (groupCount === undefined) {
  console.error('Please provide a group count parameter');
  process.exit(1);
}

glob('src/**/*.cytest.tsx', (err, files) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exit(1);
  }

  console.log('Found', files.length, 'files');

  // sort files randomly
  files.sort(() => Math.random() - 0.5);

  // take 1/3 of the files and put them in the first group to warm up Vite's cache
  const firstGroupAmount = Math.ceil(files.length / groupCount / 3);
  const groups = [files.splice(0, firstGroupAmount)];

  // ignore the first group in this loop
  for (let i = 0; i < groupCount - 1; i++) {
    groups.push(files.slice((i * files.length) / (groupCount - 1), ((i + 1) * files.length) / (groupCount - 1)));
  }

  console.log(
    'Created',
    groups.length,
    'groups with following distribution:',
    groups.map((group) => group.length)
  );

  fs.writeFileSync(path.join(__dirname, '..', '..', 'component-test-groups.json'), JSON.stringify(groups, null, 2));
});

On CI we have a following setup of steps:

  • Distribute component tests randomly
    • ./bin/vite-migration/distribute-component-tests.js 5
  • Component tests [1]
    • ./bin/vite-migration/cypress-ct-runner.mjs 1
  • Component tests [2-5]
    • ./bin/vite-migration/cypress-ct-runner.mjs 2-5

Our whole CI flow looks like this:
image

I hope that someone will find this useful until the root cause gets fixed.

@lmiller1990
Copy link
Contributor

lmiller1990 commented Feb 27, 2023

Wow, what a hack - the fact you needed to do that really isn't ideal, I hope we can isolate and fix this soon. I wonder if we need to reach out to the Vite team - they'd probably have more insight than we would for the Vite internals.

Seems a lot of similar issues in Vite: https://github.com/vitejs/vite/issues?q=is%3Aissue+Failed+to+fetch+dynamically+imported+module

FYI we do the import here:

load: () => import(`${devServerPublicPathRoute}${supportRelativeToProjectRoot}`),

I wonder if we can add some pre-optimzation logic during CI mode to force Vite to pre-compile all dependencies, side stepping this issue entirely (which is sort of what the workarounds here are doing).

@lmiller1990
Copy link
Contributor

Does anyone know any large OSS React + Vite + MUI projects I could try using to reproduce? I tried moving MUI core to Vite but it's not straight forward.

@matanAlltra
Copy link

matanAlltra commented Mar 7, 2023

This seems like a duplication of this thread so writing here also:

I have tried to run the tests: locally, on our Github Actions CI, cypress IO cloud and currents.dev(cypress cloud competitor). Out project is using React + Typescript.

locally:
env: Macbook Pro 2021 12.5.1 (21G83) (M1 chip)
running locally without docker
result: failing after a few test runs

Github Actions:
env: ubuntu-latest-4-cores
docker: Github Worker Ubuntu 4 cores
result: failing after a few test runs

Cypress.io
env: unknown
docker: unknown
result: success - it seems to work as expected

currents.dev(cypress alternative)
env: unknown
docker: unknown
result: success - it seems to work as expected

Haven't mention it until now but on the cloud solution it does seem to work. which leads me to the conclusion that it is something with my environment that I'm doing wrong

here is an example of a test I notice that fails more often

ApproveRun.spec.cy.tsx

import { ROUTES } from 'consts';
import { IN_PROGRESS } from 'consts/RUN';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import DynamicPage from 'routes/DynamicPage/DynamicPage';

import { beforeEachInit, run } from './util';

describe('Approve run', () => {
	beforeEach(() => {
		beforeEachInit();
		cy.wrappedMount(
			<MemoryRouter initialEntries={[`/run?runId=${run.id}`]}>
				<Routes>
					<Route path="/:page" element={<DynamicPage />} />
				</Routes>
			</MemoryRouter>,
		);
	});

	it('should approve run action', () => {
		cy.getByTestId('ApprovalHeader__decline-button').click();

		cy.intercept(`${ROUTES.runRoute}/${run.id}`, {
			ok: true,
			run: { ...run, status: IN_PROGRESS },
		});

		cy.getByTestId('JsonFormModal_SubmitButton').click();

		cy.getByTestId('TagCell__turquoise').contains('In progress...');
	});
});

@lmiller1990 regarding the complexity of the test - I'd say it is pretty complex and heavy test because we render almost the top component in a SPA application
regarding the imports - we are using absolute paths all across the codebase so it will be difficult to refactor. In our tests I did suspected that absolute paths imports may cause the issue so I tried to refactor the imports in the test to be relative instead of absolute but it didn't seem to solve the problem. It is pretty difficult to see if it actually helps tho because the error is not occurring in consistent way(not on a specific test).

@Murali-Puvvada the bash scripts is a workaround. tests that should take 300ms for example with the cypress run command takes 1000-3000ms instead because it rerun the chrome instance every time. Also: we don't have cypress e2e in our application(only component tests.)

I've uploaded the debug output to this public file(700mb log file) when I run all the tests: https://drive.google.com/file/d/1-2KOb6KV1SyOc_hBi2c2DK40gRtFeeop/view?usp=sharing

Would love any help regarding the issue.

here is the error I'm talking about:
image

@KaelWD
Copy link

KaelWD commented Mar 7, 2023

We're seeing this in Vuetify's CI too: https://github.com/vuetifyjs/vuetify/actions/runs/4354367755/jobs/7609586657
It isn't always the same file, usually src/cypress/support/index.ts but I've seen spec files too like src/components/VInput/__tests__/VInput.spec.cy.tsx
We're still on Vite 3.2.5, I'll try updating to 4.1.4 and see if that helps.

@matanAlltra
Copy link

matanAlltra commented Mar 7, 2023

@KaelWD
I'm using Vite 4 and it doesn't seem to help

@chojnicki
Copy link

chojnicki commented Mar 8, 2023

@lmiller1990 For us it's in docker and component testing. For months now... But what's weird:

It only happens on CI/CD (GH actions) where we using exact same docker container as on devs computers. On PC (docker) it works always.
We have around 200 tests and in around 1/4 deploys it crashes on random test.
It fails always on just single test, that is usually in the middle of list, so around 100th test. But not always.
Sometimes it works days without any issue, and then fails multiple times per day.
If I repeat workflow, it passes fully, but if it crashes again it is usually on same test.
It's really annoying because it slows deployment heavily, If I have to repeat testing in CI/CD multiple times, when I know tests are just fine....

I was using Vite from beginning, and had this issue for months, trying multiple Cypress, browers and Vite versions and any workaround I could find. Everything is up to date.

It's hard to debug because like I said in never happens locally, just on GH. I was suspecting memory leak, but cypress/docker is running with stable ram usage, low for GH limits.

What I tried recently was configuring "attempts" to tests, so even if it fails, it should work fine second time. But this is useless option in that scenario, because when Failed to fetch dynamically imported module happens, Cypress just stops running that test and second attempt never happens........

Recently I added 2 packages that constantly optimized itself to optimizationDeps.entries. And I think that helped a little, but random failed tests still occurs.

Now I'm expirecing "failed to fetch" not component file, but cypress index file.
Failed to fetch dynamically imported module: http://localhost:3000/__cypress/src/cypress/support/index.js Still on single test, and after rerun everything works.

@matanAlltra
Copy link

matanAlltra commented Mar 8, 2023

@chojnicki for me it's happening a lot of constantly. Currently the only workaround I've found was this bash script which run the tests separately one by one and if it fails than it retries for two times. Its significantly slower, but it does the job to only fail when a test actually fails. (we're running this bash script in GH actions too)

#!/bin/bash
ARGS=$@
all_failed=0

for file in $( find . -type f -name '*.spec.cy.tsx' ); do
    attempts=0
    while [ $attempts -lt 3 ]; do # Retry up to 3 times
        attempts=$((attempts+1))
        echo "Running $file (attempt $attempts)..."
        yarn cypress run $ARGS --component --browser chrome --spec $file && break
    done

    if [ $attempts -eq 3 ]; then # All attempts failed
        all_failed=1
        break
    fi
done

if [ $all_failed -eq 1 ]; then
    exit 1
fi

@lmiller1990
Copy link
Contributor

lmiller1990 commented Mar 8, 2023

We have an internal repro 🔥 FINALLY!

It's Cypress org private project, but dropping the link here so someone at Cypress can internally... https://cloud.cypress.io/projects/d9sdrd/runs/4193/test-results/2f924501-7fb2-4c80-b69f-819010c67c87

Now we've got a repro I can dig into it... for us it's CI only too, so sounds like a resources/race condition.

@lmiller1990
Copy link
Contributor

Related: vitejs/vite#11804

@matanAlltra
Copy link

matanAlltra commented Mar 8, 2023

FYI It seems I don't have a access to the link @lmiller1990

@chojnicki
Copy link

@lmiller1990 hope that repo you got will help, but if not, I think I could get access for you/cypress to our repo too.

@lmiller1990
Copy link
Contributor

@lmiller1990
Copy link
Contributor

lmiller1990 commented Mar 8, 2023

@chojnicki thanks a lot, I'll ping you if we need access, I think our reproduction should be enough. Is your reproduction consistent? CI only?

@matanAlltra sorry this reproduction repo is in our Cypress org but a private repo, I can't make it public it right now, but someone on our team will be able to see it and debug it. If anyone else can share a public repo with this issue, that would sure help, too!

@floroz
Copy link

floroz commented Sep 13, 2023

we don't use the __tests__ directories and have been experiencing the issue.

@RadomirNowak
Copy link

RadomirNowak commented Sep 13, 2023

the thing with __tests__ was something that allowed me to find the root issue, which is:
if you import packages inside your tests that are not imported in your app (so they are not prebundled by vite) then they will be dynamically optimized during test run which will result in the problem described in this thread.
@floroz @adam-thomas-privitar can you try to include your test files in the entries section like I mentioned before? Also another thing could be to include there a glob for all your files (as a test) e.g. **/*.ts

@maxie7
Copy link

maxie7 commented Sep 15, 2023

I have this issue in local environment (not CI) when I launch a single test.

vite 2.9.9
cypress 13.1.0
typescript 4.5.4
vue-tsc 0.35
vue 3.2.25
@vitejs/plugin-vue 2.3.3

image

I solved the problem. In my case it was a wrong relative import '@/...' instead of '../../' inside the test file.

@muratkeremozcan
Copy link

We were seeing this issue in our monorepo, over 1k cyct working well with webpack, but intermittently hanging when running large suites, CI or local. That was with node 14.

With node 18 the issue seems to have self healed.

@floroz
Copy link

floroz commented Sep 30, 2023

sorry @RadomirNowak haven't had time yet to test your workaround of vite.optimizeDeps. Will try to do it in the next days/week.

@muratkeremozcan we use node 18.16 and issue has always happened there, so can confirm that node 18 doesn't solve / mitigate the issue

@samjowen
Copy link

samjowen commented Oct 4, 2023

We've been stuck on this for about 3 weeks, would really love to hear from anyone with a workaround.

@adam-thomas-privitar
Copy link

adam-thomas-privitar commented Oct 5, 2023

the thing with __tests__ was something that allowed me to find the root issue, which is: if you import packages inside your tests that are not imported in your app (so they are not prebundled by vite) then they will be dynamically optimized during test run which will result in the problem described in this thread. @floroz @adam-thomas-privitar can you try to include your test files in the entries section like I mentioned before? Also another thing could be to include there a glob for all your files (as a test) e.g. **/*.ts

Just to let you know I am trying your workaround in CI currently, and I should be able to get back to you once its run a sufficient amount of times.

@jennifer-shehane jennifer-shehane added CI General issues involving running in a CI provider and removed CI: github actions labels Oct 5, 2023
@adamscybot
Copy link

adamscybot commented Oct 6, 2023

Hi. I'm adam-thomas-privitar on my personal account :). So I wouldn't say that my confidence is 100% due to the odd nature of this issue. But I can say so far, the problem has not reoccurred in my branch since adding the test files to the optimizeDeps.entries after kicking it off a fair few times. I was also able to re-enable a certain suite which in particular was causing us especially big problems (but not exclusively) in regards to this issue, which is really promising. Previously we were retrying that test at the CLI level 3 times and even that eventually was not sufficient. So the fact it's working without those retries strongly suggests there may be a path here.

I'm trying not to get my hopes up too much though since the real test will come when this gets merged to main next week and the build volume increases. So ultimately we still need to proceed with caution.

I will report back, but I feel there's sufficient data to say that anyone experiencing this should try it so we can add more data points. If it does fix it, @RadomirNowak is a legend :D, as this problem has truly haunted us at scale. If it suddenly fails, I might go and have a little cry somewhere and hang up my keyboard forever.

It's perhaps of note that we have our own substantial layer above cypress (test support stuff) that is imported only into tests, in its own package, to abstract common things, and so the proposition that this plays a part is very plausible.

I believe nearly all of the discourse about solutions that seem to work then don't is likely due to varying load on the agent that's executing the tests. It's a race condition in its current state, and so any fluctuation in speed affects it, and I've observed this myself. Also, the problem gets worse the more deps you add. If you are running tests in parallel that definitely adds to the randomness also, since that's also causing varying load between runs. Another reason is if you have many agents and some are more highly specced than others.

If it does emerge that this is the problem then I guess a PR to the Cypress vite integration package is needed to automatically merge the test files into optimizeDeps.entries .

@dyc3
Copy link

dyc3 commented Oct 7, 2023

Just tried adding optimizeDeps.entries to my config.

{
        optimizeDeps: {
		entries: ["tests/e2e/**/*.ts", "src/**/*.{ts,vue}"],
	},
}

(tests/e2e/ is where my project has the component tests relative to the vite config's root)

And I'm still getting the import error. Also, I'm able to reproduce this outside of CI using the headless mode.

@RadomirNowak
Copy link

RadomirNowak commented Oct 9, 2023

@dyc3 I had the same issue - turned out I was not catching all files with all import - in my case it was also cypress/support.ts file which was not caught by the src/**/* glob pattern in entries section.

it could be also a different problem on your side, as I'm using React so maybe there's something different in Vue 🤔
Anyways I'd recommend investigating the output of CLI when you run the tests for any [vite] ✨ new dependencies optimized: and also check contents of node_modules/.vite/deps and how they change between different test suites - ideally to mitigate the error the contents of this folder should always be the same - if it changes then there's some dependencies not caught by the optimizeDeps.entries option

@adamscybot
Copy link

adamscybot commented Oct 12, 2023

So its been a couple of days now and the problem has not come back in CI. As others have said, I think this is about capturing everything, including any test-support stuff as well as the test files themselves. Makes sense support file should be added here as well because that is imported separately by cypress and not from the tests.

@adamscybot
Copy link

Just a check in that over 2 weeks now the problem remains gone!

@catdoingcatstuff
Copy link

Any updates on this? We tried the latest workarounds suggested in here but still get the error (less frequently, but it still happens at least once a complete component test run). Looks like its always the component.ts for us.

@ThomasKientz
Copy link

@adamscybot optimizeDeps doesn't work for us. Still getting random errors on CI.

@marktnoonan marktnoonan removed their assignment Nov 20, 2023
@derHodrig
Copy link

Still a problem.
In my case, all test run fine locally but crashing randomly in pipeline, with the error mentioned. You can run the same job twice and where in the first run the Test A crashes and Test B works, in the second run both can crash or B crash and A works...

@OliJeffery
Copy link

I was encountering this and I think the message is a red herring. In switching between branches, I'd uninstalled @pinia/testing, which I frequently use on component tests. Once I reinstalled that, it worked.

@trevordavidlawrence
Copy link

trevordavidlawrence commented Dec 8, 2023

Chiming in here in the hopes of saving others some time.

We had had intermittent issues with component tests failing in CI. The errors always came near a [vite] ✨ new dependencies optimized line in the job log.

I was able to reproduce the issue locally by clearing node_modules/.vite/deps before running the tests, and adding:

optimizeDeps: {
  entries: ['cypress/**/*'],
},

to our vite.config.js solved those issues. Our test files (.cy.js) are all located within that directory.

That was fine for a while, but I just now had to address another similar issue, this time with a new dependency (vue-router) that was not previously imported directly by our tested components. I eventually figured out that the issue was due to the fact that we cache the node_modules directory in between jobs (we use GitLab CI/CD). I don't have a full understanding of the Vite internals, but I think it saw that (now stale) .vite/deps data and assumed it didn't need to optimize (pre-optimize?) the Cypress test files again. I added a simple rm -rf node_modules/.vite/deps to the Cypress test job, and that fixed the issue.

@OliJeffery
Copy link

I experienced this too - for me it was because I was switching between branches with different dependencies, and one of them somehow hadn't been reinstalled inbetween changing branches (in my case @pinia/testing). So while Cypress was reporting that it couldn't import components.js, it wasn't because that file was missing, it was because that file was erroring because of the missing import of @pinia/testing within it. Hope this helps people who end up here in the future (or me, when this happens again and see my own comment).

@asteed21
Copy link

asteed21 commented Feb 5, 2024

I was also unable to see any impact from manipulating the optimizeDeps option in the vite config, but I did want to document what worked for us in case it's useful to someone else as it turned out to be tertiary to the original issue documented. The issue for us turned out to be tied to CircleCI and the use of a static port in our vite config. Our CI config used the same executor to run both E2E tests and Component tests in parallel after a build/install job. The static port was required for the E2E tests to serve reliably for the cypress run, but Cypress component tests were also picking up the static port from the default vite config.

Though I initially thought the executors would be isolated, this post made me reconsider. Explicitly overriding the port in our cypress config file fixed this issue and prevented the error from occurring due to host conflicts on the same port.

import viteConfig from './vite.config.ts';

// override default config
const customViteConfig = {
  ...viteConfig({ mode: 'dev' }),
  server: {
    port: 3001,
  },
};

...

// add configuration for component dev server
  component: {
    devServer: {
      framework: 'react',
      bundler: 'vite',
      viteConfig: customViteConfig,
    },
... 

@duartmig
Copy link

I am facing same issue

@Adnan-kreiker
Copy link

Adnan-kreiker commented Apr 23, 2024

Running the tests in chrome fixed it for me npm run test:component -- --browser chrome
The tests fail when they run in electron

Update: fixed by excluding vuetify from the optimizeDeps

@MarcelMalke
Copy link

MarcelMalke commented May 23, 2024

Fixed by deleting node_modules then npm i - the oldest trick in the book - excluding restarting the device

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI General issues involving running in a CI provider CT Issue related to component testing npm: @cypress/vite-dev-server @cypress/vite-dev-server package issues prevent-stale mark an issue so it is ignored by stale[bot] Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team.
Projects
None yet