Unit tests failing on Github Actions

Hi everyone, first post. I just created a new project using the Ionic CLI. The unit tests ran perfectly. Then I added a .github folder and a workflow which runs the unit tests, and when I checked the code into GitHub, the unit tests failed.

Here’s what the error looks like:

⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯
ReferenceError: document is not defined
 ❯ Module.startTapClick node_modules/@ionic/core/components/index9.js:133:15
    131|   };
    132|   const doc = document;
    133|   doc.addEventListener('ionGestureCaptured', cancelActive);
       |               ^
    134|   doc.addEventListener('touchstart', onTouchStart, true);
    135|   doc.addEventListener('touchcancel', onTouchEnd, true);
 ❯ node_modules/@ionic/core/components/ion-app.js:21:113

This error was caught after test environment was torn down. Make sure to cancel any running tasks before test finishes:
- cancel timeouts using clearTimeout and clearInterval
- wait for promises to resolve using the await keyword

I am pretty sure that there’s no error in my GitHub workflow config, as I have used the same workflow for several other projects.

The specific command that is failing is “vitest run”. (The “run” subcommand disables watch mode, which is what you want when running on a CI pipeline). As mentioned, this command works when running locally.

Normally the message “document is not defined” happens when you forget to specify a test environment of jsdom, but that’s not the case here. The jsdom environment was already setup by the Ionic CLI wizard.

The exception gets thrown after the unit tests completed - the tests themselves succeeded with no errors.

I suspect that what’s happening is that some asynchronous process or timer is getting started as a side-effect of running the tests, and then doesn’t get cleaned up afterwards. (I tried manually doing a react-testing-library cleanup - no dice.) Unfortunately, whatever process or timer is not something I’m doing. Is there some Ionic process that I need to await for after the test runs?

Same problem. Squeaky clean project. This might be related to ReferenceError: document is not defined · Issue #990 · vitest-dev/vitest · GitHub. I took the proposed solution of switching to happy-dom, but that throws just another error.

I removed the vitest-legacy plugin added in fix(vue): add support for ionic supported browser versions (#1768) · ionic-team/starters@e3d3362 · GitHub . This seems to have solved the problem.

I found the solution. Add the following to your setupTests.ts file:

// Disable rendering of certain components which have issues running in jsdom
// @ts-ignore
window.Ionic = {
  config: {
    _testing: true,