1- import { describe , it , expect } from 'vitest' ;
1+ import { describe , it , expect , vi , beforeEach } from 'vitest' ;
22import EverywhereBaseCommand from '../../src/lib/command.js' ;
33
4+ class TestCommand extends EverywhereBaseCommand {
5+ async run ( ) : Promise < void > { }
6+
7+ setVerbose ( value : boolean ) : void {
8+ ( this as any ) . _verbose = value ;
9+ }
10+ }
11+
412describe ( 'EverywhereBaseCommand' , ( ) => {
513 describe ( 'baseFlags' , ( ) => {
614 it ( 'defines a plugin-dir flag' , ( ) => {
@@ -19,4 +27,103 @@ describe('EverywhereBaseCommand', () => {
1927 expect ( EverywhereBaseCommand . baseFlags [ 'verbose' ] . char ) . toBe ( 'v' ) ;
2028 } ) ;
2129 } ) ;
30+
31+ describe ( 'isVerbose' , ( ) => {
32+ describe ( 'when verbose has not been set' , ( ) => {
33+ it ( 'returns false' , ( ) => {
34+ const cmd = new TestCommand ( [ ] , { } as any ) ;
35+ expect ( ( cmd as any ) . isVerbose ) . toBe ( false ) ;
36+ } ) ;
37+ } ) ;
38+
39+ describe ( 'when verbose has been set' , ( ) => {
40+ it ( 'returns true' , ( ) => {
41+ const cmd = new TestCommand ( [ ] , { } as any ) ;
42+ cmd . setVerbose ( true ) ;
43+ expect ( ( cmd as any ) . isVerbose ) . toBe ( true ) ;
44+ } ) ;
45+ } ) ;
46+ } ) ;
47+
48+ describe ( 'catch()' , ( ) => {
49+ let cmd : TestCommand ;
50+ let warnSpy : ReturnType < typeof vi . spyOn > ;
51+
52+ beforeEach ( ( ) => {
53+ cmd = new TestCommand ( [ ] , { } as any ) ;
54+ warnSpy = vi . spyOn ( cmd , 'warn' ) . mockImplementation ( ( ) => { } ) ;
55+ } ) ;
56+
57+ describe ( 'when verbose is not set' , ( ) => {
58+ it ( 'does not output the stack trace' , async ( ) => {
59+ const error = new Error ( 'oops' ) ;
60+ error . stack = 'Error: oops\n at src/build.ts:42:5' ;
61+ try {
62+ await cmd . catch ( error ) ;
63+ } catch {
64+ /* oclif re-throws */
65+ }
66+ expect ( warnSpy ) . not . toHaveBeenCalledWith ( expect . stringContaining ( 'at src/build.ts' ) ) ;
67+ } ) ;
68+
69+ it ( 'does not output a timestamp' , async ( ) => {
70+ const error = new Error ( 'oops' ) ;
71+ try {
72+ await cmd . catch ( error ) ;
73+ } catch {
74+ /* oclif re-throws */
75+ }
76+ expect ( warnSpy ) . not . toHaveBeenCalledWith ( expect . stringMatching ( / \d { 4 } - \d { 2 } - \d { 2 } T / ) ) ;
77+ } ) ;
78+ } ) ;
79+
80+ describe ( 'when verbose is set' , ( ) => {
81+ beforeEach ( ( ) => {
82+ cmd . setVerbose ( true ) ;
83+ } ) ;
84+
85+ it ( 'outputs the stack trace' , async ( ) => {
86+ const error = new Error ( 'oops' ) ;
87+ error . stack = 'Error: oops\n at src/build.ts:42:5' ;
88+ try {
89+ await cmd . catch ( error ) ;
90+ } catch {
91+ /* oclif re-throws */
92+ }
93+ expect ( warnSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'at src/build.ts' ) ) ;
94+ } ) ;
95+
96+ it ( 'outputs a timestamp' , async ( ) => {
97+ const error = new Error ( 'oops' ) ;
98+ try {
99+ await cmd . catch ( error ) ;
100+ } catch {
101+ /* oclif re-throws */
102+ }
103+ expect ( warnSpy ) . toHaveBeenCalledWith ( expect . stringMatching ( / \d { 4 } - \d { 2 } - \d { 2 } T / ) ) ;
104+ } ) ;
105+
106+ it ( 'outputs the error code when present' , async ( ) => {
107+ const error = Object . assign ( new Error ( 'oops' ) , { code : 'ENOENT' } ) ;
108+ try {
109+ await cmd . catch ( error ) ;
110+ } catch {
111+ /* oclif re-throws */
112+ }
113+ expect ( warnSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'ENOENT' ) ) ;
114+ } ) ;
115+
116+ it ( 'does not output the error cause' , async ( ) => {
117+ const cause = { token : 'eyJ0b2tlbg.secret.sig' } ;
118+ const error = Object . assign ( new Error ( 'Request failed' ) , { cause } ) ;
119+ try {
120+ await cmd . catch ( error ) ;
121+ } catch {
122+ /* oclif re-throws */
123+ }
124+ const allWarnArgs = warnSpy . mock . calls . flat ( ) . join ( '' ) ;
125+ expect ( allWarnArgs ) . not . toContain ( 'eyJ0b2tlbg' ) ;
126+ } ) ;
127+ } ) ;
128+ } ) ;
22129} ) ;
0 commit comments