hbenl / vscode-mocha-test-adapter

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

describe is not a function #158

Closed ngilmore14 closed 3 years ago

ngilmore14 commented 3 years ago

Updated this extension in VS Code last week and now seeing this error:

Worker: Caught error TypeError: describe is not a function

Stack trace: at Object. (owner.js:49:1) at Module._compile (internal/modules/cjs/loader.js:1133:30) at Module._compile (node_modules\pirates\lib\index.js:99:24) at Module._extensions..js (internal/modules/cjs/loader.js:1153:10) at Object.newLoader [as .js] (node_modules\pirates\lib\index.js:104:7) at Module.load (internal/modules/cjs/loader.js:977:32) at Function.Module._load (internal/modules/cjs/loader.js:877:14) at Module.require (internal/modules/cjs/loader.js:1019:19) at require (internal/modules/cjs/helpers.js:77:18) at Object.exports.requireOrImport (node_modules\mocha\lib\esm-utils.js:20:12) at Object.exports.loadFilesAsync (node_modules\mocha\lib\esm-utils.js:33:34)

We import mocha functions like this: const { describe, before, beforeEach, after, afterEach, it } = require('mocha');

hbenl commented 3 years ago

We import mocha functions like this: const { describe, before, beforeEach, after, afterEach, it } = require('mocha');

That may be the reason: it's not necessary to import these functions (they are provided by Mocha as globals when you run your tests) and I've seen these imports sometimes create problems. I would also be interested to know if the behavior changes if you go back to version 2.11.0 of this extension (VSCode allows you to install a different version from the extension's menu in the extensions panel).

ngilmore14 commented 3 years ago

With version 2.11.0 everything works fine. It is only an issue with v2.11.1 as far as I have seen.

hbenl commented 3 years ago

Interesting. Could you create a little sample repo with which I can reproduce this?

ngilmore14 commented 3 years ago

I can confirm that importing the function from Mocha is causing the problem (e.g. removing the import doesn't not throw the error). Not sure what would have changed though to make that error now. I have the following versions installed (running on Windows)

Try this (reproduces for me): const { describe } = require( 'mocha'); describe('Test suite', () => { });

TheUnlocked commented 3 years ago

I'm encountering what I assume is the same issue but with a different stack trace:

TypeError: Cannot read property 'describe' of undefined
    at Function.exports.describe (D:\Users\Unlocked\Documents\GitHub\exclaim\node_modules\mocha\lib\mocha.js:119:26)
    at Object.<anonymous> (D:\Users\Unlocked\Documents\GitHub\exclaim\test\parser.spec.ts:10:1)
    at Module._compile (node:internal/modules/cjs/loader:1102:14)
    at Module.m._compile (d:\Users\Unlocked\Documents\GitHub\exclaim\node_modules\ts-node\src\index.ts:1056:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
    at Object.require.extensions.<computed> [as .ts] (d:\Users\Unlocked\Documents\GitHub\exclaim\node_modules\ts-node\src\index.ts:1059:12)
    at Module.load (node:internal/modules/cjs/loader:967:32)
    at Function.Module._load (node:internal/modules/cjs/loader:807:14)
    at Module.require (node:internal/modules/cjs/loader:991:19)
    at require (node:internal/modules/cjs/helpers:92:18)

The issue only appears on Windows for me. If I run vscode through WSL the extension works perfectly fine.

That may be the reason: it's not necessary to import these functions (they are provided by Mocha as globals when you run your tests) and I've seen these imports sometimes create problems.

Removing the imports fixed the issue for me, but like ngilmore14, it worked for me with the imports in earlier versions.

hbenl commented 3 years ago

I found that importing the describe/it/etc. functions in test files produces this error on Windows (but not on other OSes) when you run mocha directly (from a command line). Earlier versions of this extension used Unix-style paths (with / instead of \) internally and for some reason this (unintentionally) worked around this strange mocha behavior. But using Unix-style paths on Windows created some other issues, so starting with version 2.11.1, the extension started using Windows-style paths on Windows instead. My goal is to make this extension produce the exact same results as using mocha directly does - including errors, so I don't want to change this. @ngilmore14 @TheUnlocked if you find a situation where running mocha directly works but this extension reports an error, please create another issue (or reopen this one).

stupid-genius commented 3 years ago

I have a dummy file test/sub/dir/config.js which contains no tests or code. When I run my tests from command line there are no problems, but the VSC extension throws an error. The file just contains the string "test" and the extension complains that "test is not defined".

hbenl commented 3 years ago

@stupid-genius In my tests, both the extension and mocha work when using mocha's tdd ui and they both fail when using a different ui. My guess is that in your setup, the extension uses different mocha settings than when you run it directly. Otherwise, a sample repo with which I can reproduce this would be very welcome.