Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chai require from .http not supported #268

Open
zawiasam opened this issue Mar 12, 2024 · 6 comments
Open

chai require from .http not supported #268

zawiasam opened this issue Mar 12, 2024 · 6 comments

Comments

@zawiasam
Copy link

zawiasam commented Mar 12, 2024

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"
  }
}
@zawiasam zawiasam changed the title chai re chai require from .http not supported Mar 12, 2024
@AnWeber
Copy link
Owner

AnWeber commented Mar 15, 2024

@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 [email protected]

@zawiasam
Copy link
Author

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

@AnWeber
Copy link
Owner

AnWeber commented Mar 21, 2024

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

@alekdavisintel
Copy link

@zawiasam Did you figure out how to do this?

@AnWeber What would you recommend to use for testing complex assertions (e.g. check if an object list returned with response contains elements with specific properties, such as expect(response.parsedBody).to.deep.include.members([{ id: 123 }, {id: 456']))? Is there an alternative to chai? If not, what are the steps to use a downgraded version of chai? It would be really helpful if a richer assertion library (chai 4.x or whatever) were bundled with httpYac (like these: https://httpyac.github.io/guide/scripting.html#require), because assertions are the essential part of what the API testing and if it requires manual steps, it would complicate usage and increase support calls.

@alekdavis
Copy link

@AnWeber I manually installed chai 4.4.1:

image

Restarted VSCode, but I'm still getting an error that the module is missing:

image

Did I miss a step?

@AnWeber
Copy link
Owner

AnWeber commented Aug 30, 2024

@alekdavisintel You are now right that Chai should be bundled. My idea of the tool initially came from a different direction. Just a small Postman replacement. The ideas I got from issues, have changed it. Using chai@v4 is supported. Only v5 is broken because of esm. NodeJS v22 brings better support to also require esm modules. Maybe this Problem is solved. I will retry a solution next week. But I try to survive the heat in italy at the moment

Did i muss a step?

Probably Not. That is the way I used it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants