garageScript / curriculum

GarageScript Curriculum
21 stars 164 forks source link

js2-6-test: refactor with mocks, improve fault coverage for late calls #333

Closed danielthalhuber closed 4 years ago

danielthalhuber commented 4 years ago

Overview:

Examples:

Solution that should pass all tests:

const solution = (arr, time, i = 0) => {
  if (i >= arr.length) return;

  setTimeout(() => {
    arr[i]();
    solution(arr, time, i + 1);
  }, time);
};

Solution that should fail, but passes existing tests:

const solution = (arr, time, i = 0) => {
  if (i < arr.length) {
    // error introduced with time + time: the delay doubles between each call
    solution(arr, time + time, i + 1);
    setTimeout(arr[i], time);
  }
}