hbenl / vscode-mocha-test-adapter

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

No Tests #210

Closed machineghost closed 2 years ago

machineghost commented 2 years ago

I added Mocha and a single test file to an existing project. I then added a very basic .mocharc.js:

module.exports = {
  exit: true,
  require: ['jsdom-global/register', '@babel/register'],
  spec: './{,!(node_modules)/**}/*.spec.js',
};

No tests, no error message no ... anything. How can I get tests to run, or at least get the tool to tell me why it isn't showing any tests?

P.S. When I simply run npx mocha the tests run fine. Only the extension has issues. P.P.S. In my Terminal Output for "Test Explorer" and "Mocha Tests" I see ... absolutely nothing. There's no related output in the general logs either.

If I could at least see an error message, I might be able to debug/solve this issue myself, but currently things seem like a giant black void. If nothing else, the Readme could certainly use a few lines describing how to debug this tool.

machineghost commented 2 years ago

One more detail. My relevant VS Code settings are:

  "mochaExplorer.files": "./{,!(node_modules)/**}/*.spec.js",
  "mochaExplorer.ignore": "node_modules",
  "mochaExplorer.require": ["jsdom-global/register", "@babel/register"],
  "testExplorer.addToEditorContextMenu": true,
  "testExplorer.hideEmptyLog": false,
  "mochaExplorer.esmLoader": true,
  "testExplorer.errorDecoration": false,

However, I get the error whether I have those settings or delete them (either way no tests).

hbenl commented 2 years ago

the Readme could certainly use a few lines describing how to debug this tool.

Try diagnostic logging: https://github.com/hbenl/vscode-mocha-test-adapter#troubleshooting

machineghost commented 2 years ago

I somehow missed that, apologies! I'll try using that to debug.

machineghost commented 2 years ago

Now I see the following in my Mocha Explorer Log:

[2022-05-02 00:56:11.918] [INFO] Loading test files of /home/me/Projects/Project
[2022-05-02 00:56:11.927] [INFO] Adapter disabled for this folder, loading cancelled

Unfortunately that doesn't explain why I have no tests.

pcan commented 2 years ago

I got the same issue. It seems "mochaExplorer.globImplementation": "vscode" fixes the issue, because the default glob implementation does not support backslashes on Windows. In particular, I get an empty match array in this line with the default glob implementation, while switching to vscode seems to work for Windows.

I think it's worth either adding a debug log for the matched files, or improving the README by explaining the implications of using glob vs. vscode, or both.

atava commented 2 years ago

It's always worked for me. Started a new project just now and tests were not being detected.

The suggestion above by pcan seemed to solve it.

machineghost commented 2 years ago

I tried changing globImplementation, but it didn't help: I still get:

[2022-05-02 15:26:37.782] [INFO] Adapter disabled for this folder, loading cancelled

P.S. The description of that setting seems wrong. It says:

The glob implementation to use. "glob" (the default) is more compatible, "vscode" (the old default) may be faster.

But from the reports here it seems that vscode is the more compatible option.

P.P.S. I also a few of these in the logs:

[2022-05-02 16:07:29.872] [INFO] Reloading cancelled because the adapter or autoloading is disabled

danielbayley80 commented 2 years ago

I got the same issue. It seems "mochaExplorer.globImplementation": "vscode" fixes the issue,

I lost hours of my life to this. 😤 this should be mentioned somewhere in the main documentation.

driescroons commented 2 years ago

I got the same issue. It seems "mochaExplorer.globImplementation": "vscode" fixes the issue

Was this a change/update that caused the flag to be needed to be set? I'm using windows and have been using the extension for quite some time, and it has always worked, until I opened my editor with the test explorer today.

pcan commented 2 years ago

I guess something changed either in the plugin or in vscode in the last few days (I don't have time now to go through the history to confirm). I had no issue the past week with the default options neither.

hbenl commented 2 years ago

@machineghost Try running the command "Mocha Test Explorer: Enable for a workspace folder" from the VS Code command palette. @pcan @atava @danielbayley80 @driescroons I had released a new version yesterday with upgraded dependencies. As @pcan pointed out, this included a change (which I was not aware of) that removed support for backslashes on Windows. I have now downgraded them again and released a new version of this extension.

machineghost commented 2 years ago

When I used "Enable for a workspace folder" I finally started seeing some errors ... but they don't make sense.

I'm now seeing (in "Test Explorer" logging):

Error: Cannot find module 'jsdom-global/register'

The stack trace is:

  • /home/me/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.3/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 (/home/me/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.3/node_modules/mocha/lib/nodejs/esm-utils.js:60:20) {

The reason it doesn't make sense though is that I have that package installed:

"jsdom-global": "^3.0.2",

More importantly, if I didn't have it installed, my tests wouldn't run ... but they do, just fine (outside of the extension, at the command line).

I'm baffled as to why the extension can't find this package.

P.S. The reason it's looking for the package in the first place is that I require it in .mocharc.js:

require: ['jsdom-global/register', '@babel/register', 'next/babel'],
hbenl commented 2 years ago

OK, that's progress. From the stack trace I can see that the extension uses its bundled version of mocha. Usually it would use the version installed in your project, the bundled version is just a fallback. And I realized now that the require option may not work with the bundled version of mocha because it will look for the required packages in the extension's dependencies instead of your project's dependencies. Do you have mocha in your project's node_modules? If not, try to install it there and see if that makes the extension work. If it does, I'll have to add a warning that should pop up if you're using the require option and it doesn't find mocha in your project.

machineghost commented 2 years ago

Do you have mocha in your project's node_modules?

Yup:

$ ls node_modules/mocha/
assets  bin  browser-entry.js  index.js  lib  LICENSE  mocha.css  mocha-es2018.js  mocha.js  mocha.js.map  node_modules  package.json  README.md
machineghost commented 2 years ago

I should note that this is a "monorepo", so the root folder I open in VS Code is not the same as the folder of the project (that folder is a sub-folder of the root folder).

In other words, I have the directory structure:

A
     B

where "A" is the VS Code root and "B" is the project folder. Mocha is inside the project's node_modules, ie. in A/B/node_modules/mocha.

If the extension is only checking the root folder, that could explain why it isn't finding Mocha.

hbenl commented 2 years ago

If the extension is only checking the root folder, that could explain why it isn't finding Mocha.

Yes, that is exactly the issue. For monorepos I would strongly recommend to use a multi-root workspace with each project added as a workspace folder. This has several advantages, most importantly you can have different VS Code settings for each project (although you can still have settings that are applied to the entire workspace). So if you had tests in 2 projects in the same monorepo and they require different configurations, that would only be possible in a multi-root workspace. And there are probably many extensions that don't recognize projects in subfolders.

machineghost commented 2 years ago

This is really under-documented. When I look at the readme, it says:

Getting started

- Install the extension and restart VS Code
- Put your Mocha command line options (if you have any) in a [mocha configuration file](https://mochajs.org/#configuring-mocha-nodejs) (either a .mocharc.* file or a mocha property in your package.json or a [mocha.opts](https://mochajs.org/#mochaopts) file) or VS Code's settings (see below)
- Open the Test view by clicking on the flask icon in the [Activity Bar](https://code.visualstudio.com/docs/getstarted/userinterface#_activity-bar)
- Run / Debug your tests using the Run / Debug icons in the Test Explorer or the CodeLenses in your test file

It seems to me there should be another bullet point there saying that you must have your project's root added as a folder to VS Code (if you are using a monorepo).

Meanwhile, adding the project as a VS Code root helped ... sort of. Now VS Code thinks I have three Jest test suites in my three sub-projects (there's no actual tests, but it seems to think some random non-test file is a test). However, I still don't see my Mocha tests.

In the logs I now see:

[2022-05-04 19:09:32.766] [INFO] Creating adapter for /home/me/projects/project/server-2021
[2022-05-04 19:09:32.767] [INFO] Registering adapter for /home/me/projects/project/server-2021
[2022-05-04 19:09:32.767] [INFO] Loading test files of /home/me/projects/project/server-2021
[2022-05-04 19:09:32.777] [INFO] Initialization finished
[2022-05-04 19:09:32.778] [DEBUG] Using working directory: /home/me/projects/project/next-2022
[2022-05-04 19:09:32.855] [DEBUG] Using nodePath: /usr/bin/node
[2022-05-04 19:09:33.557] [INFO] Adapter disabled for this folder, loading cancelled
[2022-05-04 19:09:33.584] [INFO] Adapter disabled for this folder, loading cancelled
[2022-05-04 19:10:03.884] [DEBUG] Using Mocha options: {"ui":"bdd","timeout":2000,"retries":0,"requires":["jsdom-global/register","@babel/register"],"delay":false,"fullTrace":false,"exit":true,"asyncOnly":false,"parallel":false}
[2022-05-04 19:10:03.884] [DEBUG] Looking for test files ["./{,!(node_modules)/**}/*.spec.js","!(node_modules)/**/*.spec.{js,cjs,mjs}"] in /home/me/projects/project/next-2022
[2022-05-04 19:10:04.002] [DEBUG] Found test files []
[2022-05-04 19:10:04.019] [DEBUG] Using environment variables from config: {}
[2022-05-04 19:10:04.021] [DEBUG] Spawning /home/me/.vscode/extensions/hbenl.vscode-mocha-test-adapter-2.13.5/out/worker/bundle.js with IPC options {}
[2022-05-04 19:10:04.368] [INFO] Worker: Using the mocha package at /home/me/projects/project/next-2022/node_modules/mocha
[2022-05-04 19:10:04.419] [INFO] Worker: Patching Mocha
[2022-05-04 19:10:04.419] [INFO] Worker: Trying requireOrImport('jsdom-global/register')
[2022-05-04 19:10:04.846] [INFO] Worker: Trying requireOrImport('@babel/register')
[2022-05-04 19:10:04.954] [INFO] Worker: Loading files
[2022-05-04 19:10:04.954] [INFO] Worker: Trying to use Mocha's experimental ESM module loader
[2022-05-04 19:10:04.956] [INFO] Worker (stdout): 

[2022-05-04 19:10:04.956] [INFO] Worker (stdout): 

[2022-05-04 19:10:04.957] [INFO] Worker (stdout):   0 passing (1ms)

[2022-05-04 19:10:04.957] [INFO] Worker (stdout): 

[2022-05-04 19:10:04.957] [INFO] Worker: Converting tests and suites
[2022-05-04 19:10:04.957] [INFO] Worker found no tests
[2022-05-04 19:10:04.967] [INFO] Worker finished with code null and signal SIGTERM

But again, I do have tests, and when I simply npx mocha at the command line they run just fine.

EDIT: P.S. I removed the Jest extension, and now I no longer see the bogus Jest tests ... I once again see nothing in the testing panel.

hbenl commented 2 years ago

Did you set mochaExplorer.globImplementation to vscode? If so, remove that setting, the vscode implementation won't work with your globs. If not, could you give me the path of one of your test files so I can check why the extension isn't finding it?

machineghost commented 2 years ago

That was it: the extension finally shows my tests!

Thanks so much for all the help, and hopefully my PR will help others avoid this same problem.