4

Upon trying to setting up a test with vitest and running npm run test, an error is being raised with test():

  • TypeError: Cannot read properties of undefined (reading 'test')

When the explicit import is declared as import {test} from "vitest" in the test file while globals: true is added to the configuration the error is raised.

When the explicit import import {test} from "vitest" is commented out and globals: true is added to the configuration the error is not raised.

When globals: true is removed from the configuration, just the explicit import import {test} from "vitest" raises the same error.

How can this error be resolved so just the explicit import statement of import {test} from "vitest" can be declared without the globals key added to the config object?

file.test.js

import { test } from "vitest";

test ("Test", () => {});

vite.config.js

/// <reference types="vitest" />
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  test: {
    globals: true,
    coverage: {
      provider: "v8",
      reporter: ["json", "html", "text"]
    },
    environment: "happy-dom"
  }
})

package.json

{
  "name": "markdown_previewer",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
    "preview": "vite preview",
    "test": "vitest",
    "coverage": "vitest run --coverage"
  },
  "dependencies": {
    "marked": "^12.0.2",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^6.4.2",
    "@testing-library/react": "^15.0.5",
    "@testing-library/user-event": "^14.5.2",
    "@types/react": "^18.2.66",
    "@types/react-dom": "^18.2.22",
    "@vitejs/plugin-react": "^4.2.1",
    "@vitest/coverage-v8": "^1.5.3",
    "@vitest/ui": "^1.5.3",
    "eslint": "^8.57.0",
    "eslint-plugin-react": "^7.34.1",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-react-refresh": "^0.4.6",
    "happy-dom": "^14.7.1",
    "vite": "^5.2.0",
    "vitest": "^1.5.3"
  }
}

3
  • Have you found any remedy? I'm stuck having the same problem right now. Commented May 12, 2024 at 20:30
  • If you are using jest-dom it requires globals: true. I'm assuming because Jest uses globals by default and jest-dom is an extension to that. Commented May 14, 2024 at 6:27
  • Upon examining the Vitest docs again...here's an explanation: vitest.dev/guide/… Commented May 20, 2024 at 18:05

1 Answer 1

2

The issue is not actually related to globals: true even though that can get you partially around the issue. Of all things, it's likely your path and related to this bug: https://github.com/vitest-dev/vitest/issues/5251

If you're on Windows and you're in a c: based command prompt, try again in C: (Uppercase) instead. From my testing I've seen the following:

For lower c:

  • global:true and no imports - vitest run works but real-time updates for vitest do not
  • With imports - vitest fails across the board with your type error
  • With the root: fix outlined in the bug report - vitest run works but real-time updates for vitest do not

For upper C: no mitigations are necessary.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm on Windows 10, but using Git Bash as my terminal. I'm not clear if that would still have an impact?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.