Got simple task: name the season of the year according to the input date. Wrote simple code:
function getSeason(date) {
const isValidDate = (d) => {
return d instanceof Date && !isNaN(d);
};
let season = "";
if (date === undefined) return "Unable to determine the time of year!";
if (!isValidDate(date)) throw new Error("Invalid date!");
const springStart = new Date(date.getFullYear(), 2, 1);
const summerStart = new Date(date.getFullYear(), 5, 1);
const autumnStart = new Date(date.getFullYear(), 8, 1);
const winterStart = new Date(date.getFullYear(), 11, 1);
if (date >= springStart && date < summerStart) {
season = "spring";
} else if (date >= summerStart && date < autumnStart) {
season = "summer";
} else if (date >= autumnStart && date < winterStart) {
season = "autumn";
} else if (date >= winterStart && date < springStart) {
season = "winter";
} else {
return "winter";
}
return season;
}
Passes all of test, except the last two:
it.optional('throws an error with message "Invalid date!" on tricky moment', function () {
const fakeDate = {
toString() {
return Date.prototype.toString.call(new Date());
},
[Symbol.toStringTag]: 'Date'
};
Object.setPrototypeOf(fakeDate, Object.getPrototypeOf(new Date()));
const res = checkForThrowingErrors.call(this, [
() => getSeason(fakeDate)
], 'Invalid date!');
assert.strictEqual(res.every($ => $ === CORRECT_RESULT_MSG), true);
});
Tryin to check prototypes, getPrototype and everything else.
What the hell it wants from me? Got actual: false; expected: true on both. The getSeason() func sould return string (it's ok)