vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Use closest `vitest.config.ts` from the test file for mono repo

Open vlechemin opened this issue 3 years ago • 8 comments

For now there isnt a way to set a configuration file for a package inside a monorepo.

https://github.com/zxch3n/vitest-explorer/issues/6#issuecomment-1090037324

Thanks!

vlechemin avatar Apr 06 '22 09:04 vlechemin

Yes! In my setup, each sub-package has it's own vitest.config.ts file and its own test command. However, the CWD is defaulting to the root monorepo dir.

mikestopcontinues avatar May 04 '22 14:05 mikestopcontinues

Hi! I'm also using monorepo. I have thoughts for other kind of setup (inspired by turborepo example) where:

  • A monorepo that is using only vite just for test (other projects may use webpack etc)
  • There will be a subpackage for only test config(vitest config)
  • Other subpackages will consume the shared test config package

Not sure if this kind of setup is a good idea, here's a sample from turborepo, it's jest but I think it's similar.

norman-ags avatar May 08 '22 14:05 norman-ags

The same here, I use turborepo for monorepo, and just some packages in it using vitest, I don't want to put vitest config to root dir.

laozhu avatar May 20 '22 04:05 laozhu

+1

kevinvalk avatar Jun 03 '22 06:06 kevinvalk

Same. I can't use the extension in my turborepo without this enhancement

dgdolzhenko avatar Jun 09 '22 10:06 dgdolzhenko

Same issue here

marsidev avatar Jun 13 '22 02:06 marsidev

+1

redplant3d avatar Jun 15 '22 12:06 redplant3d

As a note: setting commandLine to something like npm exec vitest -ws -- --passWithNoTests will work if you're only running individual test suites, but will not work for running all tests at the same time. Presumably that's because when the extension runs with a filter of files just within one particular package, vitest will run in each package but exit without doing anything because the filter it passes only matches files in one package. However, if that doesn't hold, as soon as the API reports results for the first package that has tests, the process is killed before vitest is run on any subsequent packages. Also worth noting that if you have many packages, running vitest against every single package when you only need to run it for one is quite slow. :)

I'd imagine the desired solution here would be something like what dbaeumer.vscode-eslint does with the eslint.workingDirectories setting - you can either configure (or let it determine via the presence of package.json and eslint config files) what directories are usable CWDs to launch eslint from - so if packages/mypackage is a valid working directory, when packages/mypackage/test.ts needs to be run against, packages/mypackage is picked up on as the directory that should be used.

luxaritas avatar Aug 31 '22 18:08 luxaritas

Same here turborepo & vitest, will be extremely useful, its a blocker for us from using the vscode extension

yarinsa avatar Oct 24 '22 09:10 yarinsa

+1 can't use the extension with my monorepo :/

jameslounds avatar Nov 04 '22 21:11 jameslounds

Add me to the camp of people who would love to use this, but can't because we use vitest in a monorepo

jamesandersonwalsh avatar Jan 11 '23 21:01 jamesandersonwalsh

I've tackled some issues about vitest not detecting the correct path when starting (#88), and I think that this problem may be related.

The monorepo samples of this project seems broken, can one of you please try it on your machine?

  • clone the project:
git clone https://github.com/vitest-dev/vscode.git
npm install
  • Click on "Run and Debug", select "Run Extension Monorepo Sample" then click play
  • A new VSCode window will open: in this window run pnpm install
  • Open the extension, select the test and start it
  • In the Output panel > vitest you should see [Error 9:44:07 AM] Test not found: file:///Users/ngirardin/dev/playground/vitest-vscode/samples/monorepo/packages/react/test/basic.test.tsx/Link changes the class when hovered@0

Thanks!

ngirardin avatar Jan 23 '23 09:01 ngirardin

Any update? I need to use it with Turborepo.

sandercoffee avatar Feb 06 '23 12:02 sandercoffee

