maty21 / mocha-sidebar

Mocha side bar viewer that allows you to run Mocha tests from side bar menu and view results can run each level hierarchy from all tests to a single test(and each describer of course)
MIT License
191 stars 61 forks source link

cannot run es6 modules based tests #266

Open BePo65 opened 3 years ago

BePo65 commented 3 years ago

Description

As my project is based on ES6 modules, my tests start with import path from 'path'; instead of const path = require('path'); and my package.json contains a property "type": "module".

When I start vscode with activated extension 'mocha-sidebar' I get the following errors showing that the extension can only handle commonjs modules:

trying to searching for tests using these settings: 

    mocha path: d:\Projekte\github\license-downloader\node_modules\mocha\index.js
    test files location: test/**/*.spec.js
    files to ignore: **/.git/**/*,**/node_modules/**/*
    environmets: {}
    requires: []
    options:  {}

if you find anything wrong please change those default settings
____________________________________________________________________________
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: d:\Projekte\github\license-downloader\test\license-report-file.spec.js
require() of ES modules is not supported.
require() of d:\Projekte\github\license-downloader\test\license-report-file.spec.js from d:\Projekte\github\license-downloader\node_modules\mocha\lib\mocha.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.

Instead rename license-report-file.spec.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from d:\Projekte\github\license-downloader\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)

    at d:\Projekte\github\license-downloader\node_modules\mocha\lib\mocha.js:436:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (d:\Projekte\github\license-downloader\node_modules\mocha\lib\mocha.js:433:14)

    at c:\Users\Entwicklung\.vscode\extensions\maty.vscode-mocha-sidebar-0.22.2\lib\worker\findtests.js:86:15
    at f (c:\Users\Entwicklung\.vscode\extensions\maty.vscode-mocha-sidebar-0.22.2\node_modules\once\once.js:25:25)
    at Glob.<anonymous> (c:\Users\Entwicklung\.vscode\extensions\maty.vscode-mocha-sidebar-0.22.2\node_modules\glob\glob.js:151:7)
    at Glob.emit (events.js:315:20)
    at Glob._finish (c:\Users\Entwicklung\.vscode\extensions\maty.vscode-mocha-sidebar-0.22.2\node_modules\glob\glob.js:197:8)
    at done (c:\Users\Entwicklung\.vscode\extensions\maty.vscode-mocha-sidebar-0.22.2\node_modules\glob\glob.js:182:14)
    at Glob._processGlobStar2 (c:\Users\Entwicklung\.vscode\extensions\maty.vscode-mocha-sidebar-0.22.2\node_modules\glob\glob.js:637:12)
    at c:\Users\Entwicklung\.vscode\extensions\maty.vscode-mocha-sidebar-0.22.2\node_modules\glob\glob.js:626:10
cant get tests [object Object] 

Steps to Reproduce

see text above

Expected Results

no errors when using es6 module based tests

Actual Results

see above

Settings file

no special settings

Versions

OS version

Example for reproducing

I can deliver one if needed

Result of my problem analysis

The problem comes from the fact that this extension uses mocha v5.2.0. If using e.g. mocha v8.3.2, it could call mocha.loadFilesAsync()in findtests.js line 86 instead of the old mocha.loadFiles() , as the new 'async' version can handle commonjs and es6 modules

spigelli commented 2 years ago

Did you find a workaround?

BePo65 commented 2 years ago

Unluckily no. For me it looks like this great project is dead - no commits since 2019.

jonkoops commented 2 years ago

I can confirm this is an issue as well.