mitsuhiko / insta

A snapshot testing library for rust
https://insta.rs
Apache License 2.0
2.26k stars 102 forks source link

Add a setting to remove part of the module name #601

Closed nyurik closed 2 months ago

nyurik commented 2 months ago

In some cases, especially when the snapshot files reside in a sub-directory next to the test files, snapshot filenames become increasingly hard to manage. I would like to propose a setting to remove some beginning parts of the module name with a setting.

Example setup

My project crate myproj contains a proc macro in the crate myproj-macros. When testing proc-macros, it is the easiest to keep the tests in a sub-directory, e.g. ./myproj-macros/src/tests instead of being in parallel to ./myproj-macros/src because procmacro crates cannot have any public exported functions (this way these are unit, not integration tests, and the entire test submodule is behind #[cfg(test)]).

If my tests use insta, the snapshot dir is created next to them in the ./myproj-macros/src/tests/snapshots. The filename then becomes huge (assuming i am using named tests)

./myproj-macros/src/tests/snapshots/myproj_macros__tests__testfilename_testname.snap

A more readable name would be testfilename__testname.snap

Suggestion

The test name is generated with module_path!() -- which in my case is myproj_macros::tests::testfilename. I think it would be great if we could remove some prefix from it. Some approaches:

max-sixty commented 2 months ago

Yes, is this the same as https://github.com/mitsuhiko/insta/issues/377; do we need more flexibility or is the proposed default there of no repetition sufficient?

max-sixty commented 2 months ago

Closing as likely dupe but please reopen if mistaken, thank you