If you use vscode workspaces (honestly you should if you use any monorepo). That should make it all work across projects, without any config needed. You might need to set vitest.commandLine to pnpm exec vitest or similar

// file: ROOT/.vscode/project-name.code-workspace
{
	"folders": [
		{
			"name": "Project-name",
			"path": "../"
		},
		{
			"name": "App 1",
			"path": "../apps/first"
		},
		{
		       "name": "Package 1",
		       "path": "../packages/first"
		 } 
      ], 
      "settings": {
            "vitest.commandLine": "pnpm exec vitest",
            // these settings are applied to all workspaces
      }
}

jameslounds avatar Feb 06 '23 12:02 jameslounds

Set "vitest.commandLine": "pnpm exec vitest" in workspace didn't work for me, tests keeps running forever

My structure image

The errors image

Test list (disabled) image

Root folder image

Packages/Ui image

Any workaround?

sandercoffee avatar Feb 07 '23 11:02 sandercoffee

Hi guys,

We made good progress on https://github.com/vitest-dev/vscode/pull/125 which should resolve this kind of issue.

Could you try it on your machine to see it fix your issue?

ngirardin avatar Feb 07 '23 11:02 ngirardin

You need to open the workspace. You can either run code <path-to-workspace.code-workspace , or just click the open workspace button with the workspace file open

jameslounds avatar Feb 07 '23 11:02 jameslounds

Hi guys! The PR has been merged, can you try v0.2.39 and let us know if it solved your issues?

If not, please open the output tab > vitest and past the logs to help us debug.

ngirardin avatar Feb 09 '23 08:02 ngirardin

hey @ngirardin, I just tried but it looks like it doesn't detect the version correctly... Or do I missing something? Thank you 2023-02-09 at 12 44

tomtobac avatar Feb 09 '23 11:02 tomtobac

@tomtobac could you open the terminal pane, go to the output tab, select « vitest » and paste the logs here?

ngirardin avatar Feb 09 '23 12:02 ngirardin

@tomtobac could you open the terminal pane, go to the output tab, select « vitest » and paste the logs here?

[INFO 14:08:06] Vitest Workspace [js]: Vitest version = undefined
[Error 14:08:06] Because Vitest version < 0.12.0 for every workspace folder, run/debug/watch tests from Vitest extension disabled.

Basically the same as the popup :/

tomtobac avatar Feb 09 '23 13:02 tomtobac

@ngirardin here is my log. I'm in NX monorepo.

[INFO 15:32:06] Trying to get vitest version from /Users/Shared/repos/nx-vitest/node_modules/.bin/vitest -v...
[INFO 15:32:07] vitest/0.28.4 darwin-arm64 node-v16.19.0

