Closed Sureshp11 closed 6 months ago
Can you share the script you used for loadModule?
may be await
keyword is missing
I have used await in the code below. However it's not working.
//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,
};
require("C:\Temp\lodashTestDemoScript.js");
const obj1 = { 'amit': 20, };
const obj2 = {
'amit': 30,
};
await new LodashTests().performLodashTest(obj1,obj2);
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?
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();
why do you need promises our extension supports async and await.
Remove promises it will work directly and the syntax for your code to load lodash is also wrong I think.
const performLodashTest = async () => {
const _ = await tc.loadModule('lodash');
tc.test('Lodash test', function(){
console.log(_.now());
_.now()>0
});
};
await performLodashTest();
you can simply the above code further as below
const _ = await tc.loadModule('lodash');
tc.test('Lodash test', function(){
console.log(_.now());
_.now()>0;
})
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.
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();
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.
I have used two iterations below to show it more clearer:
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();
That works. Thanks.
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