Skip to content

Instantly share code, notes, and snippets.

@mfikes
Last active February 16, 2016 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfikes/a043a4772a2bce65dfba to your computer and use it in GitHub Desktop.
Save mfikes/a043a4772a2bce65dfba to your computer and use it in GitHub Desktop.
cljs.test failure line numbers

When running tests with cljs.test the current approach is to examine the stack of a newly-created exception to get the source location of the failing test. But this doesn't appear to work for me.

Consider this test namespace:

(ns foo.core-test
  (:require [cljs.test :refer-macros [deftest is]]))

(deftest test-equality 
  (is (= 0 0))
  (is (= 3 4)))

I get the following using the Node REPL:

cljs.user=> (require 'foo.core-test)
nil
cljs.user=> (cljs.test/run-tests 'foo.core-test)

Testing foo.core-test

FAIL in (test-equality) (at /Users/mfikes/Projects/clojurescript/.cljs_node_repl/cljs/test.js:436:14)
expected: (= 3 4)
  actual: (not (= 3 4))

Ran 1 tests containing 2 assertions.
1 failures, 0 errors.
nil

One alternate approach is to simply report the location of the var set up by the deftest macro.

Here is a revision to cljs.test/do-report, with an update to the :fail case, making use of a couple of helper functions to look at the var meta:

(defn var-file-and-line [v]
  (select-keys (meta v) [:file :line :column]))

(defn current-testing-var []
  (first (:testing-vars (get-current-env))))

(defn do-report [m]
  (let [m (case (:type m)
            :fail  (merge (var-file-and-line (current-testing-var)) m)
            :error (merge (file-and-line (:actual m) 0) m)
            m)]
    (report m)))

This results in

cljs.user=> (require 'foo.core-test)
nil
cljs.user=> (cljs.test/run-tests 'foo.core-test)

Testing foo.core-test

FAIL in (test-equality) (/Users/mfikes/Desktop/src/foo/core_test.cljs:4:1)
expected: (= 3 4)
  actual: (not (= 3 4))

Ran 1 tests containing 2 assertions.
1 failures, 0 errors.
nil

This is better, but it does not pinpoint the line of the failing assertion. (In Clojure, line 6 would be reported.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment