A Shelf reader filters the catalogue to books that are available. A test checks that every result is available. The filter breaks and returns no books. The test passes.
const results = [];
const allAvailable = results.every((book) => book.available);
console.log(allAvailable);
true
every takes a callback. It calls the callback with each item and gives true when the callback said true for all of them.
With no items, the callback never runs. No item said false, so the answer is true. The language defines it this way, and every engine agrees. It is correct logic and a bad test.
Add the question the test forgot to ask:
const results = [];
const allAvailable = results.length > 0 && results.every((book) => book.available);
console.log(allAvailable);
false
&& means "and". The whole line is true only when the list has items and every item passes.
Add this question only where an empty list is wrong. Some lists should be empty. A list of error messages on a page that loaded correctly is one. For that list, every on its own gives the right answer.
The five methods you will use most
Each of these takes a callback and calls it once per item.
const books = [
{ title: "Dune", available: true },
{ title: "Emma", available: false },
{ title: "Ulysses", available: true },
];
const titles = books.map((book) => book.title);
const available = books.filter((book) => book.available);
const emma = books.find((book) => book.title === "Emma");
console.log(titles.join(", "));
console.log(available.length);
console.log(emma.available);
console.log(books.some((book) => book.available));
console.log(books.every((book) => book.available));
Dune, Emma, Ulysses
2
false
true
false
mapbuilds a new array from what the callback returns. Here it turns books into titles.filterbuilds a new array with only the items where the callback returned true.findgives the first item where the callback returned true.someis true when at least one item passes.everyis true when all items pass.
join(", ") turns an array into one string, with the given text between the items.
What they return when nothing matches
This is the part that decides whether your test can fail.
const books = [];
const missing = books.find((book) => book.title === "Hamlet");
const matches = books.filter((book) => book.available);
console.log(missing);
console.log(matches.length);
console.log(Boolean(matches));
console.log(books.some((book) => book.available));
console.log(books.every((book) => book.available));
undefined
0
true
false
true
findgivesundefined. The next line that reads a property from it stops with an error. The next lecture covers that error.filtergives an empty array. An empty array counts as true in a condition, soif (matches)is always true. Checkmatches.length.somegivesfalse.everygivestrue.
There is one more version of the same problem. A test reads the rows from the page, then checks that the count of rows equals the count of titles it collected from those same rows. Both numbers are 0 on a broken page, and 0 equals 0. Compare with a number your test data fixes, not with a second reading of the same page.
Remember this
every is true for an empty list, find gives undefined, and an empty filter result still counts as true. Before a check on a list, assert that the list has the items you expect.
Reference: Array.prototype.every on MDN.