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

@ObservableState crashes snapshots with bad access #3550

Open
3 tasks done
aj2109 opened this issue Jan 10, 2025 · 0 comments
Open
3 tasks done

@ObservableState crashes snapshots with bad access #3550

aj2109 opened this issue Jan 10, 2025 · 0 comments
Labels
bug Something isn't working due to a bug in the library.

Comments

@aj2109
Copy link

aj2109 commented Jan 10, 2025

Description

I am trying to write a unit test that will snapshot a View that has an observableState reducer, however doing so causes bad access. I have produced a simple example to reproduce this below:

View and Reducer:

public struct DummyViewCrashes: View {
    let store: StoreOf<DummyReducerCrashes>
    public init(store: StoreOf<DummyReducerCrashes>) {
        self.store = store
    }
    public var body: some View {
        return Text("")
    }
}
@Reducer
public struct DummyReducerCrashes {
    @ObservableState
    public struct State: Equatable {}
    public enum Action {}
    public var body: some ReducerOf<Self> {
        Reduce { state, action in
            switch action {
            }
        }
    }
}

public struct DummyViewNoCrashes: View {
    let store: StoreOf<DummyReducerNoCrashes>
    public init(store: StoreOf<DummyReducerNoCrashes>){
        self.store = store
    }
    public var body: some View {
        return Text("")
    }    
}

@Reducer
public struct DummyReducerNoCrashes {
    
    public struct State: Equatable {}
    
    public enum Action {}
    public var body: some ReducerOf<Self> {
        Reduce { state, action in
            switch action {
            }
        }
    }
}

Unit test:


    @MainActor
    func testCrash() async throws {
        let view = DummyViewCrashes(store: .init(initialState: .init(), reducer: {
            DummyReducerCrashes()
        }))
                
        assertSnapshot(of: view, as: .dump)
    }
    
    @MainActor
    func testNoCrash() async throws {
        let view = DummyViewNoCrashes(store: .init(initialState: .init(), reducer: {
            DummyReducerNoCrashes()
        }))
                
        assertSnapshot(of: view, as: .dump)
    }`
    
    
    By contrast, a vanilla Observable model and SwiftUI vanilla view, does not crash:
    
    View and model:
    `@available(iOS 17.0, *)
@Observable
public class ObservableModel {
    let someText = ""
}

@available(iOS 17.0, *)
public struct VanillaView: View {
    @State var observableModel: ObservableModel
    public init(observableModel: ObservableModel) {
        self.observableModel = observableModel
    }
    public var body: some View {
        return Text("")
    }
}

Test:

  @available(iOS 17.0, *)
   @MainActor
   func testVanillaObservable() async throws {
       let view = VanillaView(observableModel: .init())
               
       assertSnapshot(of: view, as: .dump)
   }

Checklist

  • I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

The test runs and passes or fails

Actual behavior

The test crashes with Thread 1: EXC_BAD_ACCESS (code=2, address=0x16cbdbbe8) on let mirror = Mirror(reflecting: value) in private func snap<T>(_ value: T, name: String? = nil, indent: Int = 0) -> String {... in the Snapshotting class

Reproducing project

No response

The Composable Architecture version information

1.16.1

Destination operating system

iOS 18 iPhone 16 Pro sim

Xcode version information

Xcode 16.2

Swift Compiler version information

No response

@aj2109 aj2109 added the bug Something isn't working due to a bug in the library. label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working due to a bug in the library.
Projects
None yet
Development

No branches or pull requests

1 participant