I have another toy project that i use to develop in my free time (No, I do not have too much free time. I just have started a couple of minor projects in the past and have not yet written about it). It’s a javascript based graph drawing tool. It turned out that i had introduced a couple of bugs and only then wanted to have Unittests. So I looked around for unittesting frameworks for Javascript and found QUnit.


QUnit is a powerful, easy-to-use JavaScript unit testing framework.“ says the official homepage (link). It actually is easy to get used to and offers what one can expect from a unit testing framework (which I consider to be a nice test runner and a couple of asserts). The framework is quickly set up and first tests are written soon. The question I asked myself was „If I do this again tomorrow, the day after tomorrow and every day for the next 2 years – how can I simplify my usage of this powerful framework.

And this is where Emacs jumps in!


So currently the situation is like this: I am editing some javascript file and realize „Time for Unittests“. This is what i came up with:

(defun indent-buffer ()
  (indent-region (point-min) (point-max)) )
(defun mp/html-post-processing ()
  "This method looks for a couple of key-strings and replaces them with some meaningful values."
    (goto-char (point-min))
    (when (re-search-forward "%TITLE%" nil t)
      (replace-match (replace-regexp-in-string (regexp-quote ".html") "" (buffer-name) 'fixedcase)))
    (goto-char (point-min))
    (when (re-search-forward "%CSSFILE%" nil t)
      (replace-match (replace-regexp-in-string (regexp-quote ".html") ".css" (buffer-name) 'fixedcase) 'fixedcase))
    (when (re-search-forward "%TESTEE%" nil t)
      (replace-match (replace-regexp-in-string (regexp-quote "-test.html") ".js" (buffer-name) 'fixedcase) 'fixedcase))
    (when (re-search-forward "%UNITTESTS%" nil t)
      (replace-match (replace-regexp-in-string (regexp-quote "-test.html") "-test.js" (buffer-name) 'fixedcase) 'fixedcase))))
(define-auto-insert '("\\.html\\'" . "HTML5 Skeleton for QUnit-Unittests")
  [ '(nil
      "<!DOCTYPE html>\n"
      "<meta charset=\"UTF-8\" />\n"
      "<script src=\"jquery-3.1.0.js\"></script>\n"
      "<script src=\"jquery-ui-1.12.0.js\"></script>\n"
      "<script src=\"subrx.js\"></script>\n"
      "<link rel=\"stylesheet\" href=\"%CSSFILE%\" />\n"
      "</html>\n" )
    mp/html-post-processing ] )
;; with the nifty auto-insert defined above opening a buffer for the unit test is actually as
;; simple as a call to find-file!
(defun mp/qunit-test-for-current-buffer ()
  (let ((test-file-name (replace-regexp-in-string (regexp-quote ".js") "-test.html" (buffer-name))))
      (find-file test-file-name)))

I found this very convincing and currently do it exatcly this way! I hope everybody who shares this snippet enjoys it, too!