@@ -2,9 +2,17 @@ import {validateApp} from './validate.js'
22import { testAppLinked } from '../models/app/app.test-data.js'
33import { AppErrors } from '../models/app/loader.js'
44import { describe , expect , test , vi } from 'vitest'
5+ import { outputResult } from '@shopify/cli-kit/node/output'
56import { renderError , renderSuccess } from '@shopify/cli-kit/node/ui'
67import { AbortSilentError } from '@shopify/cli-kit/node/error'
78
9+ vi . mock ( '@shopify/cli-kit/node/output' , async ( importOriginal ) => {
10+ const actual = await importOriginal < typeof import ( '@shopify/cli-kit/node/output' ) > ( )
11+ return {
12+ ...actual ,
13+ outputResult : vi . fn ( ) ,
14+ }
15+ } )
816vi . mock ( '@shopify/cli-kit/node/ui' )
917
1018describe ( 'validateApp' , ( ) => {
@@ -18,6 +26,20 @@ describe('validateApp', () => {
1826 // Then
1927 expect ( renderSuccess ) . toHaveBeenCalledWith ( { headline : 'App configuration is valid.' } )
2028 expect ( renderError ) . not . toHaveBeenCalled ( )
29+ expect ( outputResult ) . not . toHaveBeenCalled ( )
30+ } )
31+
32+ test ( 'outputs json success when --json is enabled and there are no errors' , async ( ) => {
33+ // Given
34+ const app = testAppLinked ( )
35+
36+ // When
37+ await validateApp ( app , { json : true } )
38+
39+ // Then
40+ expect ( outputResult ) . toHaveBeenCalledWith ( JSON . stringify ( { valid : true , errors : [ ] } , null , 2 ) )
41+ expect ( renderSuccess ) . not . toHaveBeenCalled ( )
42+ expect ( renderError ) . not . toHaveBeenCalled ( )
2143 } )
2244
2345 test ( 'renders errors and throws when there are validation errors' , async ( ) => {
@@ -35,6 +57,31 @@ describe('validateApp', () => {
3557 body : expect . stringContaining ( 'client_id is required' ) ,
3658 } )
3759 expect ( renderSuccess ) . not . toHaveBeenCalled ( )
60+ expect ( outputResult ) . not . toHaveBeenCalled ( )
61+ } )
62+
63+ test ( 'outputs json errors and throws when --json is enabled and there are validation errors' , async ( ) => {
64+ // Given
65+ const errors = new AppErrors ( )
66+ errors . addError ( '/path/to/shopify.app.toml' , 'client_id is required' )
67+ errors . addError ( '/path/to/extensions/my-ext/shopify.extension.toml' , 'invalid type "unknown"' )
68+ const app = testAppLinked ( )
69+ app . errors = errors
70+
71+ // When / Then
72+ await expect ( validateApp ( app , { json : true } ) ) . rejects . toThrow ( AbortSilentError )
73+ expect ( outputResult ) . toHaveBeenCalledWith (
74+ JSON . stringify (
75+ {
76+ valid : false ,
77+ errors : [ 'client_id is required' , 'invalid type "unknown"' ] ,
78+ } ,
79+ null ,
80+ 2 ,
81+ ) ,
82+ )
83+ expect ( renderError ) . not . toHaveBeenCalled ( )
84+ expect ( renderSuccess ) . not . toHaveBeenCalled ( )
3885 } )
3986
4087 test ( 'renders success when errors object exists but is empty' , async ( ) => {
@@ -48,5 +95,6 @@ describe('validateApp', () => {
4895
4996 // Then
5097 expect ( renderSuccess ) . toHaveBeenCalledWith ( { headline : 'App configuration is valid.' } )
98+ expect ( outputResult ) . not . toHaveBeenCalled ( )
5199 } )
52100} )
0 commit comments