I need to reverse the order of "objects" in json files of very specific format.
I wrote a test to compare the expectedResult with the actual result.
The test expect(result).to.equal(expectedResult);
is passing even though they have different values. I confirmed this by logging both variables to the console.
Why does this happen?
VALUE OF STRING RESULT
"result": [
{
"courses_dept": "math",
"courses_avg": 77.00
},
{
"courses_dept": "epse",
"courses_avg": 88.50
},
{
"courses_dept": "math",
"courses_avg": 93.00
},
{
"courses_dept": "epse",
"courses_avg": 92.19
}
]
VALUE OF STRING EXPECTED RESULT:
"result": [
{
"courses_dept": "epse",
"courses_avg": 92.19
},
{
"courses_dept": "math",
"courses_avg": 93.00
},
{
"courses_dept": "epse",
"courses_avg": 88.50
},
{
"courses_dept": "math",
"courses_avg": 77.00
}
]
}
Here is the code.
describe("reversify", () => {
it("Should reverse file", ()=> {
let result = "";
let expectedResult = "";
try{
let resultPromise = reversify("./test/json_files/inputs/t1.json");
let expectedResultPromise = readJsonFile("./test/json_files/outputs/t1.json");
//AS OF NOW THIS JUST READS THE FILE, HENCE TEST SHOULD FAIL
resultPromise.then((data) =>{
console.log('Result is: ', data);
result = JSON.stringify(data);
});
expectedResultPromise.then(data => {
expectedResult = JSON.stringify(data);
console.log("Expected Result is: ",data);
});
} catch(err) {
throw (err);
} finally {
expect(result).to.equal(expectedResult);
}
});
});
Your expect
statement will get executed before resolving the promise making the value of both result
and expectedResult
equal to ""
at the time of execution. You can use await
to wait for the promise to get resolved.