[INFO 15:32:07] Vitest Workspace [nx-vitest]: Vitest version = 0.28.4
[INFO 15:32:23] Tests run start
[INFO 15:32:23] [Workspace "nx-vitest] Run tests from workspace
[INFO 15:32:23] [Execute Vitest] /Users/Shared/repos/nx-vitest/node_modules/.bin/vitest /Users/Shared/repos/nx-vitest/apps/functions/src/firestore/theme/on-create.spec.ts -t Theme onCreate
[INFO 15:32:23] Start api process at port 53234
[INFO 15:32:23] [RUN] /Users/Shared/repos/nx-vitest/node_modules/.bin/vitest /Users/Shared/repos/nx-vitest/apps/functions/src/firestore/theme/on-create.spec.ts -t Theme onCreate
[INFO 15:32:23] [RUN.cwd] /Users/Shared/repos/nx-vitest
[INFO 15:32:23] WS Close
[INFO 15:32:24] RECONNECT
[INFO 15:32:24] WS Close
[INFO 15:32:24] RECONNECT
[INFO 15:32:24] WS Opened
[INFO 15:32:24] 

 DEV  v0.28.4 /Users/Shared/repos/nx-vitest

      API started at http://127.0.0.1:53234



[INFO 15:32:25] Vitest api process finished
[Error 15:32:25] Test not found: file:///Users/Shared/repos/nx-vitest/apps/functions/src/firestore/theme/on-create.spec.ts/Theme onCreate should create usedInDocument when property is included in created doc@1
[INFO 15:32:25] [Workspace "nx-vitest] Test run finished
[INFO 15:32:25] Tests run end
[INFO 15:32:25] WS Close
[INFO 15:32:25]  ❯ apps/functions/src/firestore/theme/on-create.spec.ts  (0 test)



 Test Files  1 failed (1)

      Tests  no tests

   Start at  15:32:24

   Duration  548ms (transform 72ms, setup 0ms, collect 0ms, tests 0ms)





 FAIL  Tests failed. Watching for file changes...

       press h to show help, press q to quit

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



 FAIL  apps/functions/src/firestore/theme/on-create.spec.ts [ apps/functions/src/firestore/theme/on-create.spec.ts ]

Error: Failed to load url @nx-vitest/data-mocks (resolved id: @nx-vitest/data-mocks). Does the file exist?

 ❯ loadAndTransform node_modules/.pnpm/[email protected]_qal5rxk5qwxlcnknltbi62urva/node_modules/vite/dist/node/chunks/dep-3007b26d.js:39457:21



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

I'm able to run tests in cli with: /Users/Shared/repos/nx-vitest/node_modules/.bin/vitest -r /Users/Shared/repos/nx-vitest/apps/functions /Users/Shared/repos/nx-vitest/apps/functions/src/firestore/theme/on-create.spec.ts

My actual monorepo root is: /Users/Shared/repos/nx-vitest

diginikkari avatar Feb 09 '23 13:02 diginikkari

/usr/local/bin/node ./node_modules/vitest/vitest.mjs [ProjectRootPath] [vitest.commandLine] [ProjectRootPath]/apps/Somepackage/src/utils/__tests__/sometest.test.ts --api.port 64217 --api.host 127.0.0.1
 DEV  v0.28.5 [ProjectRootPath]
      API started at http://127.0.0.1:64217
 ❯ packages/testing/__tests__/sometest.test.ts  (0 test)
 ❯ apps/Somepackage/src/utils/__tests__/some-other-test.test.ts  (0 test)
⎯⎯⎯⎯⎯⎯ Failed Suites 2 ⎯⎯⎯⎯⎯⎯⎯
 FAIL  packages/testing/__tests__/some-other-test.test.ts  [ packages/testing/__tests__/some-other-test.test.ts  ]

Running into similar issues. The plugin can't find any tests in the testfiles. Commandline works. I added a workspace file and put the vitest.commandLine config there. The plugin doesn't seem to run that actual command though, it just adds it where i wrote [vitest.commandLine] in the log above. I tried playing around with different commands to get it to work even in one of the packages but can't get it to work. Very unfortunate that this plugin doesn't work in monorepos

axelinternet avatar Feb 28 '23 10:02 axelinternet

+1

smoglica avatar Mar 06 '23 17:03 smoglica

Worked for me thank you. I had to use include inside config, and used command line to specify the config. Otherwise vitest.include didnt do it for me

Inviz avatar Mar 10 '23 15:03 Inviz

@diginikkari Im getting issues with debug mode. in my monorepo the run mode works, but not debug mode. Here are the commands

[INFO 8:37:31 AM] Tests run start
[INFO 8:37:31 AM] [Workspace "2023-03 ghostai-dev] Run tests from workspace
[INFO 8:37:31 AM] [Execute Vitest] yarn test /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev/obsidian-plugin/test/services/documentService.test.ts -t buildHeirarchy it handles empty document
[INFO 8:37:31 AM] Start api process at port 58741
[INFO 8:37:31 AM] [RUN] yarn test /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev/obsidian-plugin/test/services/documentService.test.ts -t buildHeirarchy it handles empty document
[INFO 8:37:31 AM] [RUN.cwd] /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev
[INFO 8:37:31 AM] WS Close
[INFO 8:37:32 AM] Tests run start
[INFO 8:37:32 AM] [Workspace "2023-03 ghostai-dev] Run tests from workspace
[INFO 8:37:32 AM] [Execute Vitest] yarn test /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev/obsidian-plugin/test/services/documentService.test.ts -t buildHeirarchy it handles empty document
[INFO 8:37:32 AM] Start api process at port 58743
[INFO 8:37:32 AM] [RUN] yarn test /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev/obsidian-plugin/test/services/documentService.test.ts -t buildHeirarchy it handles empty document
[INFO 8:37:32 AM] [RUN.cwd] /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev
[INFO 8:37:32 AM] WS Close
[INFO 8:37:32 AM] RECONNECT
[INFO 8:37:32 AM] WS Close
[INFO 8:37:32 AM] RECONNECT
[INFO 8:37:32 AM] WS Close
[INFO 8:37:32 AM] RECONNECT
[INFO 8:37:32 AM] WS Close
[INFO 8:37:33 AM] RECONNECT
[INFO 8:37:33 AM] WS Close
[INFO 8:37:33 AM] RECONNECT
[INFO 8:37:33 AM] WS Close
[INFO 8:37:33 AM] RECONNECT
[INFO 8:37:33 AM] WS Close
[INFO 8:37:33 AM] RECONNECT
[INFO 8:37:33 AM] WS Close
[INFO 8:37:34 AM] RECONNECT
[INFO 8:37:34 AM] WS Opened
[INFO 8:37:34 AM] RECONNECT
[INFO 8:37:34 AM] WS Opened
[INFO 8:37:34 AM] 

 DEV  v0.30.1 /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev/obsidian-plugin

      API started at http://127.0.0.1:58743



[INFO 8:37:34 AM] 

 DEV  v0.30.1 /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev/obsidian-plugin

      API started at http://127.0.0.1:58741



[INFO 8:37:34 AM] Vitest api process finished
[INFO 8:37:34 AM] [Workspace "2023-03 ghostai-dev] Test run finished
[INFO 8:37:34 AM] Tests run end
[INFO 8:37:34 AM] Vitest api process finished
[INFO 8:37:34 AM] [Workspace "2023-03 ghostai-dev] Test run finished
[INFO 8:37:34 AM] Tests run end
[INFO 8:37:34 AM] WS Close
[INFO 8:37:34 AM] WS Close
[INFO 8:37:34 AM] ----------------------------------------

----------------------------------------

----------------------------------------

----------------------------------------

 ✓ test/services/documentService.test.ts  (9 tests | 8 skipped) 2ms



 Test Files  1 passed (1)

      Tests  1 passed | 8 skipped (9)

   Start at  08:37:34

   Duration  433ms (transform 108ms, setup 9ms, collect 139ms, tests 2ms, environment 0ms, prepare 62ms)





 PASS  Waiting for file changes...

       press h to show help, press q to quit


this is debug mode that does not work.

[INFO 8:38:19 AM] [Execute Vitest] yarn test /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev/obsidian-plugin/test/services/documentService.test.ts -t buildHeirarchy it handles empty document
[INFO 8:38:19 AM] Start api process at port 58758
[INFO 8:38:19 AM] [RUN] yarn test /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev/obsidian-plugin/test/services/documentService.test.ts -t buildHeirarchy it handles empty document
[INFO 8:38:19 AM] [RUN.cwd] /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev
[INFO 8:38:19 AM] WS Close
[INFO 8:38:19 AM] Debugging started
[INFO 8:38:19 AM] RECONNECT
[INFO 8:38:19 AM] WS Close
[INFO 8:38:20 AM] RECONNECT
[INFO 8:38:20 AM] WS Opened
[INFO 8:38:20 AM] Vitest api process finished
[INFO 8:38:20 AM] WS Close
[INFO 8:38:20 AM] API PROCESS EXIT
[INFO 8:38:20 AM] Vitest api process finished
[Error 8:38:20 AM] 
Failed to get any result
( Vitest should be configured to be able to run from project root )

Error when running
    yarn test /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev/obsidian-plugin/test/services/documentService.test.ts -t buildHeirarchy it handles empty document

cwd: /Users/shravansunder/Documents/dev/project-dev/2023-03 ghostai-dev
node: v18.12.1

env.PATH: /Users/shravansunder/miniforge3/bin:/Users/shravansunder/miniforge3/condabin:/Users/shravansunder/.volta/bin:/Users/shravansunder/.pyenv/shims:/Library/Frameworks/Python.framework/Versions/3.10/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/shravansunder/.cargo/bin:/Users/shravansunder/.foundry/bin

[Error 8:38:20 AM] Debugging started

cli works with yarn test. My yarn test in root package.json is configured to run in the workspace: "test": "yarn workspace obsidian-ghostai-plugin test"

ShravanSunder avatar Apr 18 '23 12:04 ShravanSunder

Debug is currently not working with my setup. I am using turborepo with pnpm

Root pnpm-workspace.yaml:

packages:
  - "*"
  # exclude packages that are inside test directories etc
  - "!**/test/**"
  - "!.vscode"
  - "!node_modules"

Root turbo.json:

{
    "$schema": "https://turbo.build/schema.json",
    "pipeline": {
        "build": {
            "inputs": ["src/**"],
            "outputs": ["lib/**"],
            "dependsOn": ["^build"]
        },
        "test": {
            "inputs": ["src/**", "__tests__/**"],
            "dependsOn": ["package"]
        },
        "lint": {},
        "format": {},
        "format-check": {},
        "package": {
            "inputs": ["src/**", "lib/**"],
            "outputs": ["dist/**"],
            "dependsOn": ["build"]
        },
        "all": {}
    }
}

package vitest.config.ts:

import { defineConfig } from "vitest/config";

export default defineConfig({
    test: {
        clearMocks: true,
        environment: "node",
    },
});
component version
OS 5.10.0-23-amd64 #1 SMP Debian 5.10.179-1 (2023-05-12) x86_64 GNU/Linux
nodejs 18.16.0
pnpm 8.6.0
vite 4.3.9
vitest 0.31.4
zixuanchen.vitest-explorer v0.2.41

VS Code output from Vitest:

[Error 9:15:31 AM] Error: Cannot get vitest version. Please open an issue at https://github.com/vitest-dev/vscode/issues and join the logs above.
[INFO 9:15:31 AM] Vitest Workspace [debug-action]: Vitest version = undefined
[INFO 9:15:31 AM] Vitest Workspace [setup-meta]: Vitest version = undefined
[INFO 9:15:31 AM] Vitest Workspace [ci-java]: Vitest version = undefined
[INFO 9:15:31 AM] Vitest Workspace [hook-action]: Vitest version = undefined
[INFO 9:15:31 AM] Vitest Workspace [get-version]: Vitest version = undefined
[Error 9:15:31 AM] Because Vitest version < 0.12.0 for every workspace folder, run/debug/watch tests from Vitest extension disabled.

davidwincent avatar Jun 05 '23 07:06 davidwincent

[Workaround] Below .vscode/settings.json config works for me for a mono repo (for both running and debugging tests via vitest extension). Hopefully this helps.

/workspace/.vscode/settings.json

{
  "vitest.enable": true,
  "vitest.commandLine": "/usr/bin/node /workspace/app-3/node_modules/.bin/vitest --root /workspace/app-3"
}

image

image

VSCode: 1.80.0
OS: Linux 45ed49a584cc 5.4.49-050449-generic #202006241630 SMP Wed Jun 24 16:36:16 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
nodejs: v18.16.1
npm: 9.7.2
vite: 4.4.2
vitest: 0.32.4
vue: 3.3.4
zixuanchen.vitest-explorer: v0.2.42
$ pwd
/workspace
$ /usr/bin/node /workspace/app-3/node_modules/.bin/vitest --root /workspace/app-3 --run

 RUN  v0.32.4 /workspace/app-3

 ✓ src/components/__tests__/HelloWorld.spec.ts (1)
   ✓ HelloWorld (1)
     ✓ renders properly

 Test Files  1 passed (1)
      Tests  1 passed (1)
   Start at  12:00:42
   Duration  1.01s (transform 167ms, setup 0ms, collect 124ms, tests 19ms, environment 298ms, prepare 172ms)
$ ls -a /workspace/
.  ..  app-3  .devcontainer  .git  .gitignore  LICENSE  .prettierrc  README.md  .vscode

# This is a sample project generated by `npm init vue@latest` with name `app-3`.
ls /workspace/app-3/
e2e       index.html    package.json       playwright.config.ts  public     src           tsconfig.app.json  tsconfig.node.json    vite.config.ts
env.d.ts  node_modules  package-lock.json  playwright-report     README.md  test-results  tsconfig.json      tsconfig.vitest.json  vitest.config.tsz`

abhishekrana avatar Jul 10 '23 12:07 abhishekrana

If you use vscode workspaces (honestly you should if you use any monorepo). That should make it all work across projects, without any config needed. You might need to set vitest.commandLine to pnpm exec vitest or similar

// file: ROOT/.vscode/project-name.code-workspace
{
	"folders": [
		{
			"name": "Project-name",
			"path": "../"
		},
		{
			"name": "App 1",
			"path": "../apps/first"
		},
		{
		       "name": "Package 1",
		       "path": "../packages/first"
		 } 
      ], 
      "settings": {
            "vitest.commandLine": "pnpm exec vitest",
            // these settings are applied to all workspaces
      }
}

This works pretty well with my turborepo project, as long as I don't configure my root folder as a separate folder of the code-workspace file. If I do that, every single test (leaf) successfully runs, but the root/parent test (with the name "Test run at ,

renet avatar Nov 07 '23 15:11 renet

I'm in a repository / VS code workspace with two projects, each of which is in a top-level folder:

/
|- .vscode/
|  |- settings.json
|- /node_modules
|- /project1
|  |- .vscode/
|  |  |- settings.json
|  |- src/
|  |  |- __tests__/
|  |      |- …
|  |- tsconfig.json
|  |- vitest.config.json
|
|- /project2
|  |- .vscode/
|  |  |- settings.json
|  |- src/
|  |  |- __tests__/
|  |      |- …
|  |- tsconfig.json
|  |- vitest.config.json
|
|- package.json
|- project-name.code-workspace

I can easily run tests from inside the root folder using cd project1 && npx vitest --config vitest.config.ts --watch src/__tests__ or npx vitest --config project1/vitest.config.ts. However, I haven't figured out a single way to get vitest to work from within VS Code, no matter what proposed workaround I try and no matter whether or how I specify vitest.commandLine in project-name.code-workspace, .vscode/settings.json, or ./project(1|2)/.vscode/settings.json (compare VSCode docs).

The result is always:

Test result not found. 
If you set `vitest.commandLine` please check: 
    Did you set `vitest.commandLine` to `run` mode? (This extension requires `watch` mode to get the results from Vitest api)
    Does it have the ability to append extra arguments? (For example it should be `yarn test --` rather than `yarn test`)
Are there tests with the same name?
Can you run vitest successfully on this file? Does it need custom option to run?

I'm having the feeling the extension might not even pick up my settings correctly.

Anyway, I just discovered vitest workspaces. While I haven't been able to get them to work (yet), at least now vitest CLI and the VSCode extension behave identically and both show the same error (something about duplicate/conflicting dependencies in my code), so I will continue to investigate this path.

codethief avatar Dec 14 '23 20:12 codethief