vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.76k stars 1.15k forks source link

ReferenceError on globals using Vitest #5759

Closed DanielRoodzant closed 2 months ago

DanielRoodzant commented 4 months ago

Describe the bug

When running vitest locally or in a pipeline I often get the following (or similar) errors:

ReferenceError: document is not defined
 ❯ Timeout.OD.update [as _onTimeout] node_modules/path/to/package/dist/components.mjs:12439:25
    12437|         return;
    12438|       const o = this.cookies;
    12439|       this.cookies = jf(document.cookie), this._checkChanges(o);
       |                         ^
    12440|     };
    12441|     const a = typeof document > "u" ? "" : document.cookie;
 ❯ listOnTimeout node:internal/timers:573:17
 ❯ processTimers node:internal/timers:514:7

This error originated in "src/components/component/__tests__/component.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
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
ReferenceError: window is not defined
 ❯ Timeout.Ur [as _onTimeout] node_modules/path/to/package/dist/components.mjs:10600:13
    10598|   };
    10599| }, Ur = () => {
    10600|   const e = window;
       |             ^
    10601|   if (e && e.utag && e.utag_data) {
    10602|     const { utag_data: t } = e;
 ❯ listOnTimeout node:internal/timers:573:17
 ❯ processTimers node:internal/timers:514:7

This error originated in "src/views/path/to/view/__tests__/view.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.
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

The errors do not occur every time tests are run and happen on multiple different files. The associated package displayed on the first line of the stack trace is a private utility package that my project uses. My running theory is that the errors are caused by hanging event listeners and timers. The Document error for example, is caused by the addChangeListener function from the universal-cookie package that vueUse uses for its UseCookies implementation.

My Vite config has the following test configuration:

test: {
    globals: true,
    environment: 'jsdom',
    setupFiles: ['src/tests/setup.ts'],
    coverage: {
      provider: 'v8',
      all: true,
      include: ['**/src'],
      exclude: [
        ...configDefaults.exclude,
        'paths/to/excludes'
      ],
      reporter: ['text', 'text-summary', 'cobertura']
    }
  }

I have tried mocking the functions that the errors occur on in a setup.ts file. But so far that has not solved the issue.

Reproduction

I have not been able to reproduce the problem in other projects.

Please let me know if I need to provide additional information.

System Info

System:
    OS: Windows 10 10.0.17763
    CPU: (4) x64 Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz
    Memory: 1.28 GB / 16.00 GB
  Binaries:
    Node: 20.6.1 - C:\Program Files\nodejs\node.EXE
    npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Internet Explorer: 11.0.17763.2989
  npmPackages:
    @vitejs/plugin-vue: 5.0.4 => 5.0.4
    @vitest/coverage-v8: 1.6.0 => 1.6.0
    vite: 5.2.8 => 5.2.8
    vitest: 1.6.0 => 1.6.0

Used Package Manager

npm

Validations

sheremet-va commented 4 months ago

These event listeners and timers need to be cleaned up by you - they are automatically removed when component unmounts, but you say they are called globally

DanielRoodzant commented 4 months ago

I do not call any listeners globally. Where did you read that from? I will edit it for clarification.

DanielRoodzant commented 4 months ago

@sheremet-va Any other thoughts that could be of help?

daniluk4000 commented 4 months ago

We get this randomly in CI and I have no idea why

daniluk4000 commented 4 months ago

image

Like at some point it works but....

We are using isolated forks

github-actions[bot] commented 3 months ago

Hello @DanielRoodzant. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with needs reproduction will be closed if they have no activity within 3 days.

sheremet-va commented 3 months ago

Again, this looks like a user error. Some code somewhere is not cleaned-up properly.