hbenl / vscode-mocha-test-adapter

Mocha Test Adapter for the VS Code Test Explorer
MIT License
90 stars 31 forks source link

Configure with monorepository #114

Open Ahryman40k opened 4 years ago

Ahryman40k commented 4 years ago

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/ ) But it act like my tsconfig wasn't use, meaning

Hope you may help me ! Ahry.

hbenl commented 4 years ago

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.

liam-murray-xealth commented 4 years ago

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

xenoterracide commented 2 years ago

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

xenoterracide commented 2 years ago

also

    "mochaExplorer.ignore": ["**/node_modules/**","**/dist/**"],
    "mochaExplorer.watch": ["**/*.ts","package-lock.json"],
    "mochaExplorer.require": "ts-node/register",
    "mochaExplorer.files": "src/**/*.spec.ts",
xenoterracide commented 2 years ago

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
ts-node@10.7.0 dev

test explorer

v2.13.5
xenoterracide commented 2 years ago
e1-root@ /Users/nqy642/IdeaProjects/exceptionsone
└─┬ e1-validation@0.0.0 -> ./packages/validation
  ├── @types/chai@4.3.0
  ├── @types/mocha@9.1.0
  ├── @types/node@14.18.12
  ├─┬ @types/sinon@10.0.11
  │ └── @types/sinonjs__fake-timers@8.1.1
  ├─┬ chai@4.3.6
  │ ├── assertion-error@1.1.0
  │ ├── check-error@1.0.2
  │ ├── deep-eql@3.0.1
  │ ├── get-func-name@2.0.0
  │ ├── loupe@2.3.4
  │ ├── pathval@1.1.1
  │ └── type-detect@4.0.8
  ├─┬ clean-deep@3.4.0
  │ ├── lodash.isempty@4.4.0
  │ ├── lodash.isplainobject@4.0.6
  │ └── lodash.transform@4.6.0
  ├─┬ joi@17.6.0
  │ ├── @hapi/hoek@9.2.1
  │ ├── @hapi/topo@5.1.0
  │ ├── @sideway/address@4.1.3
  │ ├── @sideway/formula@3.0.0
  │ └── @sideway/pinpoint@2.0.0
  ├─┬ mocha-junit-reporter@2.0.2
  │ ├── debug@2.6.9
  │ ├── md5@2.3.0
  │ ├── mkdirp@0.5.6
  │ ├── mocha@10.0.0 deduped
  │ ├── strip-ansi@6.0.1
  │ └── xml@1.0.1
  ├── mocha-param@2.0.1
  ├─┬ mocha@10.0.0
  │ ├── @ungap/promise-all-settled@1.1.2
  │ ├── ansi-colors@4.1.1
  │ ├── browser-stdout@1.3.1
  │ ├── chokidar@3.5.3
  │ ├── debug@4.3.4
  │ ├── diff@5.0.0
  │ ├── escape-string-regexp@4.0.0
  │ ├── find-up@5.0.0
  │ ├── glob@7.2.0
  │ ├── he@1.2.0
  │ ├── js-yaml@4.1.0
  │ ├── log-symbols@4.1.0
  │ ├── minimatch@5.0.1
  │ ├── ms@2.1.3
  │ ├── nanoid@3.3.3
  │ ├── serialize-javascript@6.0.0
  │ ├── strip-json-comments@3.1.1
  │ ├── supports-color@8.1.1
  │ ├── workerpool@6.2.1
  │ ├── yargs-parser@20.2.4
  │ ├── yargs-unparser@2.0.0
  │ └── yargs@16.2.0
  ├─┬ nyc@15.1.0
  │ ├── @istanbuljs/load-nyc-config@1.1.0
  │ ├── @istanbuljs/schema@0.1.3
  │ ├── caching-transform@4.0.0
  │ ├── convert-source-map@1.8.0
  │ ├── decamelize@1.2.0
  │ ├── find-cache-dir@3.3.1
  │ ├── find-up@4.1.0
  │ ├── foreground-child@2.0.0
  │ ├── get-package-type@0.1.0
  │ ├── glob@7.1.7
  │ ├── istanbul-lib-coverage@3.0.0
  │ ├── istanbul-lib-hook@3.0.0
  │ ├── istanbul-lib-instrument@4.0.3
  │ ├── istanbul-lib-processinfo@2.0.2
  │ ├── istanbul-lib-report@3.0.0
  │ ├── istanbul-lib-source-maps@4.0.0
  │ ├── istanbul-reports@3.0.2
  │ ├── make-dir@3.1.0
  │ ├── node-preload@0.2.1
  │ ├── p-map@3.0.0
  │ ├── process-on-spawn@1.0.0
  │ ├── resolve-from@5.0.0
  │ ├── rimraf@3.0.2 deduped
  │ ├── signal-exit@3.0.4
  │ ├── spawn-wrap@2.0.0
  │ ├── test-exclude@6.0.0
  │ └── yargs@15.4.1
  ├─┬ rimraf@3.0.2
  │ └── glob@7.1.7 deduped
  ├─┬ ts-essentials@9.1.2
  │ └── typescript@4.3.5 deduped
  ├─┬ ts-node@10.7.0
  │ ├── @cspotcode/source-map-support@0.7.0
  │ ├── UNMET OPTIONAL DEPENDENCY @swc/core@>=1.2.50
  │ ├── UNMET OPTIONAL DEPENDENCY @swc/wasm@>=1.2.50
  │ ├── @tsconfig/node10@1.0.8
  │ ├── @tsconfig/node12@1.0.9
  │ ├── @tsconfig/node14@1.0.1
  │ ├── @tsconfig/node16@1.0.2
  │ ├── @types/node@16.9.4
  │ ├── acorn-walk@8.2.0
  │ ├── acorn@8.5.0
  │ ├── arg@4.1.3
  │ ├── create-require@1.1.1
  │ ├── diff@4.0.2
  │ ├── make-error@1.3.6
  │ ├── typescript@4.3.5 deduped
  │ ├── v8-compile-cache-lib@3.0.0
  │ └── yn@3.1.1
  └── typescript@4.3.5
hbenl commented 2 years ago

@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?

xenoterracide commented 2 years ago

yes, I set up the multi root workspace. I don't think that should be an issue.

hbenl commented 2 years ago

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?

xenoterracide commented 2 years ago

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.

xenoterracide commented 2 years ago

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.

xenoterracide commented 2 years ago

also might help to understand module resolution https://nodejs.org/api/modules.html#loading-from-node_modules-folders

xenoterracide commented 2 years ago

btw, sorry about the comment spam, work proxy doesn't allow us to edit comments

xenoterracide commented 2 years ago

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...

hbenl commented 2 years ago

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.

pokey commented 9 months ago

Just wanted to check in. We use mocha in our pnpm monorepo and would love to have the test explorer work there

tjprescott commented 9 months ago

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!)