vscode-mocha-test-adapter
vscode-mocha-test-adapter copied to clipboard
Configure with monorepository
Hi,
I have a lerna project with several tested typescript libraries. But I'm unable to make tests discovered.
In the default settings, no tests are found. So I tried to set some parameters:
"mochaExplorer.files": "**/*.spec.ts", // or "packages/**/*.spec.ts" or "packages/<package>/src**/*.spec.ts"
"mochaExplorer.logpanel": true,
"mochaExplorer.pruneFiles": true,
"mochaExplorer.require": [
"ts-node/register",
"source-map-support/register"
]
I also tried to set "mochaExplorer.cwd" to a specific package root. ( packages/
- it can't find tests dependencies when they come from other libraries inside my lerna project.
- it failed on chai/mocha/sinon imports with esModuleInterop error ...
Hope you may help me ! Ahry.
Have you tried creating a multi-root workspace for your project? That usually works a lot better because VS Code will then understand that each package has its own settings (I mean .vscode/settings.json
as well as tsconfig.json
etc.).
Also, you probably should remove source-map-support/register
from your requires because that is only needed if you run the tests from transpiled *.spec.js
files instead of the original *.spec.ts
files. But I don't think this is the source of your problems.
I recommend the following extension to make it easy to switch among packages in your mono repo, as well as open multiple under a single workspace. It makes mono repos work well with the test explorer extension.
https://marketplace.visualstudio.com/items?itemName=folke.vscode-monorepo-workspace
I have the same problem
[2022-05-19 15:05:37.013] [INFO] Worker: Trying requireOrImport('ts-node/register')
[2022-05-19 15:05:37.023] [INFO] Worker: Caught error Error: Cannot find module 'ts-node/register'
Require stack:
- /Users/nqy642/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/node_modules/mocha/lib/nodejs/esm-utils.js
- /Users/nqy642/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/node_modules/mocha/lib/mocha.js
- /Users/nqy642/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/node_modules/mocha/index.js
- /Users/nqy642/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/out/worker/bundle.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at exports.requireOrImport (/Users/nqy642/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/node_modules/mocha/lib/nodejs/esm-utils.js:49:16) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/nqy642/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/node_modules/mocha/lib/nodejs/esm-utils.js',
'/Users/nqy642/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/node_modules/mocha/lib/mocha.js',
'/Users/nqy642/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/node_modules/mocha/index.js',
'/Users/nqy642/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/out/worker/bundle.js'
]
}
[2022-05-19 15:05:37.023] [INFO] Received error from worker
[2022-05-19 15:05:37.024] [ERROR] Worker (stderr): node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
I've tried the above suggestions. I'm using npm's built in workspaces with npm 8. I've tried a large variety of configurations. I did get it working once, then I switched branch's and it's not working again. I don't get what makes this work. ts-node is installed in every package.json
also
"mochaExplorer.ignore": ["**/node_modules/**","**/dist/**"],
"mochaExplorer.watch": ["**/*.ts","package-lock.json"],
"mochaExplorer.require": "ts-node/register",
"mochaExplorer.files": "src/**/*.spec.ts",
sorry about the comment spam, work proxy blocks edit
Darwin 5c52309d33e3 20.6.0 Darwin Kernel Version 20.6.0: Tue Feb 22 21:10:41 PST 2022; root:xnu-7195.141.26~1/RELEASE_X86_64 x86_64 i386 MacBookPro16,1 Darwin
1.67.2
c3511e6c69bb39013c4a4b7b9566ec1ca73fc4d5
x64
[email protected] dev
test explorer
v2.13.5
e1-root@ /Users/nqy642/IdeaProjects/exceptionsone
└─┬ [email protected] -> ./packages/validation
├── @types/[email protected]
├── @types/[email protected]
├── @types/[email protected]
├─┬ @types/[email protected]
│ └── @types/[email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── @hapi/[email protected]
│ ├── @hapi/[email protected]
│ ├── @sideway/[email protected]
│ ├── @sideway/[email protected]
│ └── @sideway/[email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected] deduped
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── @ungap/[email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── @istanbuljs/[email protected]
│ ├── @istanbuljs/[email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected] deduped
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └── [email protected] deduped
├─┬ [email protected]
│ └── [email protected] deduped
├─┬ [email protected]
│ ├── @cspotcode/[email protected]
│ ├── UNMET OPTIONAL DEPENDENCY @swc/core@>=1.2.50
│ ├── UNMET OPTIONAL DEPENDENCY @swc/wasm@>=1.2.50
│ ├── @tsconfig/[email protected]
│ ├── @tsconfig/[email protected]
│ ├── @tsconfig/[email protected]
│ ├── @tsconfig/[email protected]
│ ├── @types/[email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected] deduped
│ ├── [email protected]
│ └── [email protected]
└── [email protected]
@xenoterracide I can see that the extension uses its bundled version of mocha instead of the version installed in your project, and this causes issues with mochaExplorer.require
(which I want to fix, but I don't know yet how). Do you use a monorepo? If so, do you use a multi-root workspace as suggested above?
yes, I set up the multi root workspace. I don't think that should be an issue.
So do you have the folder node_modules/mocha
in your package's folder? If so, the extension should use that version of mocha, but it doesn't so I guess mocha is installed somewhere else?
no, because that's not how node_module resolution works. If you install the same version of mocha into all of your workspaces with npm workspaces each workspace will use the parent node_modules version. It does not create a different version for each module.
it only creates a version in your workspace if it needs to. Each workspace does not have it's own package-lock.json. this doesn't document that, but it's the workspace tool we're currently using. https://docs.npmjs.com/cli/v8/using-npm/workspaces yarn, IIRC wouldn't do that either, especially not if you're using pnp, which doesn't? drop them in node_modules at all.
also might help to understand module resolution https://nodejs.org/api/modules.html#loading-from-node_modules-folders
btw, sorry about the comment spam, work proxy doesn't allow us to edit comments
also, I think I may actually have figured out how to fix this a long time ago, but don't ask me how. I'm sure I had to do something beyond the monorepo plugin though...
Thanks, I hadn't really looked into npm/yarn workspaces before but now I understand why this extension keeps having problems with monorepos. I'll have a fix in the next couple of days.
Just wanted to check in. We use mocha in our pnpm monorepo and would love to have the test explorer work there
I have the opposite problem. The mocha test explorer works great... but I can't get any of the tests to run with just pnpm mocha
. I guess it's less crucial, but it's frustrating since I feel like both should work (they worked when there was just one package!)