Please help - APIs and Microservices Projects

Tell us what’s happening:
My project runs perfect when I test on browser but when I submit it gives 2 test fails for below endopoint

A request to /api/timestamp/1451001600000 should return
{ unix: 1451001600000, utc: “Fri, 25 Dec 2015 00:00:00 GMT” }

Failing test case:

  1. An empty date parameter should return the current time in a JSON object with a unix key
  2. An empty date parameter should return the current time in a JSON object with a ‘utc’ key

Your code so far

app.get("/api/timestamp", (req, res)=>{
  let date = new Date();
  
  return res.json({
    'unix': date.getTime(), 
    'utc': date.toUTCString()
  });  
});

app.get("/api/timestamp/:date", (req, res)=>{
  let resultDate='';

  let inputDate = new Date(req.params.date);

  if(inputDate.toString() == "Invalid Date"){
    //convert from unix date to normal date
    inputDate = new Date(parseInt(req.params.date));
    // console.log(inputDate, "Invalid Date");
  }
 
  if(inputDate.toString() == "Invalid Date") {
    return res.json({error: "Invalid Date"});
  } else {
    resultDate = { unix: inputDate.getTime(), 
    utc: inputDate.toUTCString() };
    // console.log(resultDate);
  }
  return res.json(resultDate);
});

Link to my solution link to solution https://repl.it/@jatinpatel136/boilerplate-project-timestamp

Please help me I am stuck on this issue since one week.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

Challenge: Timestamp Microservice

Link to the challenge:

Hello there,

Your code passes for me. Have you changed it?

Hi,
No I didn’t change anything. But still on my end its not working. I am seriously stuck on this as I can run it on browser but not all test passes.

Hey,
I did one change just now and finally it worked!!! Yay!! I subtracted 3 minutes in order to match my windows time while test case match. date.setMinutes(date.getMinutes()-3);

Well, I know the tests use a 20000 ms leeway for the assertions. So, perhaps that was the issue.

@nhcarrigan Do you think we need to increase the leeway in the tests? I assume 20000 was pulled out of thin air, in which case fine-tuning is needed.

1 Like

I’m wondering if the source of the errors comes from this approach:

  let date = new Date();
  date.setMinutes(date.getMinutes()-3);

As opposed to using the Date.now(); method.

AFAIK, the 20000ms leeway was determined based on the limit set before a test times out. So in theory, if the result were to deviate beyond that 20000ms then the test would have timed out (thus failed regardless)

1 Like

I understood that that approach was needed to pass the tests. It was not there previously.

For now, let us keep this in the back of our minds, for if it pops up again. Thanks, Nicholas.

2 Likes

Sounds good! I’ll keep it on my radar. :slight_smile: