Two Shelf tests pass when the whole file runs. One of them fails when you run it alone. The other fails when the order of the file is changed.
Here are the two tests with a very small runner, so you can see the whole thing run.
const tests = [];
function test(name, body) {
tests.push({ name, body });
}
let basket = [];
test("adds one book", () => {
basket.push("Dune");
console.log("adds one book: " + basket.length);
});
test("adds two books", () => {
basket.push("Emma");
basket.push("Ulysses");
console.log("adds two books: " + basket.length);
});
for (const { body } of tests) {
body();
}
adds one book: 1
adds two books: 3
The second test added two books and counted three. The basket still held the book from the first test.
Scope: where a name can be seen
The scope of a variable is the part of the code that can use it. A variable declared with let or const belongs to the nearest pair of curly brackets around it. Code inside those brackets can use it, including code in brackets nested deeper. Code outside cannot.
const site = "Shelf";
function describe() {
const page = "loans";
console.log(site + " / " + page);
}
describe();
console.log(typeof page);
Shelf / loans
undefined
describe can see site, which was declared outside it. The last line cannot see page, which was declared inside describe. typeof on a name that does not exist gives "undefined" and raises no error, so it is a safe way to show this.
basket in the first example was declared outside both tests. Both can see it, and both change the same array.
Closures: a function keeps what it can see
A function keeps access to the variables around it, even when it runs much later. A function together with the variables it keeps is called a closure.
function makeCounter() {
let count = 0;
return () => {
count = count + 1;
return count;
};
}
const next = makeCounter();
console.log(next());
console.log(next());
const other = makeCounter();
console.log(other());
1
2
1
makeCounter has returned, and count is still alive, because the arrow function uses it. Each call to makeCounter creates a new count, so other starts again at 1.
Every test callback is a closure. The runner stores the callback and calls it later. When it runs, it reads the variables around it as they are at that moment, not as they were when the test was written.
Give each test its own state
The fix is to create the value inside the test, so each test gets a new one.
const tests = [];
function test(name, body) {
tests.push({ name, body });
}
test("adds one book", () => {
const basket = [];
basket.push("Dune");
console.log("adds one book: " + basket.length);
});
test("adds two books", () => {
const basket = [];
basket.push("Emma");
basket.push("Ulysses");
console.log("adds two books: " + basket.length);
});
for (const { body } of [...tests].reverse()) {
body();
}
adds two books: 2
adds one book: 1
The last three lines run the tests in reverse order, and both still pass. That is the check to remember. A test that only passes in one order is not independent.
When several tests need the same starting state, test runners have a hook that runs before each test. In Playwright it is test.beforeEach. The hook assigns a new value every time, so no test sees what the one before left behind.
This costs teams real time. A suite passes for months. Then someone adds a test in the middle of a file, or the runner starts using several workers, and ten tests fail that nobody touched. The cause is one shared variable at the top of the file.
Remember this
A variable belongs to the brackets it is declared in, and every function inside them can use it. A test callback reads shared variables as they are when it runs. Create state inside the test, or reset it in a hook before each test.
Reference: Closures on MDN.