Created
May 25, 2022 05:34
-
-
Save samhiatt/453de791880664ab1365204685832d9d to your computer and use it in GitHub Desktop.
Async unit tests with mocha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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