title: My mocha tests work separately, but fail when run all at once
body: <p>This probably has to do with asynchronous code, but I'm not sure what.  The both pass when I run them apart from each other: <code>mocha test/models.coffee</code> and <code>mocha test/login.coffee</code></p>
  
  <p>But <code>describe 'saving another user with same username', -&gt;</code> fails when I run the tests together with <code>npm test</code>.  I imagine the database gets cleared as it's running that step so it saves instead of producing an error because it's supposed to be a unique value.</p>
  
  <p>ALSO: I am extremely new at this software testing stuff, if anyone has any tips, or wants to criticize my blatant mistakes, feel free to email me (check my profile)</p>
  
  <p>Thanks!!</p>
  
  <p>Here are my two test files (coffeescript):</p>
  
  <p><code>/test/models.coffee</code></p>
  
  <pre><code>should = require 'should'
  {User} = require '../models'
  
  # function to find our test user, John Egbert
  findJohn = (cb) -&gt;
    User.findOne {'public.username': 'john'}, (err, john) -&gt;
      throw err if err
      cb(john)
  
  before (done) -&gt;
  
    # Drop all users from database
    User.collection.drop()
  
    # Create our test user, his username is John Egbert
    User.create
      password: 'test123'
      public: {username: 'john'}, done
  
  describe 'create user', -&gt;
    it 'should create a document that can be found', (done) -&gt;
      findJohn (user) -&gt;
        should.exist user
        done()
  
  describe 'user password', -&gt;
    it 'should NOT be stored in plaintext', (done) -&gt;
      findJohn (user) -&gt;
        user.password.should.not.eql 'test123'
        done()
  
    it 'should return true for matching password', (done) -&gt;
      findJohn (user) -&gt;
        user.comparePassword 'test123', (err, isMatch) -&gt;
          isMatch.should.eql true
          done()
  
    it 'should return false for non-matching password', (done) -&gt;
      findJohn (user) -&gt;
        user.comparePassword 'wrong_password', (err, isMatch) -&gt;
          isMatch.should.not.eql true
          done()
  
  describe 'saving another user with same username', -&gt;
    it 'should produce an error', (done) -&gt;
      User.create public: {username: 'john'}, (err) -&gt;
        should.exist err
        done()
  </code></pre>
  
  <p><code>/test/login.coffee</code>:</p>
  
  <pre><code>should = require 'should'
  {User} = require '../models'
  login = require '../services/login'
  
  before (done) -&gt;
  
    # Drop all users from database
    User.collection.drop()
  
    # Create our test user, his username is John Egbert
    User.create
      password: 'test123'
      public: {username: 'john'}, done
  
  describe 'login', -&gt;
    it 'should return true for an existing username/password combo', (done) -&gt;
      login username: 'john', password: 'test123', (err, loggedIn) -&gt;
        should.not.exist(err)
        loggedIn.should.be.true
        done()
  
    it 'should return false for a bad username/password combo', (done) -&gt;
      login username: 'john', password: 'wrong_pass', (err, loggedIn) -&gt;
        should.not.exist(err)
        loggedIn.should.be.false
        done()
  </code></pre>
  
  <p>And <code>/models.coffee</code></p>
  
  <pre><code>fs = require 'fs'
  path = require 'path'
  mongoose = require 'mongoose'
  
  #Connect to mongodb
  #TODO: env variable to choose production/development/testing databases
  mongoose.connect 'localhost', 'siglr'
  
  models = {}
  
  for file in fs.readdirSync './schemas'
    if path.extname(file) is '.coffee'
      modelName = path.basename file, '.coffee'
      schema = require "./schemas/#{modelName}"
      models[modelName] = mongoose.model modelName, schema
  
  # key is model name, value is actual mongoose model
  module.exports = models
  </code></pre>