From cae81d28fc40feff09036f42ce7308cef5b2b009 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Wed, 3 May 2023 13:43:02 -0700 Subject: [PATCH] Make xcode tests actually respect the result --- .../project.pbxproj | 4 -- .../xmtpreactnativesdkexampleUITests.swift | 24 ++++++--- ...ctnativesdkexampleUITestsLaunchTests.swift | 29 ----------- example/src/TestsView.tsx | 52 ++++++++++++++++--- example/src/tests.ts | 4 ++ 5 files changed, 67 insertions(+), 46 deletions(-) delete mode 100644 example/ios/xmtpreactnativesdkexampleUITests/xmtpreactnativesdkexampleUITestsLaunchTests.swift diff --git a/example/ios/xmtpreactnativesdkexample.xcodeproj/project.pbxproj b/example/ios/xmtpreactnativesdkexample.xcodeproj/project.pbxproj index b023fdb75..8b2cdf4fc 100644 --- a/example/ios/xmtpreactnativesdkexample.xcodeproj/project.pbxproj +++ b/example/ios/xmtpreactnativesdkexample.xcodeproj/project.pbxproj @@ -12,7 +12,6 @@ 13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; }; 3E461D99554A48A4959DE609 /* SplashScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */; }; A6A5DB882A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6A5DB872A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift */; }; - A6A5DB8A2A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6A5DB892A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift */; }; B18059E884C0ABDD17F3DC3D /* ExpoModulesProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAC715A2D49A985799AEE119 /* ExpoModulesProvider.swift */; }; BB2F792D24A3F905000567C9 /* Expo.plist in Resources */ = {isa = PBXBuildFile; fileRef = BB2F792C24A3F905000567C9 /* Expo.plist */; }; C90474A97E544153BED95C2A /* noop-file.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDF0078FD601458DA88B0565 /* noop-file.swift */; }; @@ -42,7 +41,6 @@ 80323D3D45487AD85F64F249 /* Pods-xmtpreactnativesdkexample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-xmtpreactnativesdkexample.debug.xcconfig"; path = "Target Support Files/Pods-xmtpreactnativesdkexample/Pods-xmtpreactnativesdkexample.debug.xcconfig"; sourceTree = ""; }; A6A5DB852A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = xmtpreactnativesdkexampleUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; A6A5DB872A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = xmtpreactnativesdkexampleUITests.swift; sourceTree = ""; }; - A6A5DB892A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = xmtpreactnativesdkexampleUITestsLaunchTests.swift; sourceTree = ""; }; AA286B85B6C04FC6940260E9 /* SplashScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = SplashScreen.storyboard; path = xmtpreactnativesdkexample/SplashScreen.storyboard; sourceTree = ""; }; BB2F792C24A3F905000567C9 /* Expo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Expo.plist; sourceTree = ""; }; ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; }; @@ -138,7 +136,6 @@ isa = PBXGroup; children = ( A6A5DB872A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift */, - A6A5DB892A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift */, ); path = xmtpreactnativesdkexampleUITests; sourceTree = ""; @@ -384,7 +381,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - A6A5DB8A2A00551E001DF8C2 /* xmtpreactnativesdkexampleUITestsLaunchTests.swift in Sources */, A6A5DB882A00551E001DF8C2 /* xmtpreactnativesdkexampleUITests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/example/ios/xmtpreactnativesdkexampleUITests/xmtpreactnativesdkexampleUITests.swift b/example/ios/xmtpreactnativesdkexampleUITests/xmtpreactnativesdkexampleUITests.swift index 497a979fe..c44662540 100644 --- a/example/ios/xmtpreactnativesdkexampleUITests/xmtpreactnativesdkexampleUITests.swift +++ b/example/ios/xmtpreactnativesdkexampleUITests/xmtpreactnativesdkexampleUITests.swift @@ -18,24 +18,34 @@ final class xmtpreactnativesdkexampleUITests: XCTestCase { // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. } - func testExample() throws { + func testRunTests() throws { // UI tests must launch the application that they test. let app = XCUIApplication() app.launch() - let button = app.buttons["Enable Test Mode"] + // Go to unit tests page + let button = app.buttons["Unit tests"] XCTAssert(button.waitForExistence(timeout: 3)) button.tap() + // Make sure we're there let view = app.staticTexts["Test View"] XCTAssert(view.waitForExistence(timeout: 3)) - let result = app.staticTexts["result"] - XCTAssert(result.waitForExistence(timeout: 3)) + // Wait for tests to complete + let complete = app.staticTexts["tests-complete"] + XCTAssert(complete.waitForExistence(timeout: 5)) - let input = app.textFields["input"] - XCTAssert(input.waitForExistence(timeout: 3)) - + // See if we have any failures + if app.staticTexts["FAIL"].waitForExistence(timeout: 3) { + // Take a screenshot so we can see what failed in the UI + let screenshot = app.windows.firstMatch.screenshot() + let attachment = XCTAttachment(screenshot: screenshot) + attachment.lifetime = .keepAlways + add(attachment) + + XCTFail("Tests failed.") + } } } diff --git a/example/ios/xmtpreactnativesdkexampleUITests/xmtpreactnativesdkexampleUITestsLaunchTests.swift b/example/ios/xmtpreactnativesdkexampleUITests/xmtpreactnativesdkexampleUITestsLaunchTests.swift deleted file mode 100644 index 35acfdc2f..000000000 --- a/example/ios/xmtpreactnativesdkexampleUITests/xmtpreactnativesdkexampleUITestsLaunchTests.swift +++ /dev/null @@ -1,29 +0,0 @@ -// -// xmtpreactnativesdkexampleUITestsLaunchTests.swift -// xmtpreactnativesdkexampleUITests -// -// Created by Pat Nakajima on 5/1/23. -// - -import XCTest - -final class xmtpreactnativesdkexampleUITestsLaunchTests: XCTestCase { - - override class var runsForEachTargetApplicationUIConfiguration: Bool { - true - } - - override func setUpWithError() throws { - continueAfterFailure = false - } - - func testLaunch() throws { - let app = XCUIApplication() - app.launch() - - let attachment = XCTAttachment(screenshot: app.screenshot()) - attachment.name = "Launch Screen" - attachment.lifetime = .keepAlways - add(attachment) - } -} diff --git a/example/src/TestsView.tsx b/example/src/TestsView.tsx index cb6f3f900..1efec75ef 100644 --- a/example/src/TestsView.tsx +++ b/example/src/TestsView.tsx @@ -5,7 +5,14 @@ import { tests, Test } from "./tests"; type Result = "waiting" | "running" | "success" | "failure" | "error"; -function TestView({ test }: { test: Test }): JSX.Element { +function TestView({ + test, + onComplete, +}: { + test: Test; + onComplete: () => void; +}): JSX.Element { + const [markedComplete, setMarkedComplete] = useState(false); const [result, setResult] = useState("waiting"); const [errorMessage, setErrorMessage] = useState(""); @@ -24,6 +31,11 @@ function TestView({ test }: { test: Test }): JSX.Element { setErrorMessage(JSON.stringify(err)); } } + + if (!markedComplete) { + onComplete(); + setMarkedComplete(true); + } } useEffect(() => { @@ -56,9 +68,19 @@ function TestView({ test }: { test: Test }): JSX.Element { title={result == "running" ? "Running..." : "Run"} /> + {result == "failure" && ( + + {test.name} failed + + )} {errorMessage && ( - Error: {errorMessage} + + Error: {errorMessage} + )} @@ -66,18 +88,36 @@ function TestView({ test }: { test: Test }): JSX.Element { } export default function TestsView(): JSX.Element { - const [text, setText] = useState(""); + const [completedTests, setCompletedTests] = useState(0); return ( - Test View - This view is used by unit tests. + Unit Tests + + + Running {completedTests}/{tests.length} + + + {completedTests == tests.length && ( + Done + )} + {tests.map((test: Test, i) => { - return ; + return ( + { + setCompletedTests((prev) => prev + 1); + }} + key={i} + /> + ); })} diff --git a/example/src/tests.ts b/example/src/tests.ts index 8c7a9da9b..0252b7e3c 100644 --- a/example/src/tests.ts +++ b/example/src/tests.ts @@ -17,6 +17,10 @@ function test(name: string, perform: () => Promise) { tests.push({ name, run: perform }); } +// test("can fail", async () => { +// return false; +// }); + test("can make a client", async () => { const client = await XMTP.Client.createRandom("local"); return client.address.length > 0;