I'm trying to create a function to testing asynchronous code, but I'm kind of lost, I want the TEST_F and TEST function below also work with asynchronous code, such as loading the image for example.
const CHECK = (actual, expected) => {
return (actual === expected);
};
const TEST = (name, ...testFunctions) => {
console.log(name+':');
for (let test of testFunctions)
console.log(test);
};
const TEST_F = (name, f) => {
const tStart = performance.now();
const check = f();
const tEnd = performance.now();
const duration = tEnd - tStart;
const details = name + ': ' + '(' + duration + ') = ' + check;
return details;
};
const imageDownload = (path, successCallback) => {
let img = new Image();
img.addEventListener("load", successCallback, false);
img.src = path;
return img;
};
TEST("TestImage",
TEST_F("testImageDownload", () => {
let spyCountSuccess = 0;
const expectedCountSuccess = spyCountSuccess + 1;
const successCallback = () => {
spyCountSuccess++;
};
const pathImage = 'https://i.imgur.com/Wutekcp.jpg';
imageDownload(pathImage, successCallback);
const actualCountSuccess = spyCountSuccess;
return CHECK(actualCountSuccess, expectedCountSuccess);
})
);
With the code above I will always i get false, even if the image is loaded, because I am not dealing right with the concept of asynchronous, I would like to understand how to adapt the code thus to also test ascincrono code.
I was able to test the asynchronous code after some tests. I do not know if I'll keep it that way, but it's working, I'll try to study these lines and see what I can improve:
const CHECK = (actual, expected) => { return (actual === expected); }; const TEST = (name, ...testFunctions) => { console.log(name+':'); for (let test of testFunctions) if(test === undefined) continue; else console.log(test); }; const TEST_F = (name, f) => { const tStart = performance.now(); const check = f(); const tEnd = performance.now(); const duration = tEnd - tStart; const details = name + ': ' + '(' + duration + ') = ' + check; return details; }; const TEST_F_ASYNC = (name, f) => { const tStart = performance.now(); f((check) => { const tEnd = performance.now(); const duration = tEnd - tStart; const details = name + ': ' + '(' + duration + ') = ' + check; console.log(details); }); }; const imageDownload = (path, successCallback) => { let img = new Image(); img.addEventListener("load", successCallback, false); img.src = path; return img; }; TEST("TestImage", TEST_F_ASYNC("testAsync", (done) => { let spyCountSuccess = 0; const expectedCountSuccess = spyCountSuccess + 1; const successCallback = () => { spyCountSuccess++; const actualCountSuccess = spyCountSuccess; const result = CHECK(actualCountSuccess, expectedCountSuccess) done(result); }; const pathImage = 'https://i.imgur.com/Wutekcp.jpg'; imageDownload(pathImage, successCallback); }), TEST_F('testSync', () => { return CHECK(1, 1); }) );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.