Skip to content

Commit 3a69d3f

Browse files
committed
test: add tests for catch
1 parent bcac21c commit 3a69d3f

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

packages/theme/src/cli/utilities/theme-command.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import {fileExistsSync} from '@shopify/cli-kit/node/fs'
88
import {AbortError} from '@shopify/cli-kit/node/error'
99
import {resolvePath} from '@shopify/cli-kit/node/path'
1010
import {renderConcurrent, renderConfirmationPrompt, renderError, renderWarning} from '@shopify/cli-kit/node/ui'
11+
import {getAllPublicMetadata} from '@shopify/cli-kit/node/metadata'
1112
import type {Writable} from 'stream'
1213

1314
vi.mock('@shopify/cli-kit/node/session')
1415
vi.mock('@shopify/cli-kit/node/environments')
1516
vi.mock('@shopify/cli-kit/node/ui')
1617
vi.mock('./theme-store.js')
1718
vi.mock('@shopify/cli-kit/node/fs')
19+
vi.mock('@shopify/cli-kit/node/metadata')
1820

1921
const CommandConfig = new Config({root: __dirname})
2022

@@ -728,4 +730,67 @@ describe('ThemeCommand', () => {
728730
expect(ensureAuthenticatedThemes).not.toHaveBeenCalled()
729731
})
730732
})
733+
734+
describe('catch', () => {
735+
test('appends request ID to error message when available', async () => {
736+
// Given
737+
vi.mocked(getAllPublicMetadata).mockReturnValue({
738+
cmd_all_last_graphql_request_id: 'test-request-id-12345',
739+
})
740+
741+
await CommandConfig.load()
742+
const command = new TestThemeCommand([], CommandConfig)
743+
const error = new Error('Something went wrong') as Error & {skipOclifErrorHandling: boolean}
744+
error.skipOclifErrorHandling = false
745+
746+
// When
747+
await command.catch(error).catch(() => {
748+
// Expected to throw, we just want to verify the error was modified
749+
})
750+
751+
// Then
752+
expect(error.message).toContain('Something went wrong')
753+
expect(error.message).toContain('Request ID: test-request-id-12345')
754+
})
755+
756+
test('does not append request ID when not available', async () => {
757+
// Given
758+
vi.mocked(getAllPublicMetadata).mockReturnValue({})
759+
760+
await CommandConfig.load()
761+
const command = new TestThemeCommand([], CommandConfig)
762+
const error = new Error('Something went wrong') as Error & {skipOclifErrorHandling: boolean}
763+
error.skipOclifErrorHandling = false
764+
765+
// When
766+
await command.catch(error).catch(() => {
767+
// Expected to throw
768+
})
769+
770+
// Then
771+
expect(error.message).toBe('Something went wrong')
772+
expect(error.message).not.toContain('Request ID:')
773+
})
774+
775+
test('does not duplicate request ID if already present in error message', async () => {
776+
// Given
777+
vi.mocked(getAllPublicMetadata).mockReturnValue({
778+
cmd_all_last_graphql_request_id: 'test-request-id-12345',
779+
})
780+
781+
await CommandConfig.load()
782+
const command = new TestThemeCommand([], CommandConfig)
783+
const error = new Error('API error\n\nRequest ID: existing-id') as Error & {skipOclifErrorHandling: boolean}
784+
error.skipOclifErrorHandling = false
785+
786+
// When
787+
await command.catch(error).catch(() => {
788+
// Expected to throw
789+
})
790+
791+
// Then
792+
expect(error.message).toBe('API error\n\nRequest ID: existing-id')
793+
expect(error.message.match(/Request ID:/g)).toHaveLength(1)
794+
})
795+
})
731796
})

0 commit comments

Comments
 (0)