Home:ALL Converter>Read files in mocha tests asynchronously?

Read files in mocha tests asynchronously?

Ask Time:2019-09-08T18:40:45         Author:Azeem

Json Formatter

I'm not getting the value of Input and Output variables. I have tried using this keyword also in the below code.

it("Final Decoding Tests", () => {
  let input = "";
  let output = "";

  fs.readFile("./test/test_data/booksEncoded.txt", { encoding: "utf-8" }, (err, data) => {
    if (!err) {
      this.input = data;
    } else {
      console.log(err);
    }
  });

  fs.readFile("./test/test_data/books.xml", { encoding: "utf-8" }, (err, data) => {
    if (!err) {
      this.output = data;
    } else {
      console.log(err);
    }
  });

  console.log(input); // NO OUTPUT
  console.log(this.output); //PRINTS undefined
});

I think I have to read the files asynchronously using the done callback.

My question is:

Why I'm not getting any values for the input and output outside the fs.readFile methods? and Is there any way to read it using done keyword asynchronously?

Author:Azeem,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57841192/read-files-in-mocha-tests-asynchronously
yy