Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require_full_match mode always fails with matching snapshots #477

Open
dspicher opened this issue Apr 29, 2024 · 2 comments
Open

require_full_match mode always fails with matching snapshots #477

dspicher opened this issue Apr 29, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@dspicher
Copy link

What happened?

When I run INSTA_REQUIRE_FULL_MATCH=1 cargo test on a dummy snapshot test case, it fails with the following output:

Output
$ INSTA_REQUIRE_FULL_MATCH=1 cargo t
   Compiling insta-tests v0.1.0 (/home/dominik/purgable/insta-tests)
    Finished test [unoptimized + debuginfo] target(s) in 0.44s
     Running unittests src/lib.rs (target/debug/deps/insta_tests-3c106da6b8f1533f)

running 1 test
test tests::test_insta ... FAILED

failures:

---- tests::test_insta stdout ----
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Snapshot Summary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Snapshot file: src/snapshots/insta_tests__tests__insta.snap
Snapshot: insta
Source: src/lib.rs:5
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Expression: 1 + 1
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
-old snapshot
+new results
────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
          - │ snapshots are matching
────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
To update snapshots run `cargo insta review`
Stopped on the first failure. Run `cargo insta test` to run all snapshots.
thread 'tests::test_insta' panicked at .../.cargo/registry/src/index.crates.io-6f17d22bba15001f/insta-1.38.0/src/runtime.rs:563:9:
snapshot assertion for 'insta' failed in line 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    tests::test_insta

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.06s

error: test failed, to rerun pass `--lib`

This is even though the expected output, as well as the expression match the snapshot file.

The normal cargo test snapshot test passes as expected.

Reproduction steps

  1. Run INSTA_REQUIRE_FULL_MATCH=1 cargo test on this minimal reproducing example.

Insta Version

cargo-insta 1.38.0

rustc Version

rustc 1.77.2 (25ef9e3d8 2024-04-09)

What did you expect?

The test should pass.

@dspicher dspicher added the bug Something isn't working label Apr 29, 2024
@max-sixty
Copy link
Sponsor Contributor

This is indeed a bug.

We can work around it temporarily by changing the test name to something that insta doesn't change:

diff --git a/src/lib.rs b/src/lib.rs
index 3f262aa..b23d00e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,7 +1,7 @@
 #[cfg(test)]
 mod tests {
     #[test]
-    fn test_insta() {
-        insta::assert_json_snapshot!(1+1);
+    fn insta() {
+        insta::assert_json_snapshot!(1 + 1);
     }
 }

...since it's caused by the name of the snapshot name being truncated, and so the metadata not matching.

We can fix it by aligning name truncation to the metadata comparison.

---- tests::test_insta stdout ----
[/Users/maximilian/workspace/insta/insta/src/runtime.rs:674:17] &x = Snapshot {
    module_name: "insta_tests",
    snapshot_name: Some(
        "tests__insta",
    ),
    metadata: MetaData {
        source: Some(
            "src/lib.rs",
        ),
        assertion_line: None,
        description: None,
        expression: Some(
            "1 + 1",
        ),
        info: None,
        input_file: None,
    },
    snapshot: SnapshotContents(
        "2",
    ),
}
[/Users/maximilian/workspace/insta/insta/src/runtime.rs:674:17] &new_snapshot = Snapshot {
    module_name: "insta_tests__tests",
    snapshot_name: Some(
        "insta",
    ),
    metadata: MetaData {
        source: Some(
            "src/lib.rs",
        ),
        assertion_line: Some(
            5,
        ),
        description: None,
        expression: Some(
            "1 + 1",
        ),
        info: None,
        input_file: None,
    },
    snapshot: SnapshotContents(
        "2",
    ),
}

@max-sixty
Copy link
Sponsor Contributor

There are a couple of other issues when calling cargo test rather than cargo insta test. I'll come back to this hopefully later this week

max-sixty added a commit to max-sixty/insta that referenced this issue May 2, 2024
max-sixty added a commit to max-sixty/insta that referenced this issue May 3, 2024
This fixes the issues in mitsuhiko#477, stacked on mitsuhiko#483.

It's possible I've still missed something -- I realize that there are plenty of areas I'm still unsure about in the repo. Possibly some slower integration tests with sample projects would be worthwhile; though they don't make for the nimblest of tests...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants