Test
Setup and teardown APIs
To make testing easier, you can use these APIs to help perform setup and teardown for test cases:
Deno.test.beforeAllDeno.test.beforeEachDeno.test.afterAllDeno.test.afterEach
Here’s a concrete example showing how to set up a test database:
;
;
;
// Run once before all tests
testDb = new ":memory:";
`CREATE TABLE users (
id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE
);
`);
});
// Run before each individual test
"DELETE FROM users";
"INSERT INTO users (name, email) VALUES (?, ?)",
);
"Alice", "alice@example.com";
"Bob", "bob@example.com";
});
// Run after each individual test
// Clean up test data
"DELETE FROM users";
});
// Run once after all tests
;
});
"should find user by email", ;
;
user?.name, "Alice";
});
"should create new user", "INSERT INTO users (name, email) VALUES (?, ?)",
);
"Charlie", "charlie@example.com";
;
;
result!.count, 3; // 2 from beforeEach + 1 new
});