vitest-dev / vitest

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

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

Closed thebjorn closed 5 months ago

thebjorn commented 5 months ago

Describe the bug

Following the docs (https://vitest.dev/guide/#writing-tests), I've created the following minimal test:

import { test, expect } from 'vitest';

test('sum test', () => {
    expect(1 + 2).toBe(3);
});

and I'm getting the following error:

 RUN  v1.4.0 c:/srv/work/vitest-problems

 ❯ src/test-sum.js (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/test-sum.js [ src/test-sum.js ]
TypeError: Cannot read properties of undefined (reading 'test')
 ❯ src/test-sum.js:3:1

package.json

    "devDependencies": {
        "@sveltejs/adapter-auto": "^3.1.1",
        "@sveltejs/kit": "^2.5.4",
        "@sveltejs/vite-plugin-svelte": "^3.0.2",
        "@vitest/coverage-v8": "^1.4.0",
        "svelte": "^5.0.0-next.80",
        "vite": "^5.1.6",
        "vitest": "^1.4.0"
    },

if I downgrade vitest to 0.26.0 (random version, saw it in another issue) then everything works.

 RUN  v0.26.3 c:/srv/work/vitest-problems

 ✓ src/test-sum.js (1)

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  17:53:04
   Duration  1.42s (transform 188ms, setup 0ms, collect 10ms, tests 2ms)

Done in 2.48s.

Reproduction

https://github.com/thebjorn/vitest-problems

Created with npm create svelte@latest vitest-problems modified vite.config.js to look for test files in jest format and added src/test-sum.js

    test: {
        include: ['src/**/test-*.js']
    }

(aside: the nice thing about jest is that it just worked..)

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
    Memory: 28.62 GB / 63.92 GB
  Binaries:
    Node: 21.4.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - C:\Program Files\nodejs\yarn.CMD
    npm: 8.8.0 - ~\AppData\Roaming\npm\npm.CMD
    pnpm: 8.15.3 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (122.0.2365.92)
    Internet Explorer: 11.0.22621.1

Used Package Manager

yarn

Validations

thebjorn commented 5 months ago

`"vitest": "^0.9.0" works, but "^1.0.0" does not... At this point I can't tell if I'm doing something wrong (I haven't done anything special, so I don't think so..) or if the docs just haven't been updated to a new syntax? (it's literally the first example in the docs, so that too seems unlikely...?)

AriPerkkio commented 5 months ago

How do I reproduce this error? Works fine here https://stackblitz.com/github/thebjorn/vitest-problems?file=src%2Ftest-sum.js

image

AriPerkkio commented 5 months ago

modified vite.config.js to look for test files in jest format (aside: the nice thing about jest is that it just worked..)

This is intentional https://github.com/vitest-dev/vitest/issues/3530. Considering files like test/test-utils.ts as test files is not ideal.

thebjorn commented 5 months ago

How do I reproduce this error?

(dev310) go|c:\srv\tmp> git clone https://github.com/thebjorn/vitest-problems
Cloning into 'vitest-problems'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 21 (delta 0), reused 21 (delta 0), pack-reused 0
Receiving objects: 100% (21/21), 31.64 KiB | 675.00 KiB/s, done.

(dev310) go|c:\srv\tmp> cd vitest-problems

(dev310) go|c:\srv\tmp\vitest-problems> node --version
v21.4.0

(dev310) go|c:\srv\tmp\vitest-problems> yarn --version
1.22.21

(dev310) go|c:\srv\tmp\vitest-problems> yarn
yarn install v1.22.21
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning "@sveltejs/vite-plugin-svelte > svelte-hmr@0.15.3" has incorrect peer dependency "svelte@^3.19.0 || ^4.0.0".
warning " > @vitest/coverage-c8@0.33.0" has incorrect peer dependency "vitest@>=0.30.0 <1".
[4/4] Building fresh packages...
Done in 1.48s.

(dev310) go|c:\srv\tmp\vitest-problems> npx vitest --run
8:30:51 AM [vite-plugin-svelte] Your are using Svelte 5.0.0-next.80. Svelte 5 support is experimental, breaking changes can occur in any release until this notice is removed.
work in progress:
 - svelte-inspector is disabled until dev mode implements node to code mapping
 - hmr for .svelte files is disabled until hmr api is implemented

 RUN  v1.4.0 c:/srv/tmp/vitest-problems

 ❯ src/test-sum.js (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/test-sum.js [ src/test-sum.js ]
TypeError: Cannot read properties of undefined (reading 'test')
 ❯ src/test-sum.js:3:1

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

 Test Files  1 failed (1)
      Tests  no tests
   Start at  08:30:51
   Duration  441ms (transform 15ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 123ms)

(dev310) go|c:\srv\tmp\vitest-problems> yarn add -D vitest@0.9.4
yarn add v1.22.21
[1/4] Resolving packages...
⠁ (node:19480) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning "@sveltejs/vite-plugin-svelte > svelte-hmr@0.15.3" has incorrect peer dependency "svelte@^3.19.0 || ^4.0.0".
warning " > @vitest/coverage-c8@0.33.0" has incorrect peer dependency "vitest@>=0.30.0 <1".
warning " > @vitest/coverage-v8@1.4.0" has incorrect peer dependency "vitest@1.4.0".
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 14 new dependencies.
info Direct dependencies
└─ vitest@0.9.4
info All dependencies
├─ @types/chai-subset@1.3.5
├─ @types/chai@4.3.12
├─ chai@4.4.1
├─ esbuild-windows-64@0.14.54
├─ function-bind@1.1.2
├─ hasown@2.0.2
├─ is-core-module@2.13.1
├─ local-pkg@0.4.3
├─ path-parse@1.0.7
├─ resolve@1.22.8
├─ supports-preserve-symlinks-flag@1.0.0
├─ tinypool@0.1.3
├─ tinyspy@0.3.3
└─ vitest@0.9.4
Done in 1.53s.

(dev310) go|c:\srv\tmp\vitest-problems> npx vitest --run

 RUN  v0.9.4 c:/srv/tmp/vitest-problems

8:31:20 AM [vite-plugin-svelte] Your are using Svelte 5.0.0-next.80. Svelte 5 support is experimental, breaking changes can occur in any release until this notice is removed.
work in progress:
 - svelte-inspector is disabled until dev mode implements node to code mapping
 - hmr for .svelte files is disabled until hmr api is implemented

 √ src/test-sum.js (1)

Test Files  1 passed (1)
     Tests  1 passed (1)
      Time  1.39s (in thread 1ms, 139434.73%)

(dev310) go|c:\srv\tmp\vitest-problems>

modified vite.config.js to look for test files in jest format (aside: the nice thing about jest is that it just worked..)

This is intentional https://github.com/vitest-dev/vitest/issues/3530. Considering files like test/test-utils.ts as test files is not ideal.

That is certainly one opinion (although why you wouldn't simply call it test/utils.js is a mystery), but for some of us api-stability, backwards compatibility, and compatibility with other languages' testing tools is more important - I realize this isn't true for everyone.

AriPerkkio commented 5 months ago

Start by resolving these issues that your package manager is warning about. These are likely pulling multiple version of Vitest into your setup.

(dev310) go|c:\srv\tmp\vitest-problems> yarn yarn install v1.22.21 [1/4] Resolving packages... [2/4] Fetching packages... [3/4] Linking dependencies... warning "@sveltejs/vite-plugin-svelte > svelte-hmr@0.15.3" has incorrect peer dependency "svelte@^3.19.0 || ^4.0.0". warning " > @vitest/coverage-c8@0.33.0" has incorrect peer dependency "vitest@>=0.30.0 <1". [4/4] Building fresh packages... Done in 1.48s.

thebjorn commented 5 months ago

I'm still getting the error after removing the two coverage packages:

(dev310) go|c:\srv\tmp\vitest-problems> rm -rf node_modules yarn.lock

(dev310) go|c:\srv\tmp\vitest-problems> yarn
yarn install v1.22.21
info No lockfile found.
[1/4] Resolving packages...
⠁ (node:55724) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning "@sveltejs/vite-plugin-svelte > svelte-hmr@0.15.3" has incorrect peer dependency "svelte@^3.19.0 || ^4.0.0".
[4/4] Building fresh packages...
success Saved lockfile.
Done in 6.94s.

(dev310) go|c:\srv\tmp\vitest-problems> npx vitest --run
9:20:18 AM [vite-plugin-svelte] Your are using Svelte 5.0.0-next.80. Svelte 5 support is experimental, breaking changes can occur in any release until this notice is removed.
work in progress:
 - svelte-inspector is disabled until dev mode implements node to code mapping
 - hmr for .svelte files is disabled until hmr api is implemented

 RUN  v1.4.0 c:/srv/tmp/vitest-problems

 ❯ src/test-sum.js (0)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/test-sum.js [ src/test-sum.js ]
TypeError: Cannot read properties of undefined (reading 'test')
 ❯ src/test-sum.js:3:1

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

 Test Files  1 failed (1)
      Tests  no tests
   Start at  09:20:18
   Duration  666ms (transform 22ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 158ms)

Another developer here has verified that they get the problem.

Interestingly, I do not see the problem when running under wsl, nor when running under windows-latest on github...(?)

AriPerkkio commented 5 months ago

(dev310) go|c:\srv\tmp\vitest-problems> npx vitest --run

Could you also try if running vitest through package.json scripts help? I guess npx could use another version if it's available as global package. So instead of npx vitest --run use yarn test.

thebjorn commented 5 months ago

same result.

AriPerkkio commented 5 months ago

I'm unable to reproduce this issue on Github Codespaces or Stackblitz. It works fine all the time.

thebjorn commented 5 months ago

Hmm... just for fun I tried using pnpm instead of yarn and that worked...(?)

thebjorn commented 5 months ago

Neither npm nor yarn works.

AriPerkkio commented 5 months ago

Both NPM and Yarn work in Github Codespaces using your reproduction setup. I have no idea how to reproduce this issue. 🤷

github-actions[bot] commented 5 months ago

Hello @thebjorn. 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.