usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
27.33k stars 1.25k forks source link

Cannot use Cheerio as an external library to test HTML. #2896

Open brutaldev opened 2 months ago

brutaldev commented 2 months ago

I have checked the following:

Describe the bug

I'm trying to use Cheerio to test HTML from Bruno but cannot get it to work because of the following error:

Error invoking remote method 'send-http-request': TypeError: 'set' on proxy: trap returned falsish for property 'Symbol(nodejs.stream.destroyed)'

I installed it with NPM as indicated in the documentation then went through a number of tests to include all the modules that need to be whitelisted.

package.json

{
  "name": "bruno-cheerio-test",
  "version": "1.0.0",
  "dependencies": {
    "cheerio": "^1.0.0"
  }
}

bruno.json

{
  "version": "1",
  "name": "CheerioTest",
  "type": "collection",
  "ignore": [
    "node_modules",
    ".git"
  ],
  "scripts": {
    "moduleWhitelist": [
      "console",
      "async_hooks",
      "diagnostics_channel",
      "events",
      "querystring",
      "net",
      "assert",
      "buffer",
      "cheerio"
    ]
  }
}

Test Script (Bruno)

const cheerio = require("cheerio");
const $ = cheerio.load(res.getBody());

test("Name Check", function () {
  expect($(".section h2").text()).to.include("DevEnterprise Software Limited");
});

Test Script (Postman)

const cheerio = require("cheerio");
const $ = cheerio.load(pm.response.text());

pm.test("Name Check", function () {
  pm.expect($(".section h2").text()).to.include("DevEnterprise Software Limited");
});

Cheerio is included in Postman so perhaps Bruno can consider having it as an inbuilt library as well since there is nothing else to parse HTML? If not, please consider providing some documentation on how to test HTML with Bruno or install Cheerio successfully.

This is also release to https://github.com/usebruno/bruno/discussions/2163

.bru file to reproduce the bug

meta {
  name: CheerioTest
  type: http
  seq: 1
}

get {
  url: https://www.deventerprise.com/
  body: none
  auth: none
}

tests {
  const cheerio = require("cheerio");
  const $ = cheerio.load(res.getBody());

  test("Name Check", function () {
    expect($(".section h2").text()).to.include("DevEnterprise Software Limited");
  });
}

Screenshots/Live demo link

Fails in Bruno

image

Works in Postman

image

optimuspaul commented 1 day ago

Was just trying Cheerio as well, I get a slightly different error

Error invoking remote method 'send-http-request': VMError: Cannot find module 'buffer'

which is making me think this is a sandboxing issue.