@@ -89,33 +89,60 @@ function createManifest(extra: Partial<ManifestPackage> = {}): ManifestPackage {
89
89
} ;
90
90
}
91
91
92
+ const PROCESS_ERROR_MESSAGE = 'PROCESS ERROR' ;
92
93
async function testPrintAndValidatePackagedFiles ( files : IFile [ ] , cwd : string , manifest : ManifestPackage , options : IPackageOptions , errorExpected : boolean , warningExpected : boolean ) : Promise < void > {
93
94
const originalLogError = log . error ;
94
95
const originalLogWarn = log . warn ;
95
96
const originalProcessExit = process . exit ;
96
97
const warns : string [ ] = [ ] ;
97
98
const errors : string [ ] = [ ] ;
99
+ let exited = false ;
100
+ let errorThrown : string | undefined ;
98
101
log . error = ( message : string ) => errors . push ( message ) ;
99
102
log . warn = ( message : string ) => warns . push ( message ) ;
100
- process . exit = ( ( ) => { throw Error ( 'Error' ) ; } ) as ( ) => never ;
103
+ process . exit = ( ( ) => { exited = true ; throw Error ( PROCESS_ERROR_MESSAGE ) ; } ) as ( ) => never ;
101
104
102
- let exitedOrErrorThrown = false ;
103
105
try {
104
106
await printAndValidatePackagedFiles ( files , cwd , manifest , options ) ;
105
- } catch ( e ) {
106
- exitedOrErrorThrown = true ;
107
+ } catch ( e : any ) {
108
+ if ( e instanceof Error && e . message !== PROCESS_ERROR_MESSAGE ) {
109
+ errorThrown = e . message + '\n' + e . stack ;
110
+ }
107
111
} finally {
108
112
process . exit = originalProcessExit ;
109
113
log . error = originalLogError ;
110
114
log . warn = originalLogWarn ;
111
115
}
112
116
113
- if ( errorExpected !== ! ! errors . length || exitedOrErrorThrown !== errorExpected ) {
114
- throw new Error ( errors . length ? errors . join ( '\n' ) : 'Expected error' ) ;
117
+ // Validate that the correct number of errors and warnings were thrown
118
+ const messages = [ ] ;
119
+
120
+ if ( errorExpected !== ! ! errors . length ) {
121
+ if ( errors . length ) {
122
+ messages . push ( ...errors ) ;
123
+ } else {
124
+ messages . push ( 'Expected an error' ) ;
125
+ }
115
126
}
116
127
117
128
if ( warningExpected !== ! ! warns . length ) {
118
- throw new Error ( warns . length ? warns . join ( '\n' ) : 'Expected warning' ) ;
129
+ if ( warns . length ) {
130
+ messages . push ( ...warns ) ;
131
+ } else {
132
+ messages . push ( 'Expected a warning' ) ;
133
+ }
134
+ }
135
+
136
+ if ( ! errorExpected && exited ) {
137
+ messages . push ( 'Process exited' ) ;
138
+ }
139
+
140
+ if ( ! errorExpected && ! ! errorThrown && ! exited ) {
141
+ messages . push ( 'Error thrown: ' + errorThrown ) ;
142
+ }
143
+
144
+ if ( messages . length ) {
145
+ throw new Error ( messages . join ( '\n' ) ) ;
119
146
}
120
147
}
121
148
0 commit comments