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.

Random test crashes with "window is not defined" error

See original GitHub issue

Describe the bug

From time to time I get the error ReferenceError: window is not defined.

image

or

image

Reproduction

I couldn’t find any pattern. It just happens sometimes.

This happens with any value of environment, and happy-dom and jsdom.

vitest.config.ts:

/// <reference types="vitest" />

import path from 'path';
import { defineConfig } from 'vite';

// eslint-disable-next-line import/no-default-export -- by design
export default defineConfig({
  test: {
    environment: 'happy-dom',
    include: ['**/*.test.ts'],
    setupFiles: [path.resolve(__dirname, 'vitest.setup.ts')],
  },
});

System Info

System:
    OS: macOS 12.2.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 2.14 GB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.15.0 - /usr/local/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 8.5.5 - /usr/local/bin/npm
  Browsers:
    Brave Browser: 101.1.38.111
    Chrome: 101.0.4951.54
    Chrome Canary: 103.0.5057.0
    Firefox: 100.0
    Safari: 15.3
  npmPackages:
    vite: ^2.9.9 => 2.9.9 
    vitest: ^0.12.4 => 0.12.4

Used Package Manager

npm

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
odinhocommented, Nov 29, 2022

It seems like vitest should not tear down the environment if there’s still stuff in the event queue then?

I get that this is some async thing that’s coming in late, but usually it would find window and it’d be a-ok. Never had this issue with jest which we just came from (same tests), which might indicate that they do wait(?).

If it is meant to be a feature, then it should always warn about things being run after teardown, and not just when it crashes due to an env issue.

These are some quite big integration tests, and I’m also unable to tell which tests they came from, even almost-so. The console.log I put in doesn’t happen for this after-teardown run, only for things inside the test. 🤔

0reactions
dartesscommented, Oct 2, 2022

@vincent-lecrubier-skydio unfortunately I can’t give you the right solution. I just refactored the code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest test fails with "window is not defined" - Stack Overflow
The issue was because in my Jest configuration I had testEnvironment set to node when it should've been jsdom .
Read more >
384014 - strange js error "window is not defined"
"window is not defined is strange js error. when unload4e.html is opened, the link is clicked and then |reload| is clicked there is...
Read more >
Stop error or blue screen error troubleshooting - Windows Client
Learn advanced options for troubleshooting stop errors, also known as blue screen errors or bug check errors.
Read more >
How to solve "window is not defined" errors in React and Next.js
Basically in HOC there was a class component and inside the render block before the returnning the Original component I wanted to check...
Read more >
How to fix almost ANY Windows Bluescreen, Error, Crash OR ...
Fix Windows crashing, errors & bluecreens - Try it out! 25% discount code for software: PAN20Windows 10 Pro OEM Key (16.5$): ......
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