insta icon indicating copy to clipboard operation
insta copied to clipboard

Whitespace issues with inline snapshots

Open michalt opened this issue 4 years ago • 8 comments

I'm just starting to use insta and it's really cool. But I've run into some problems with whitespace and inline snapshots.

Let's start with:

#[test]
fn test_insta() {
    let input = r###"

    something
    "###;

    assert_display_snapshot!(input, @r###""###);
}

This obviously fails. Running cargo insta review and accepting the new output gives:

#[test]
fn test_insta() {
    let input = r###"

    something
    "###;

    assert_display_snapshot!(input, @r###"


        something
        
    "###);
}

But the test still fails with:

-old snapshot
+new results
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
input
────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────
    1       │-something
          1 │+
          2 │+
          3 │+    something
          4 │+
────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────

Am I doing something wrong here?

michalt avatar Apr 20 '20 08:04 michalt

This is a somewhat known limitation. Leading whitespace with inline snapshots will cause this. I'm not sure if there is a good solution for this with the current model for whitespace trimming we have.

mitsuhiko avatar Apr 20 '20 21:04 mitsuhiko

I'm running into what I think is a related issue.

The following

use insta::assert_display_snapshot;

#[test]
fn test_foo() {
    let body_txt = r#"
# title

### subtitle
"#.trim_matches('\n');
    println!("{:#?}", body_txt);
    assert_display_snapshot!(body_txt, @r###"
    # title

    ### subtitle
    "###);
}

prompts a snapshot update and cargo insta accept doesn't fix the difference.

Instead what seems to work is removing the indent for the generated inline snapshot.

diff --git a/src/main.rs b/src/main.rs
index afdacba..1fce772 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -14,8 +14,8 @@ fn test_foo() {
 "#.trim_matches('\n');
     println!("{:#?}", body_txt);
     assert_display_snapshot!(body_txt, @r###"
-    # title
+# title
 
-    ### subtitle
+### subtitle
     "###);
 }

So when updating the snapshot I cargo insta accept and then remove the indent manually

sbdchd avatar Jun 21 '20 03:06 sbdchd

My recommendation would be to prefix the string with a non whitespace character.

mitsuhiko avatar Nov 15 '20 11:11 mitsuhiko

Does anyone have an example of an inline display snapshot that spans multiple lines working? I can't seem to get any combination of trimming or indenting to make things consistent. Would there be a difference between cargo insta test --accept and cargo test?

I see whitespace errors when I cargo insta test --accept and then cargo test right after:


#[test]
fn test_completions_when_algo() {
    insta::assert_display_snapshot!(completions_for("when algo|"),
    @r###"
    # "Services"
    <"Algolia">
      [Algolia] 

    # "Services"
    <"Magic">
      [Magic] 
    <"Maps">
      [Maps] 
    <"Salesforce">
      [Salesforce] 
    <"PagerDuty">
      [PagerDuty] 
    <"Twilio">
      [Twilio] 
    <"Slack">
      [Slack] 
    "###);
}

image

Currently using insta 1.6.0 and cargo-insta 1.6.0

colelawrence avatar Feb 10 '21 03:02 colelawrence

@colelawrence that's a bug that #166 will solve.

mitsuhiko avatar Feb 16 '21 07:02 mitsuhiko

To what extent could we solve this by removing leading whitespace from both the result and the snapshot?

The downside of that is the test will still pass even if they have different leading whitespace — inline snapshots would quietly allow for some false passes, rather than the current situation of loudly always raising a false fail.

An alternative advanced approach would be to try and calculate the exact leading whitespace from the location of the result, rather than the current approach of removing all leading whitespace. But that would make results indentation sensitive, and possibly have a tail of complication.

max-sixty avatar Dec 27 '22 21:12 max-sixty

I feel like touching this is tricky and will trade one issue for another. A pretty crappy but trivial solution would be to provide a helper to add markers before and after.

mitsuhiko avatar Dec 27 '22 22:12 mitsuhiko