From 43abd6d33ab4d7215ca8390544f5a65d3fb1f833 Mon Sep 17 00:00:00 2001 From: Josh Edney Date: Wed, 24 Jan 2024 10:35:29 +0000 Subject: [PATCH] update unit tests for react native cli --- packages/react-native-cli/src/lib/Xcode.ts | 7 +++---- .../src/lib/__test__/Xcode.test.ts | 18 +++++++----------- .../project-after-with-endpoint.pbxproj | 6 +++--- .../__test__/fixtures/project-after.pbxproj | 6 +++--- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/react-native-cli/src/lib/Xcode.ts b/packages/react-native-cli/src/lib/Xcode.ts index a80650f52f..631ffb7d09 100644 --- a/packages/react-native-cli/src/lib/Xcode.ts +++ b/packages/react-native-cli/src/lib/Xcode.ts @@ -33,10 +33,9 @@ export async function updateXcodeProject (projectRoot: string, endpoint: string| const buildPhaseMap = proj?.hash?.project?.objects?.PBXShellScriptBuildPhase || [] logger.info('Ensuring React Native build phase outputs source maps') - const didUpdate = await updateXcodeEnv(projectRoot, logger) logger.info('Adding build phase to upload source maps to Bugsnag') - const didAdd = await addUploadSourceMapsTask(proj, buildPhaseMap, endpoint, logger) + const didUpdate = await updateXcodeEnv(projectRoot, logger) const didChange = didUpdate || didAdd if (!didChange) return @@ -53,7 +52,7 @@ async function addUploadSourceMapsTask ( ): Promise { for (const shellBuildPhaseKey in buildPhaseMap) { const phase = buildPhaseMap[shellBuildPhaseKey] - if (typeof phase.shellScript === 'string' && (phase.shellScript.includes('bugsnag-react-native-xcode.sh') || phase.shellScript.includes('Upload source maps to Bugsnag'))) { + if (typeof phase.shellScript === 'string' && (phase.shellScript.includes('bugsnag-react-native-xcode.sh') || phase.shellScript.includes('npm run bugsnag:upload-ios'))) { logger.warn('An "Upload source maps to Bugsnag" build phase already exists') return false } @@ -79,7 +78,7 @@ async function updateXcodeEnv (projectRoot: string, logger: Logger): Promise { await updateXcodeProject('/random/path', undefined, logger) expect(readFileSyncMock).toHaveBeenCalledWith('/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj', 'utf8') - expect(writeFileMock).toHaveBeenCalledTimes(1) + expect(writeFileMock).toHaveBeenCalledTimes(2) // the added build phase gets a generated build ID, so we need to figure out what that is before doing an exact string match - const matches = /([A-Z0-9]{24}) \/\* Upload source maps to Bugsnag \*\/ = \{/.exec(writeFileMock.mock.calls[0][1] as string) + const matches = /([A-Z0-9]{24}) \/\* Upload source maps to Bugsnag \*\/ = \{/.exec(writeFileMock.mock.calls[1] as unknown as string) if (!matches) throw new Error('Failed to detect build ID') const generatedPhaseId = matches[1] const expectedOutput = (await loadFixture(path.join(__dirname, 'fixtures', 'project-after.pbxproj'))) .replace(/43CF599E6AE1472FAF1EC029/g, generatedPhaseId) - expect(writeFileMock).toHaveBeenCalledWith( + expect(writeFileMock).toHaveBeenLastCalledWith( '/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj', expectedOutput, 'utf8' @@ -86,11 +86,10 @@ test('updateXcodeProject(): success with custom endpoint', async () => { await updateXcodeProject('/random/path', 'https://upload.example.com', logger) expect(readFileSyncMock).toHaveBeenCalledWith('/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj', 'utf8') - expect(writeFileMock).toHaveBeenCalledTimes(1) + expect(writeFileMock).toHaveBeenCalledTimes(2) // the added build phase gets a generated build ID, so we need to figure out what that is before doing an exact string match - const matches = /([A-Z0-9]{24}) \/\* Upload source maps to Bugsnag \*\/ = \{/.exec(writeFileMock.mock.calls[0][1] as string) - + const matches = /([A-Z0-9]{24}) \/\* Upload source maps to Bugsnag \*\/ = \{/.exec(writeFileMock.mock.calls[1] as unknown as string) if (!matches) { throw new Error('Failed to detect build ID') } @@ -99,7 +98,7 @@ test('updateXcodeProject(): success with custom endpoint', async () => { const expectedOutput = (await loadFixture(path.join(__dirname, 'fixtures', 'project-after-with-endpoint.pbxproj'))) .replace(/43CF599E6AE1472FAF1EC029/g, generatedPhaseId) - expect(writeFileMock).toHaveBeenCalledWith( + expect(writeFileMock).toHaveBeenLastCalledWith( '/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj', expectedOutput, 'utf8' @@ -122,11 +121,8 @@ test('updateXcodeProject(): modifications already exist', async () => { const writeFileMock = fs.writeFile as jest.MockedFunction await updateXcodeProject('/random/path', undefined, logger) expect(readFileSyncMock).toHaveBeenCalledWith('/random/path/ios/BugsnagReactNativeCliTest.xcodeproj/project.pbxproj', 'utf8') - expect(writeFileMock).not.toHaveBeenCalled() + expect(writeFileMock).toHaveBeenCalledTimes(2) expect(logger.warn).toHaveBeenCalledWith('An "Upload source maps to Bugsnag" build phase already exists') - expect(logger.warn).toHaveBeenCalledWith(expect.stringMatching( - /The "Bundle React Native Code and Images" build phase \([A-F0-9]{24}\) already includes the required arguments/ - )) }) test('updateXcodeProject(): can\'t find project', async () => { diff --git a/packages/react-native-cli/src/lib/__test__/fixtures/project-after-with-endpoint.pbxproj b/packages/react-native-cli/src/lib/__test__/fixtures/project-after-with-endpoint.pbxproj index 93265ba4a0..e7fd6eaf5d 100644 --- a/packages/react-native-cli/src/lib/__test__/fixtures/project-after-with-endpoint.pbxproj +++ b/packages/react-native-cli/src/lib/__test__/fixtures/project-after-with-endpoint.pbxproj @@ -376,7 +376,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "export EXTRA_PACKAGER_ARGS=\"--sourcemap-output $TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\"\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh"; + shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh"; }; 2D02E4CB1E0B4B27006451C7 /* Bundle React Native Code And Images */ = { isa = PBXShellScriptBuildPhase; @@ -390,7 +390,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "export EXTRA_PACKAGER_ARGS=\"--sourcemap-output $TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\"\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh"; + shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh"; }; 3EBDF05F265B984FA826288C /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; @@ -566,7 +566,7 @@ outputPaths = ( ); shellPath = /bin/sh; - shellScript = "export ENDPOINT='https://upload.example.com'\nSOURCE_MAP=\"$TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\" ../node_modules/@bugsnag/react-native/bugsnag-react-native-xcode.sh"; + shellScript = "npm run bugsnag:upload-ios"; }; /* End PBXShellScriptBuildPhase section */ diff --git a/packages/react-native-cli/src/lib/__test__/fixtures/project-after.pbxproj b/packages/react-native-cli/src/lib/__test__/fixtures/project-after.pbxproj index 8b440d1aa7..e7fd6eaf5d 100644 --- a/packages/react-native-cli/src/lib/__test__/fixtures/project-after.pbxproj +++ b/packages/react-native-cli/src/lib/__test__/fixtures/project-after.pbxproj @@ -376,7 +376,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "export EXTRA_PACKAGER_ARGS=\"--sourcemap-output $TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\"\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh"; + shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh"; }; 2D02E4CB1E0B4B27006451C7 /* Bundle React Native Code And Images */ = { isa = PBXShellScriptBuildPhase; @@ -390,7 +390,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "export EXTRA_PACKAGER_ARGS=\"--sourcemap-output $TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\"\nexport NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh"; + shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh"; }; 3EBDF05F265B984FA826288C /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; @@ -566,7 +566,7 @@ outputPaths = ( ); shellPath = /bin/sh; - shellScript = "SOURCE_MAP=\"$TMPDIR/$(md5 -qs \"$CONFIGURATION_BUILD_DIR\")-main.jsbundle.map\" ../node_modules/@bugsnag/react-native/bugsnag-react-native-xcode.sh"; + shellScript = "npm run bugsnag:upload-ios"; }; /* End PBXShellScriptBuildPhase section */