Skip to content

Instantly share code, notes, and snippets.

@samhiatt
Created May 25, 2022 05:34
Show Gist options
  • Save samhiatt/453de791880664ab1365204685832d9d to your computer and use it in GitHub Desktop.
Save samhiatt/453de791880664ab1365204685832d9d to your computer and use it in GitHub Desktop.
Async unit tests with mocha
import assert = require("assert");
describe("Async Tests", function() {
it("uses callback", function(done) {
console.log("Starting test...");
setTimeout(()=>{
assert.ok("Timeout complete");
console.log("Test complete");
done();
}, 1000);
});
it("uses promise", function() {
console.log("Starting test...");
return new Promise((resolve) => {
setTimeout(()=>{
assert.ok("Timeout complete");
console.log("Test complete");
resolve(true);
}, 1000);
});
});
it("uses async func", async function() {
await new Promise((resolve) => {
setTimeout(()=>{
resolve(true);
}, 1000);
});
assert.ok("Timeout complete");
console.log("Test complete");
});
it("fails if 'done(return_val)' isn't called.", function(done) {
console.log("Starting test...");
return new Promise((resolve) => {
setTimeout(()=>{
assert.ok("Timeout complete");
console.log("Test complete");
resolve(true);
}, 1000);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment