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

windows drive casing causes - TypeError: Cannot read properties of undefined (reading 'test') #5251

Closed
6 tasks done
dlemonhed opened this issue Feb 20, 2024 · 2 comments · Fixed by #6779
Closed
6 tasks done
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) windows

Comments

@dlemonhed
Copy link

dlemonhed commented Feb 20, 2024

Describe the bug

When adding vitest to an existing project & all tests are failing with TypeError: Cannot read properties of undefined (reading 'test').

Looks like this is due to my windows cmd line having a lower-case drive letter. Tests successfully run in powershell, & vs code console.

Adding a vitest.config.ts file w/ a normalized root property worked around the issue.

export default mergeConfig(
  viteConfig,
  defineConfig({
    test: {
      environment: 'jsdom',
      exclude: [...configDefaults.exclude, 'e2e/*']
      
      // including this line, it works, commenting out, it fails
      // , root: fileURLToPath(new URL('./', import.meta.url))
    }
  })
)

Below is the stack from the test exception getting thrown - note the varying drive letters:

TypeError: Cannot read properties of undefined (reading 'test')
    at Object.<anonymous> (file:///c:/github/vue/vitest-repro/node_modules/@vitest/runner/dist/index.js:256:22)
    at Module.chain2 (file:///c:/github/vue/vitest-repro/node_modules/@vitest/runner/dist/chunk-tasks.js:98:17)
    at c:\github\vue\vitest-repro\src\components\__tests__\HelloWorld.spec.ts:7:1
    at VitestExecutor.runModule (file:///C:/github/vue/vitest-repro/node_modules/vite-node/dist/client.mjs:362:5)
    at VitestExecutor.directRequest (file:///C:/github/vue/vitest-repro/node_modules/vite-node/dist/client.mjs:346:5)
    at VitestExecutor.cachedRequest (file:///C:/github/vue/vitest-repro/node_modules/vite-node/dist/client.mjs:189:14)
    at VitestExecutor.executeId (file:///C:/github/vue/vitest-repro/node_modules/vite-node/dist/client.mjs:165:12)
    at collectTests (file:///C:/github/vue/vitest-repro/node_modules/@vitest/runner/dist/index.js:578:7)
    at startTests (file:///C:/github/vue/vitest-repro/node_modules/@vitest/runner/dist/index.js:908:17)
    at file:///C:/github/vue/vitest-repro/node_modules/vitest/dist/chunks/runtime-runBaseTests.9RbsHRbU.js:114:7

Reproduction

Using the default example on StackBlitz

Download to a local folder on windows - i.e. C:\repro\vitest-repro.

From a windows cmd line (not powershell or vs code console), make sure your drive prompt is lower-case.

cd c:\repro\vitest-repro

Run tests

npm run test

 DEV  v1.3.1 c:/work/vite-repro

 ❯ test/suite.test.ts (0)
 ❯ test/basic.test.ts (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 2 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/basic.test.ts [ test/basic.test.ts ]
TypeError: Cannot read properties of undefined (reading 'test')
 ❯ test/basic.test.ts:6:1

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/2]⎯

 FAIL  test/suite.test.ts [ test/suite.test.ts ]
Error: No test suite found in file C:/work/vite-repro/test/suite.test.ts
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/2]⎯

 Test Files  2 failed (2)
      Tests  no tests
   Start at  11:53:59
   Duration  847ms (transform 107ms, setup 0ms, collect 208ms, tests 0ms, environment 1ms, prepare 475ms)

If you add a vitest.config.ts w/ the root property as above, it resolves

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (8) x64 Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz
    Memory: 4.73 GB / 15.92 GB
  Binaries:
    Node: 18.13.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.10.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 121.0.6167.185
    Edge: Chromium (121.0.2277.128)
    Internet Explorer: 11.0.22621.1

Used Package Manager

npm

Validations

@SecretAgentKen
Copy link

Note that while adding root: fileURLToPath(new URL('./', import.meta.url))) does fix the issue for a vitest run, it does not fix the issue for a plain vitest. Any updated tests that cause a watch to trigger will run a cached version of the test file and not the updated one.

@sheremet-va sheremet-va added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Jul 1, 2024
@leonbloy
Copy link

leonbloy commented Aug 6, 2024

I've also lost several hours because of this.

In my case, my beforeAll(...) threw a

TypeError: Cannot read properties of undefined (reading 'on')

in a seemingly random way (it worked inside a VsCode cmd terminal, but not in my plain cmd terminal)
It took me a while (i had to dig inside beforeAll source to guess that that getCurrentSuite() was returning undefined, and then that it had to do with the case of my Windows drive letter.

It think that it would be good practice to FAIL FAST here: I've check that the callers to getCurrentSuite() never expect to get undefined, hence, it should inside throw a descriptive error (and perhaps, if inside windows, warn about drive letter in lowercase?).

I bet, for example, that #5401 (close for comments) is related to this,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) windows
Projects
None yet
5 participants