Test Pre-conditions

I have this function:

(ns cljslab.core.actions.building-blocks)

(defn objs [x]
  {:pre [(not= 0 x)]}
  #js {"data" x})

The goal is to test the precondition - by verifying it returns an error or throws an exception, whichever the case is:

(ns cljslab.core.actions.building-blocks-test
  (:require [cljs.test :as t :refer-macros [deftest is testing]]
            [cljslab.core.actions.building-blocks :refer (action objs)]))

(deftest name-test-2
  (testing "Context of the test assertions"
    (t/assert-predicate "something" (objs 0))))

But I am getting this error:

$ npx shadow-cljs compile test
shadow-cljs - config: /Users/blah/cljslab/shadow-cljs.edn
[:test] Compiling ...
========= Running Tests =======================

Testing cljslab.core.actions.building-blocks-test

ERROR in (name-test-2) (Error:NaN:NaN)
Uncaught exception, not in assertion.
expected: nil
  actual: #object[Error Error: Assert failed: (not= 0 x)]

Ran 2 tests containing 2 assertions.
0 failures, 1 errors.
===============================================
[:test] Build completed. (70 files, 2 compiled, 1 warnings, 4.44s)

------ WARNING #1 - :undeclared-var --------------------------------------------
 File: /Users/blah/cljslab/test/cljslab/core/actions/building_blocks_test.cljs:15:6
--------------------------------------------------------------------------------
  12 |
  13 | (deftest name-test-2
  14 |   (testing "Context of the test assertions"
  15 |     (t/assert-predicate "OK" (objs 0))))
------------^-------------------------------------------------------------------
 Use of undeclared Var cljs.test/assert-predicate
--------------------------------------------------------------------------------
  16 |
--------------------------------------------------------------------------------

What is missing?
(cljs.test/assert-predicate is in the documentation of cljs.test)

cljs.test/assert-predicate is an internal implementation specific function on the macro side, it is not a testing API function.

You’d generally use thrown-with-msg? instead. IIRC (is (thrown-with-msg? :default #"something" (objs 0))), haven’t used it in a long time.

Changed the tests:

(deftest name-test-2
  (testing "Context of the test assertions"
    (is (thrown-with-msg?
         js/Error
         #"object[Error Error: Assert failed: (not= 0 x)]"
         (objs 0)))))

The test output shows this error:

$ npx shadow-cljs compile test
shadow-cljs - config: /Users/blah/cljslab/shadow-cljs.edn
[:test] Compiling ...
========= Running Tests =======================

Testing cljslab.core.actions.building-blocks-test

FAIL in (name-test-2) (cljslab/core/actions/building_blocks_test.cljs:15:9)
Context of the test assertions
expected: (thrown-with-msg? js/Error #"object[Error Error: Assert failed: (not= 0 x)]" (objs 0))
  actual: #object[Error Error: Assert failed: (not= 0 x)]

Ran 2 tests containing 2 assertions.
1 failures, 0 errors.
===============================================
[:test] Build completed. (70 files, 2 compiled, 0 warnings, 4.96s)

Searched GitHub for thrown-with-msg? and this should work.

It only matches the msg, not how the exception is printed. So #"Assert failed: (not= 0 x)" should work. The object[Error parts are not part of the actual error message, just happen to be part of how they error is presented later.

1 Like

That helped! I had to escape the parenthesis inside the regexp:

(deftest name-test-2
  (testing "Context of the test assertions"
    (is (thrown-with-msg?
         js/Error
         #"Assert failed: \(not= 0 x\)" ;; <- escape parens
         (objs 0)))))

OK, so this looks like LLM-generated spam to me.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.