ITADN

Insta adds numbers to snapshot filenames even though they are already unique

#875ClosedPerson-93 创建于 2026-02-09
bug
P
Person-93commented
### What happened? Extra numbers are added to snapshot filenames if the code that makes the snapshots runs more than once. This makes snapshots hard to track when using a custom test harness like [`datatest_stable`](https://docs.rs/datatest-stable/latest/datatest_stable/). I tried setting the snapshot path differently for each run so that the outputs wouldn't overwrite each other, but `insta` still adds numbers to the filenames. ### Reproduction steps 1. Write a function that is used in multiple test cases. 2. Set the output path in snapshot settings. You can run this integration test file with the default test harness disabled. ```rust use std::path::Path; use insta::Settings; // this calls the `parse` function once for every file that ends in `.htn` datatest_stable::harness! { { test = parse, root = test_files(), pattern = r".*\.htn$" }, } fn parse(path: &Path) -> datatest_stable::Result<()> { let stripped_path = path.strip_prefix(test_files())?; let mut settings = Settings::new(); settings.set_description(stripped_path.to_string_lossy()); // give each snapshot a unique filename based on the input filename settings.set_snapshot_path(Path::new("snapshots").join(stripped_path)); settings.set_prepend_module_to_snapshot(false); settings.bind(|| { let input = std::fs::read_to_string(path)?; let parsed = mock_file_parser(&input)?; insta::assert_debug_snapshot!(parsed); Ok(()) }) } /// Pretend this is actually parsing the input and not just cloning it fn mock_file_parser(input: &str) -> Result<String, std::convert::Infallible> { Ok(input.to_owned()) } fn workspace_root() -> &'static Path { Path::new(env!("CARGO_MANIFEST_DIR")) .parent() .expect("test files path") .parent() .expect("test files path") } fn test_files() -> String { workspace_root() .join("tests/files") .into_os_string() .into_string() .expect("test files path") } ``` ### rustc Version rustc 1.93.0 (254b59607 2026-01-19) ### insta Version 1.46 ### cargo insta --version output cargo-insta 1.46.0 ### What did you expect? I expected the snapshot filenames not to depend on the order in which the snapshots are created.
关闭于 2026-02-13 2 条评论