       (setq i (+ i 1)))
     dir))
 
-(defun mocha-generate-command (&optional mocha-file test)
+(defun mocha-generate-command (debug &optional mocha-file test)
   "The test command to run.
 
+If DEBUG is true, then make this a debug command.
+
 If MOCHA-FILE is specified run just that file otherwise run
-MOCHA-PROJECT-TEST-DIRECTORY
+MOCHA-PROJECT-TEST-DIRECTORY.
 
 IF TEST is specified run mocha with a grep for just that test."
   (let ((path (or mocha-file mocha-project-test-directory))
-        (target (if test (concat "--grep \"" test "\" ") "")))
+        (target (if test (concat "--grep \"" test "\" ") ""))
+        (node-command (concat mocha-which-node (if debug " --debug=5858" "")))
+        (options (concat mocha-options (if debug " -t 21600000"))))
     (concat mocha-environment-variables " "
-            mocha-which-node " "
+            node-command " "
             mocha-command " "
-            mocha-options " "
+            options " "
             target
             path)))
+
+(defun mocha-debug (&optional mocha-file test)
+  "Debug mocha using realgud.
+
+If MOCHA-FILE is specified run just that file otherwise run
+MOCHA-PROJECT-TEST-DIRECTORY.
+
+IF TEST is specified run mocha with a grep for just that test."
+  (if (not (fboundp 'realgud:nodejs))
+      (message "realgud is required to debug mocha")
+    (save-some-buffers (not compilation-ask-about-save)
+                     (when (boundp 'compilation-save-buffers-predicate)
+                       compilation-save-buffers-predicate))
+
+  (when (get-buffer "*mocha tests: debug*")
+    (kill-buffer "*mocha tests: debug*"))
+  (let ((test-command-to-run (mocha-generate-command t mocha-file test))
+        (root-dir (mocha-find-project-root))
+        (debug-command (concat mocha-which-node " debug localhost:5858")))
+    (with-current-buffer (get-buffer-create "*mocha tests: debug*")
+      (setq default-directory root-dir)
+      (compilation-start test-command-to-run 'mocha-compilation-mode (lambda (m) (buffer-name)))
+      (realgud:nodejs debug-command)))))
 
 (defun mocha-run (&optional mocha-file test)
   "Run mocha in a compilation buffer.

  (let ((file (buffer-file-name)) (test-at-point (mocha-find-current-test)))
    (mocha-run file test-at-point)))

(provide 'mocha)
;;; mocha.el ends here


  (when (get-buffer "*mocha tests*")
    (kill-buffer "*mocha tests*"))
  (let ((test-command-to-run (mocha-generate-command nil mocha-file test)) (root-dir (mocha-find-project-root)))
    (with-current-buffer (get-buffer-create "*mocha tests*")
      (setq default-directory root-dir)
      (compilation-start test-command-to-run 'mocha-compilation-mode (lambda (m) (buffer-name))))))
