{"id":26120657,"url":"https://github.com/swiftrex/testingextensions","last_synced_at":"2025-04-13T12:10:33.582Z","repository":{"id":45346775,"uuid":"267448097","full_name":"SwiftRex/TestingExtensions","owner":"SwiftRex","description":"Testing helpers and extensions for SwiftRex","archived":false,"fork":false,"pushed_at":"2024-11-29T13:16:54.000Z","size":51,"stargazers_count":2,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T22:38:19.359Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SwiftRex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-27T23:35:45.000Z","updated_at":"2024-11-27T10:47:06.000Z","dependencies_parsed_at":"2025-03-10T13:43:47.144Z","dependency_job_id":"aee89221-21a3-4ca1-ba7d-f7e8b7801df2","html_url":"https://github.com/SwiftRex/TestingExtensions","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwiftRex%2FTestingExtensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwiftRex%2FTestingExtensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwiftRex%2FTestingExtensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwiftRex%2FTestingExtensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SwiftRex","download_url":"https://codeload.github.com/SwiftRex/TestingExtensions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248710445,"owners_count":21149190,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-10T13:43:37.982Z","updated_at":"2025-04-13T12:10:33.556Z","avatar_url":"https://github.com/SwiftRex.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TestingExtensions\nTesting helpers and extensions for SwiftRex and Combine\n\n## SwiftRex Functional Tests (Use Case)\n```swift\nlet (dependencies, scheduler) = self.setup\n\nassert(\n    initialValue: AppState.initial,\n    reducer: Reducer.myReducer.lift(action: \\.actionReducerScope, state: \\.stateReducerScope),\n    middleware: MyHandler.middleware.inject(dependencies),\n    steps: {\n        Send(action: .certainAction(.verySpecific))\n\n        SideEffectResult {\n            scheduler.advance(by: .seconds(5))\n        }\n\n        Send(action: .anotherAction(.withSomeValues(list: [], date: dependencies.now())))\n\n        // if you receive an action and don't add this block, the test will fail to remind you\n        Receive { action -\u003e Bool in\n            // validates that the received action is what you would expect\n            // if this function returns false, the test will fail to show you that you've got an unexpected action\n            if case let .my(.expectedAction(list)) = action {\n                return list.isEmpty\n            } else {\n                return false\n            }\n        }\n\n        SideEffectResult {\n            scheduler.advance(by: .seconds(5))\n        }\n\n        Send(action: .anotherAction(.stopSomething)).expectStateToHaveChanged { state in\n            // if during Send or Receive action, your state is expected to mutate, you must indicate which change is expected to happen here:\n            state.somePropertyShouldHaveChangedTo = true\n            // any unexpected state mutation will fail the test, as well as any expected state mutation that doesn't occur, will also fail the test\n        }\n\n        SideEffectResult {\n            scheduler.advance(by: .seconds(5))\n        }\n\n        // if you receive an action and don't add this block, the test will fail to remind you\n        Receive { action -\u003e Bool in\n            // validates that the received action is what you would expect\n            // if this function returns false, the test will fail to show you that you've got an unexpected action\n            if case let .my(.expectedAction(list)) = action {\n                return list.isEmpty\n            } else {\n                return false\n            }\n        }.expectStateToHaveChanged { state in\n            // if during Send or Receive action, your state is expected to mutate, you must indicate which change is expected to happen here:\n            state.somePropertyShouldHaveChangedTo = true\n            // any unexpected state mutation will fail the test, as well as any expected state mutation that doesn't occur, will also fail the test\n        }\n    }\n)\n```\n\n## Better diagnostics\nResult Builders (https://developer.apple.com/videos/play/wwdc2021/10253/) are awesome, however the diagnostics in case of type mismatch or failure to\ninfer the type expression are misleading, incomplete or confusing. Although the Swift Core Team keeps improving the diagnostics every release, you may\neventually find yourself in a compiler error that is tricky to understand. In that case, you may want to use the old format, that use plain arrays.\n\n```swift\n// Instead of:\nassert(\n    // ...\n    steps: {\n        Send(action: .certainAction(.verySpecific))\n\n        SideEffectResult {\n            scheduler.advance(by: .seconds(5))\n        }\n    }\n)\n\n// You can use:\nassert(\n    // ...\n    steps: [\n        Send(action: .certainAction(.verySpecific))\n            .asStep,\n            \n        SideEffectResult {\n            scheduler.advance(by: .seconds(5))\n        }.asStep\n    ]\n)\n```\n\nFor that:\n- replace `steps: { }` curly-brackets for square-brackets `steps: []`\n- add `.asStep` after each step\n- add comma (,) after each step, except the last\n\nThe syntax is not so elegant, but the diagnostics are better.\n\n## Combine\n\nValidate Output of Publishers\n```swift\nlet operation = assert(\n    publisher: myPublisher,\n    eventuallyReceives: \"🙉\", \"🙊\", \"🙈\",\n    andCompletes: false\n)\nsomethingHappensAndPublisherReceives(\"🙉\")\nsomethingHappensAndPublisherReceives(\"🙊\")\nsomethingHappensAndPublisherReceives(\"🙈\")\n\noperation.wait(0.0001)\n```\n\nValidate Output and Successful Completion of Publishers\n```swift\nlet operation = assert(\n    publisher: myPublisher,\n    eventuallyReceives: \"🙉\", \"🙊\", \"🙈\",\n    andCompletesWith: .isSuccess\n)\nsomethingHappensAndPublisherReceives(\"🙉\")\nsomethingHappensAndPublisherReceives(\"🙊\")\nsomethingHappensAndPublisherReceives(\"🙈\")\nsomethingHappensAndPublisherCompletes()\n\noperation.wait(0.0001)\n```\n\nValidate Output and some Failure of Publishers\n```swift\nlet operation = assert(\n    publisher: myPublisher,\n    eventuallyReceives: \"🙉\", \"🙊\", \"🙈\",\n    andCompletesWith: .isFailure\n)\nsomethingHappensAndPublisherReceives(\"🙉\")\nsomethingHappensAndPublisherReceives(\"🙊\")\nsomethingHappensAndPublisherReceives(\"🙈\")\nsomethingHappensAndPublisherFails(SomeError())\n\noperation.wait(0.0001)\n```\n\nValidate Output and specific Failure of Publishers\n```swift\nlet operation = assert(\n    publisher: myPublisher,\n    eventuallyReceives: \"🙉\", \"🙊\", \"🙈\",\n    andCompletesWith: .failedWithError { error in error == SomeError(\"123\") }\n)\nsomethingHappensAndPublisherReceives(\"🙉\")\nsomethingHappensAndPublisherReceives(\"🙊\")\nsomethingHappensAndPublisherReceives(\"🙈\")\nsomethingHappensAndPublisherFails(SomeError(\"123\"))\n\noperation.wait(0.0001)\n```\n\nValidate No Output but completion of Publishers\n```swift\nlet operation = assert(\n    publisher: myPublisher,\n    completesWithoutValues: .isSuccess\n)\nsomethingHappensAndPublisherCompletes()\n\noperation.wait(0.0001)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswiftrex%2Ftestingextensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswiftrex%2Ftestingextensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswiftrex%2Ftestingextensions/lists"}