File tree 4 files changed +37
-0
lines changed 4 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ export class ModuleNode {
33
33
transformResult : TransformResult | null = null
34
34
ssrTransformResult : TransformResult | null = null
35
35
ssrModule : Record < string , any > | null = null
36
+ ssrError : Error | null = null
36
37
lastHMRTimestamp = 0
37
38
lastInvalidationTimestamp = 0
38
39
Original file line number Diff line number Diff line change
1
+ export const bad = 1
2
+ throw new Error ( 'it is an expected error' )
Original file line number Diff line number Diff line change
1
+ import { resolve } from 'path'
2
+ import { createServer } from '../../index'
3
+
4
+ const badjs = resolve ( __dirname , './fixtures/ssrModuleLoader-bad.js' )
5
+ const THROW_MESSAGE = 'it is an expected error'
6
+
7
+ test ( 'always throw error when evaluating an wrong SSR module' , async ( ) => {
8
+ const viteServer = await createServer ( )
9
+ const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } )
10
+ const expectedErrors = [ ]
11
+ for ( const i of [ 0 , 1 ] ) {
12
+ try {
13
+ await viteServer . ssrLoadModule ( badjs )
14
+ } catch ( e ) {
15
+ expectedErrors . push ( e )
16
+ }
17
+ }
18
+ await viteServer . close ( )
19
+ expect ( expectedErrors ) . toHaveLength ( 2 )
20
+ expect ( expectedErrors [ 0 ] ) . toBe ( expectedErrors [ 1 ] )
21
+ expectedErrors . forEach ( ( error ) => {
22
+ expect ( error ?. message ) . toContain ( THROW_MESSAGE )
23
+ } )
24
+ expect ( spy ) . toBeCalledTimes ( 1 )
25
+ const [ firstParameter ] = spy . mock . calls [ 0 ]
26
+ expect ( firstParameter ) . toContain ( 'Error when evaluating SSR module' )
27
+ expect ( firstParameter ) . toContain ( THROW_MESSAGE )
28
+ spy . mockClear ( )
29
+ } )
Original file line number Diff line number Diff line change @@ -77,6 +77,10 @@ async function instantiateModule(
77
77
const { moduleGraph } = server
78
78
const mod = await moduleGraph . ensureEntryFromUrl ( url , true )
79
79
80
+ if ( mod . ssrError ) {
81
+ throw mod . ssrError
82
+ }
83
+
80
84
if ( mod . ssrModule ) {
81
85
return mod . ssrModule
82
86
}
@@ -202,6 +206,7 @@ async function instantiateModule(
202
206
ssrExportAll
203
207
)
204
208
} catch ( e ) {
209
+ mod . ssrError = e
205
210
if ( e . stack && fixStacktrace !== false ) {
206
211
const stacktrace = ssrRewriteStacktrace ( e . stack , moduleGraph )
207
212
rebindErrorStacktrace ( e , stacktrace )
You can’t perform that action at this time.
0 commit comments