AnWeber / vscode-httpyac

Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT and WebSocket requests directly within Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=anweber.vscode-httpyac
MIT License
222 stars 20 forks source link

chai require from .http not supported #268

Open zawiasam opened 3 months ago

zawiasam commented 3 months ago

Can't import chai for assertions

after sending request

Host: postman-echo.com

{{
    const assert = require("chai").assert
      , foo = 'bar'
      , beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
    test("chai", function () {
        assert.typeOf(foo, 'string'); // without optional message
    })
}}

error from vc extension

/Users/maciek/source/test/node_modules/chai/chai.js from /Users/maciek/source/test/Untitled-1.http not supported. Instead change the require of chai.js in /Users/maciek/source/test/Untitled-1.http to a dynamic import() which is available in all CommonJS modules. at Function.<anonymous> (node:electron/js2c/node_init:2:13357) at h._load (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:173:5634) at n._load (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:170:29786) at t._load (/Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:135:35289) at s (/Users/maciek/.vscode/extensions/anweber.vscode-httpyac-6.11.6/dist/extension.js:196:2430) at Object.userJS (/Users/maciek/source/test/Untitled-1.http:5:20) at CQ (/Users/maciek/.vscode/ex...

from CLI

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/maciek/source/test/node_modules/chai/chai.js from /Users/maciek/source/test/Untitled-1.http not supported.
Instead change the require of chai.js in /Users/maciek/source/test/Untitled-1.http to a dynamic import() which is available in all CommonJS modules.
    at s (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:6:2432)
    at Object.userJS (/Users/maciek/source/test/Untitled-1.http:5:20)
    at Yt (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:6:2730)
    at kt (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:6:6167)
    at Object.action (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:6:5438)
    at n.trigger (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/node_modules/hookpoint/dist/index.js:1:2866)
    at async t.execute (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:2:52568)
    at async mp (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:119:33932)
    at async Tc (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:119:34177)
    at async Eo (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:119:33684)
    at async Command._c (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:119:43602)
    at async Command.parseAsync (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/node_modules/commander/lib/command.js:936:5)
    at async Object.Qc (/Users/maciek/.nvm/versions/node/v18.16.0/lib/node_modules/httpyac/dist/index.js:119:47517) {
  code: 'ERR_REQUIRE_ESM',
  handled: true
}

package.json

{
  "dependencies": {
    "chai": "^5.1.0"
  }
}
AnWeber commented 3 months ago

@zawiasam The problem is that chai switched to ESM with v5 (see Release Notes). This is not supported by the require. You would have to use import(). However, I do not currently provide this. I first have to learn how to do it. The current workaround would be to downgrade to chai@4.x

zawiasam commented 3 months ago

@AnWeber Maybe I could help somehow, any hint where I could to start?

AnWeber commented 3 months ago

@zawiasam With pleasure. It is actually also encapsulated and can therefore be worked on without the big picture. But the problem is complicated enough. The method runScript is used to run some text Content as NodeJS Script. In this Script access to all variables is needed and exports needs to be added to the variables after script execution. Unfortunately, this method does not support ESM or the use of import. For import() setting importModuleDynamically would apparently be necessary, but this is new territory for me. Another special feature is that the import has to be "bent", or that's my idea. The code acts within httpyac package and therefore has access to httpyac dependencies. In order to have access to deps from the target package, this must be bent. Hence the const mod = createModule(filename);hack to get require calls working. In the Contributing would be a guide on how to debug it, but another approach would be to extract the code and make it run independently and then integrate the features.