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

Snapbox updated the same inline snapshots shared between different tests #345

Closed
weihanglo opened this issue Jun 27, 2024 · 2 comments · Fixed by #347
Closed

Snapbox updated the same inline snapshots shared between different tests #345

weihanglo opened this issue Jun 27, 2024 · 2 comments · Fixed by #347
Labels
A-snapbox Area: snapbox package bug Not as expected

Comments

@weihanglo
Copy link

When an inline snapshot is shared between different #[test], it could be updated multiple times, causing the final updated snapshot being wrong.

Here is a reproducible case using Cargo -Zscript:

---
[dependencies]
snapbox = "=0.6.10"
---

pub fn shared_snapshots() {
    snapbox::assert_data_eq!("Bad hobbits!", snapbox::str![]);
    snapbox::assert_data_eq!("My precious!\n", snapbox::str![]);
}

#[test]
fn frodo_baggins() {
    shared_snapshots();
}
#[test]
fn samwise_gamgee() {
    shared_snapshots();
}
#[test]
fn peregrin_took() {
    shared_snapshots();
}
#[test]
fn meriadoc_brandybuck() {
    shared_snapshots();
}

You can found snapbox has overwritten the expected string multiple times:

$ SNAPSHOTS=overwrite cargo +nightly -Zscript test --manifest-path lib.rs
...
...

Fixing: 
---- expected: /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs:8:48
++++ actual:   In-memory
        1 + My precious!

Fixing: 
---- expected: /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs:8:48
++++ actual:   In-memory
        1 + My precious!

Fixing: 
---- expected: /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs:8:48
++++ actual:   In-memory
        1 + My precious!

Fixing: 
---- expected: /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs:8:48
++++ actual:   In-memory
        1 + My precious!

test frodo_baggins ... ok
test meriadoc_brandybuck ... ok
test samwise_gamgee ... ok
test peregrin_took ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

$ cat /home/user/.cargo/target/69/8aed57ccbe65fe/lib.rs
pub fn shared_snapshots() {
    snapbox::assert_data_eq!("Bad hobbits!", snapbox::str!["Bad hobbits!""Bad hobbits!""Bad hobbits!""Bad hobbits!"]);
    snapbox::assert_data_eq!("My precious!\n", snapbox::str![[r#"
My precious!

"#][r#"
My precious!

"#][r#"
My precious!

"#][r#"
My precious!

"#]]);
}

#[test]
fn frodo_baggins() {
    shared_snapshots();
}
#[test]
fn samwise_gamgee() {
    shared_snapshots();
}
#[test]
fn peregrin_took() {
    shared_snapshots();
}
#[test]
fn meriadoc_brandybuck() {
    shared_snapshots();
}
@weihanglo
Copy link
Author

I am not sure if this is a thing worth fixing in snapbox. To me an inline snapshot should not be shared. Who knows when those tests start diverging from each other.

@epage
Copy link
Contributor

epage commented Jun 28, 2024

For some of the tests I ran into this, it was intentional that the output is the same (post redactions) so I think its reasonable to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-snapbox Area: snapbox package bug Not as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants