colored-rs / colored

(Rust) Coloring terminal so simple you already know how to do it !
Mozilla Public License 2.0
1.68k stars 82 forks source link

Consider using snapshot tests #156

Closed spenserblack closed 9 months ago

spenserblack commented 9 months ago

When reviewing the code, I noticed an it_works function that just used a bunch of println!s without asserting anything, which I assume is to act as a test for "human eyes" instead of a programmed assertion. To me, that looks like the perfect type of test to be a snapshot test.

I use insta a lot for this. insta has a few assert_*_snapshot macros that will write a snapshot to a name_of_snapshot.snap.new file, which you can then review and remove the .new to "accept" the snapshot (cargo-insta is a helper that can make this easier).

Example

Here's some usage I've used in my own repo with some decent success: https://github.com/spenserblack/gengo/blob/9229e6a46a185ed862517fb6af3e94e5193e2eb2/gengo-bin/tests/test_cli.rs#L49-L53 https://github.com/spenserblack/gengo/blob/9229e6a46a185ed862517fb6af3e94e5193e2eb2/gengo-bin/tests/snapshots/test_cli__color_javascript_repo.snap#L1-L7

Just use cat path/to/snapshot and you can see the colorized output in your terminal (depending on your environment, of course).

kurtlawrence commented 9 months ago

Yep, agreed, that would be a good way to test it.