Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

docs for hooks

TJ Holowaychuk2df96afc00e2
diff --git a/index.html b/index.html
index 4e2572ad..3df38dd9 100644
--- a/index.html
+++ b/index.html
@@ -97,6 +97,33 @@
})
</code></pre>
+<p>All "hooks", that is <code>before()</code>, <code>after()</code>, <code>beforeEach()</code>, <code>afterEach()</code> may be sync or async as well, behaving much like a regular test-case. For example you may wish to populate database with dummy content before each test:</p>
+
+<pre><code>describe('Connection', function(){
+ var db = new Connection
+ , tobi = new User('tobi')
+ , loki = new User('loki')
+ , jane = new User('jane');
+
+ beforeEach(function(done){
+ db.clear(function(err){
+ if (err) return done(err);
+ db.save([tobi, loki, jane], done);
+ });
+ })
+
+ describe('#find()', function(){
+ it('respond with matching records', function(done){
+ db.find({ type: 'User' }, function(err, res){
+ if (err) return done(err);
+ res.should.have.length(3);
+ done();
+ })
+ })
+ })
+})
+</code></pre>
+
<h2>Pending tests</h2>
<p>Pending test-cases are simply those without a callback:</p>
diff --git a/index.md b/index.md
index f0f246df..55105861 100644
--- a/index.md
+++ b/index.md
@@ -76,6 +76,32 @@ Testing asynchronous code with Mocha could not be simpler! Simply invoke the cal
})
})
+ All "hooks", that is `before()`, `after()`, `beforeEach()`, `afterEach()` may be sync or async as well, behaving much like a regular test-case. For example you may wish to populate database with dummy content before each test:
+
+ describe('Connection', function(){
+ var db = new Connection
+ , tobi = new User('tobi')
+ , loki = new User('loki')
+ , jane = new User('jane');
+
+ beforeEach(function(done){
+ db.clear(function(err){
+ if (err) return done(err);
+ db.save([tobi, loki, jane], done);
+ });
+ })
+
+ describe('#find()', function(){
+ it('respond with matching records', function(done){
+ db.find({ type: 'User' }, function(err, res){
+ if (err) return done(err);
+ res.should.have.length(3);
+ done();
+ })
+ })
+ })
+ })
+
## Pending tests
Pending test-cases are simply those without a callback: