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

Running test repeatedly results in duplicate snapshot being created. #577

Closed
Deco354 opened this issue Feb 22, 2022 · 5 comments · Fixed by #585
Closed

Running test repeatedly results in duplicate snapshot being created. #577

Deco354 opened this issue Feb 22, 2022 · 5 comments · Fixed by #585

Comments

@Deco354
Copy link

Deco354 commented Feb 22, 2022

Describe the bug
Running a snapshot test repeatedly with xcode's new 'run test repeatedly' functionality results in additional snapshots being created for that test.

We've a wrapper around this library which I've posted below although the identifier we're passing in to the testName does not include the iteration number at the real testSnapshot callsite.

    public func testSnapshot(_ view: UIView,
                             identifier: String = #function,
                             snapshotType: SnapshotType = .image,
                             precision: CGFloat = 0.95,
                             file: StaticString = #file,
                             line: UInt = #line,
                             waiting: TimeInterval = 0,
                             isArchitectureDependent: Bool = false) {
        semaphore.wait()
        var testID = identifier
        if isArchitectureDependent && (Architecture.isArm64 || Architecture.isRosettaEmulated) {
            testID.append("-arm64")
        }
        
        switch snapshotType {
        case .image:
            assertSnapshot(matching: view,
                           as: .wait(for: waiting, on: .image(precision: Float(precision))),
                           record: isRecording,
                           file: file,
                           testName: testID,
                           line: line)
            
        case .recursiveDescription:
            assertSnapshot(matching: view,
                           as: .wait(for: waiting, on: .recursiveDescription()),
                           record: isRecording,
                           file: file,
                           testName: testID,
                           line: line)
        }
        semaphore.signal()
    }

To Reproduce
Take any snapshot test right click on it and run "test repeatedly". At a certain iteration a number matching this iteration (2 in the screenshots below) will be appended to the snapshot reference image name causing an additional snapshot to be saved and the tests to fail.
image

image

Expected behavior
I'd expect the test to use only one snapshot no matter how many time they're run.

Environment

  • swift-snapshot-testing version [e.g. 1.8.1]
  • Xcode 13.2
  • Swift 5.5
  • OS: iOS 15
@simondelphia
Copy link

A workaround is to set the named parameter

@ghost
Copy link

ghost commented Jul 7, 2022

@simondelphia @Deco354
Thank you. You guys really saved my day.
Running TC on Xcode Cloud will hit the issue.

@Deco354
Copy link
Author

Deco354 commented Sep 22, 2022

@stephencelis Can we reopen this? Using the named parameter is a work-around rather than a fix. It won't be immediately obvious to anyone other than those within this thread.

@stephencelis
Copy link
Member

@Deco354 this was closed by merging #585. Are you still having issues running repeatedly in yesterday's 1.10.0 release?

@Deco354
Copy link
Author

Deco354 commented Sep 23, 2022

Sorry I must have skimmed this thread and missed #585 completely.
Thanks for getting this merged and thanks @krzysztofpawski for taking the time to fix this 🍾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants