   afterEach (done) ->
     if httpServer
       httpServer.close()
+      httpServer = null
     if ru
-      ru.disconnect done
+      ru.disconnect (err) ->
+        ru = null
+        done err
     else
       done()
 

    connectClient client, (err, def) ->
      return callback err, client, def, state, httpServer

# FIXME: move to protocol

describe 'Mocha compatibility runner', ->
  httpServer = null

      mochacompat.setup options, (err, state, server) ->
        httpServer = server

        ru = new runner.Runner runtimeDefinition(options)
        runAllComponentTests ru, (err, state) ->
          return done err if err
          chai.expect(state).to.have.length 1
          cases = state[0].cases
          chai.expect(cases).to.have.length 1
          chai.expect(cases[0].passed, 'testcase did not pass').to.equal true
          done()

  describe 'running a failing test', ->
    testcase = null
    it 'should recorded 1 failed test', (done) ->
      options =
        files: [ testPath('bdd-simple-failing.coffee') ]
      mochacompat.setup options, (err, state, server) ->
        httpServer = server
        ru = new runner.Runner runtimeDefinition(options)
        runAllComponentTests ru, (err, state) ->
          return done err if err
          chai.expect(state).to.have.length 1
          cases = state[0].cases
          chai.expect(cases).to.have.length 1
          testcase = cases[0]
          chai.expect(testcase.passed, 'failing testcase passed').to.equal false
          done()
    it 'has error message of the Chai assertion', ->
      chai.expect(testcase?.error).to.contain 'expected 42 to equal 41'
