thunderclient / thunder-client-support

Thunder Client is a lightweight Rest API Client Extension for VS Code.
https://www.thunderclient.com
Other
3.61k stars 126 forks source link

How to use require instead of tc.loadModule for a non built-in module? #1485

Closed Sureshp11 closed 6 months ago

Sureshp11 commented 6 months ago

Question: How to use require for a non built-in module? I am using 'Lodash' module in my script and loadModule is not loading when I run the script first time. It works only for the second time. Is it possible to use require instead of LoadModule for Lodash?

Are you using the free version/paid version/trial: trial

rangav commented 6 months ago

Can you share the script you used for loadModule?

may be await keyword is missing

Sureshp11 commented 6 months ago

I have used await in the code below. However it's not working.

lodashTestDemoScript.js:

//Test class contains the actual tc test class Test { constructor(obj1,obj2) { this.obj1 = obj1; this.obj2 = obj2; } async action() {

const {_} = await tc.loadModule('lodash');

_.mergeWith(this.obj1,this.obj2,
    function(objectValue, sourceValue) {
      tc.test('mergeWith Test ',
            function() {                  
              expect(objectValue).to.equal(sourceValue);
            });
    });

} }

//Run function executes the test available in the tasks async function run(tasks){
let taskNo = 1; try {
for (const task of tasks) {
await new Promise((resolve, reject) => {
task((err) => {
if (err) { reject(err); } else {
resolve(); }
taskNo++; }); }); }
} catch (err) { console.log('Promise error');
} }

//LodashTests function adds the test to the tasks and calls the run function function LodashTests() {

performLodashTest = async (obj1,obj2) => {

const tasks = [];

tasks.push(async () => {
  await new Test(obj1,obj2)
      .action();

});

run(tasks);

};

return { performLodashTest: performLodashTest }; }

module.exports = { LodashTests,
};

The inline script that calls the function:

require("C:\Temp\lodashTestDemoScript.js");

const obj1 = { 'amit': 20, };

const obj2 = {
  'amit': 30,
};    

await new LodashTests().performLodashTest(obj1,obj2);

rangav commented 6 months ago

I tested the lodash module its working for me, I did not understand your code, what you are trying to do.

Can you test with small code first?

Screenshot 2024-02-29 at 07 17 24
Sureshp11 commented 6 months ago

I agree that it works normally. However when I use it with promise then the issue occurs. I have added a sample below:

async function performLodashTest(){

const fn = async () => { const {} = await tc.loadModule('lodash'); tc.test('Lodash test', function(){.now()>0}); };

new Promise(function(resolve, reject){ fn(() => {
resolve(); }); }); }

await performLodashTest();

rangav commented 6 months ago

why do you need promises our extension supports async and await.

rangav commented 6 months ago

Remove promises it will work directly and the syntax for your code to load lodash is also wrong I think.

Screenshot 2024-02-29 at 11 30 01
const performLodashTest = async () => {
    const _ = await tc.loadModule('lodash');

        tc.test('Lodash test', function(){
          console.log(_.now());

          _.now()>0

        });
};

await performLodashTest();
rangav commented 6 months ago

you can simply the above code further as below

const _ = await tc.loadModule('lodash');

tc.test('Lodash test', function(){
   console.log(_.now());
   _.now()>0;
 })
Sureshp11 commented 6 months ago

We have been working on the existing code base to migrate to Thunder Client. Our existing code uses Lodash and promises in it. I don't want to change the logic.

rangav commented 6 months ago

ok here is your promises code updated, which is working, see comments in code

async function performLodashTest(){

const fn = async () => {
  const _ = await tc.loadModule('lodash'); // --------> use _
      tc.test('Lodash test', function(){
        _.now()>0;  // ---------> use _

      });
  };

  new Promise(function(resolve, reject){
    return fn(() => {    // -----> return keyword missing
        resolve();
    });
  });
}

await performLodashTest();
Sureshp11 commented 6 months ago

I still can't see it working. I ran exactly the same code and it didn't create the test. I ran it again then it did. image

Sureshp11 commented 6 months ago

I have used two iterations below to show it more clearer: image

rangav commented 6 months ago

modified further, this is working

async function performLodashTest(){

    const fn = async () => {
      const _ = await tc.loadModule('lodash'); // --------> use _
          tc.test('Lodash test', function(){
            _.now()>0;  // ---------> use _

          });
      };

     return new Promise(function(resolve, reject){  // ------> added return
       fn().then(()=>{  // --------> modified
           resolve();
        })
      });
}

await performLodashTest();
Sureshp11 commented 6 months ago

That works. Thanks.