{"id":18184943,"url":"https://github.com/alexkorban/snap_assert","last_synced_at":"2025-06-30T21:36:50.027Z","repository":{"id":187372515,"uuid":"674980771","full_name":"alexkorban/snap_assert","owner":"alexkorban","description":"An experiment in self-modifying code, or, instant snapshot testing inside your `ex_unit` tests.","archived":false,"fork":false,"pushed_at":"2023-10-15T05:15:37.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-07T12:48:38.617Z","etag":null,"topics":["elixir"],"latest_commit_sha":null,"homepage":"https://hexdocs.pm/snap_assert","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexkorban.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-05T11:27:25.000Z","updated_at":"2023-10-15T05:25:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"d39898c3-1ac3-47e6-aecf-8c0974b67b69","html_url":"https://github.com/alexkorban/snap_assert","commit_stats":{"total_commits":8,"total_committers":1,"mean_commits":8.0,"dds":0.0,"last_synced_commit":"af682745f521a0ea91fdcd276d39c6d457289581"},"previous_names":["alexkorban/snap_assert"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alexkorban/snap_assert","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexkorban%2Fsnap_assert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexkorban%2Fsnap_assert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexkorban%2Fsnap_assert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexkorban%2Fsnap_assert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexkorban","download_url":"https://codeload.github.com/alexkorban/snap_assert/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexkorban%2Fsnap_assert/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262854766,"owners_count":23375186,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir"],"created_at":"2024-11-02T22:05:26.642Z","updated_at":"2025-06-30T21:36:50.003Z","avatar_url":"https://github.com/alexkorban.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# snap_assert\n\nAn experiment in self-modifying code, or, instant snapshot testing inside your unit tests.\n\nThe `snap_assert` macro inserts the result of evaluating its argument directly into your source code as its second argument.\n`snap_assert` with two arguments becomes a regular `ex_unit` assert.\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `snap_assert` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:snap_assert, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\nDocumentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)\nand published on [HexDocs](https://hexdocs.pm). Once published, the docs can\nbe found at \u003chttps://hexdocs.pm/snap_assert\u003e.\n\n## Usage\n\n1. Import `Snap` in your test file\n2. Use `snap_assert \u003csome expression\u003e` in one of the tests\n3. Run the tests with `mix test`\n4. Tests will pass and `snap_assert \u003csome expression\u003e` will turn into `snap_assert \u003csome expression\u003e, \u003cexpression value\u003e`\n5. Run the tests again; `snap_assert` with two arguments will now be equivalent to a regular assert so tests will pass.\n\nMore concrete example:\n\n- Add `snap_assert(String.upcase(\"hello\"))` to a test\n- Run `mix test` (tests will pass)\n- The test file is now updated so that you have `snap_assert(String.upcase(\"hello\"), \"HELLO\")`\n- Run `mix test` again - tests will pass\n- Change the expression to `snap_assert(String.upcase(\"hello\"), \"GOODBYE\")`\n- Run `mix test` again - tests will fail, ie. now you have a regular assert.\n\nWhen working with `snap_assert`, it's useful to have the tests run on every change, for example with:\n\n```\nfswatch lib test | mix test --listen-on-stdin --stale\n```\n\n## Why?\n\nI was inspired by this post by Ian Henry: https://ianthehenry.com/posts/my-kind-of-repl/\n\nAt first, the idea of inserting the value of an expression into an assert seems tautological.\nHowever, consider how we often write functions: start with a draft implementation, try it out in the REPL,\nrealise it's incomplete, make tweaks, try again, repeat until done. Then write unit tests, possibly\nby copy-pasting from the REPL.\n\n`snap_assert` moves some of that REPL into your test file. When you run tests which include a `snap_assert`,\nyou'll see the results directly in your test file. If they are as expected - great, you now have a unit test\nfor free. If not, delete the incorrect value, fix your function, run the tests again. Rinse and repeat.\n\nI was skeptical at first, but I've been trying this macro while working on a package, and it feels good.\n\nOf course, `snap_assert` isn't suitable for all tests: sometimes you want to check for a substring match, or a pattern match,\nor check that a message is received, and in those cases you still need to use regular ex_unit functionality.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexkorban%2Fsnap_assert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexkorban%2Fsnap_assert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexkorban%2Fsnap_assert/lists"}