-
Notifications
You must be signed in to change notification settings - Fork 987
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tests): New match-strict? cljs.test directive (#20825)
Equality checks in tests using = give a bad experience by default on test failures containing nested data structures. We use the cljs.test directive match? from matcher-combinators library to help compare nested structures. The problem with match? is that its default matcher for maps (embeds) can be too permissive, and this causes surprises. Here we upgrade matcher-combinators to latest, where a new matcher called nested-equals is available. This matcher won't allow extra keys in maps. This matcher eliminates the need for manually adding nested equals matchers as we have to do currently. - Upgrades matcher-combinators from 3.8.8 to 3.9.1 (latest as of 2024-07-19) What changes? When asserting in tests, we now have the option to use match-strict? or match?. Both directives are available by integrating with cljs.test. The code implementing the new match-strict? directive was 100% copied from the library matcher-combinators because we need to wrap the expected value ourselves with matcher-combinators.matchers/nested-equals. It's ugly code, but it's how we can integrate with cljs.test/assert-expr.
- Loading branch information
Showing
8 changed files
with
143 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
(ns test-helpers.matchers | ||
"Internal use. Don't require it directly." | ||
(:require | ||
[cljs.test :as test] | ||
[matcher-combinators.core :as core] | ||
[matcher-combinators.matchers :as matchers] | ||
[matcher-combinators.parser] | ||
[matcher-combinators.result :as result])) | ||
|
||
;; This implementation is identical to `match?`, but wraps the expected value | ||
;; with `nested-equals`. This differs from the default `embeds` matcher on maps, | ||
;; where extra map keys are considered valid. | ||
(defmethod test/assert-expr 'match-strict? | ||
[_ msg form] | ||
`(let [args# (list ~@(rest form)) | ||
[matcher# actual#] args#] | ||
(cond | ||
(not (= 2 (count args#))) | ||
(test/do-report | ||
{:type :fail | ||
:message ~msg | ||
:expected (symbol "`match-strict?` expects 2 arguments: a `matcher` and the `actual`") | ||
:actual (symbol (str (count args#) " were provided: " '~form))}) | ||
|
||
(core/matcher? matcher#) | ||
(let [result# (core/match (matchers/nested-equals matcher#) actual#)] | ||
(test/do-report | ||
(if (core/indicates-match? result#) | ||
{:type :pass | ||
:message ~msg | ||
:expected '~form | ||
:actual (list 'match? matcher# actual#)} | ||
(with-file+line-info | ||
{:type :fail | ||
:message ~msg | ||
:expected '~form | ||
:actual (tagged-for-pretty-printing (list '~'not (list 'match? matcher# actual#)) | ||
result#) | ||
:markup (::result/value result#)})))) | ||
|
||
:else | ||
(test/do-report | ||
{:type :fail | ||
:message ~msg | ||
:expected (str "The first argument of `match-strict?` " | ||
"needs to be a matcher (implement the match protocol)") | ||
:actual '~form})))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(ns test-helpers.matchers | ||
"Some vars in this namespace solely exist to support the matchers.clj file." | ||
(:require-macros test-helpers.matchers) | ||
(:require | ||
[cljs.test :as t] | ||
[matcher-combinators.parser] | ||
[matcher-combinators.printer :as printer] | ||
[matcher-combinators.result :as result])) | ||
|
||
(defrecord Mismatch [summary match-result]) | ||
|
||
(defn tagged-for-pretty-printing | ||
[actual-summary result] | ||
(->Mismatch actual-summary result)) | ||
|
||
(extend-protocol IPrintWithWriter | ||
Mismatch | ||
(-pr-writer [this writer _] | ||
(-write writer (printer/as-string (-> this :match-result ::result/value))))) | ||
|
||
(defn with-file+line-info | ||
[report] | ||
(merge (t/file-and-line (js/Error.) 4) | ||
report)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters