@ -7,23 +7,19 @@
import * as fs from 'node:fs' ;
import * as fs from 'node:fs' ;
import { assert } from 'vitest' ;
import { assert } from 'vitest' ;
import { render , renderAsync } from 'svelte/server' ;
import { render , renderAsync } from 'svelte/server' ;
import {
import { compile_directory , should_update_expected , try_read_file } from '../helpers.js' ;
async_mode ,
compile_directory ,
should_update_expected ,
try_read_file
} from '../helpers.js' ;
import { assert_html_equal_with_options } from '../html_equal.js' ;
import { assert_html_equal_with_options } from '../html_equal.js' ;
import { suite , type BaseTest } from '../suite.js' ;
import { suite_with_variants , type BaseTest } from '../suite.js' ;
import type { CompileOptions } from '#compiler' ;
import type { CompileOptions } from '#compiler' ;
interface SSRTest extends BaseTest {
interface SSRTest extends BaseTest {
mode ? : ( 'sync' | 'async' ) [ ] ;
compileOptions? : Partial < CompileOptions > ;
compileOptions? : Partial < CompileOptions > ;
load_compiled? : boolean ;
load_compiled? : boolean ;
props? : Record < string , any > ;
props? : Record < string , any > ;
id_prefix? : string ;
id_prefix? : string ;
withoutNormalizeHtml? : boolean ;
withoutNormalizeHtml? : boolean ;
error s ?: string [ ] ;
error ?: string ;
}
}
// TODO remove this shim when we can
// TODO remove this shim when we can
@ -40,13 +36,23 @@ Promise.withResolvers = () => {
return { promise , resolve , reject } ;
return { promise , resolve , reject } ;
} ;
} ;
// eslint-disable-next-line no-console
const { test , run } = suite_with_variants < SSRTest , ' sync ' | ' async ' , CompileOptions > (
let console_error = console . error ;
[ 'sync' , 'async' ] ,
( variant , config , test_name ) = > {
if ( config . mode && ! config . mode . includes ( variant ) ) {
return 'no-test' ;
}
if ( test_name . startsWith ( 'async' ) && variant === 'sync' ) {
return 'no-test' ;
}
const { test , run } = suite < SSRTest > ( async ( config , test_dir ) = > {
return false ;
} ,
async ( config , test_dir ) = > {
const compile_options = {
const compile_options = {
experimental : {
experimental : {
async : async_mode ,
async : tru e,
. . . config . compileOptions ? . experimental
. . . config . compileOptions ? . experimental
} ,
} ,
. . . config . compileOptions
. . . config . compileOptions
@ -56,15 +62,15 @@ const { test, run } = suite<SSRTest>(async (config, test_dir) => {
await compile_directory ( test_dir , 'server' , compile_options ) ;
await compile_directory ( test_dir , 'server' , compile_options ) ;
}
}
const errors : string [ ] = [ ] ;
return compile_options ;
} ,
console . error = ( . . . args ) = > {
async ( config , test_dir , variant , compile_options ) = > {
errors . push ( . . . args ) ;
} ;
const Component = ( await import ( ` ${ test_dir } /_output/server/main.svelte.js ` ) ) . default ;
const Component = ( await import ( ` ${ test_dir } /_output/server/main.svelte.js ` ) ) . default ;
const expected_html = try_read_file ( ` ${ test_dir } /_expected.html ` ) ;
const expected_html = try_read_file ( ` ${ test_dir } /_expected.html ` ) ;
const rendered = async_mode
let rendered ;
try {
rendered =
variant === 'async'
? await renderAsync ( Component , {
? await renderAsync ( Component , {
props : config.props || { } ,
props : config.props || { } ,
idPrefix : config.id_prefix
idPrefix : config.id_prefix
@ -73,12 +79,22 @@ const { test, run } = suite<SSRTest>(async (config, test_dir) => {
props : config.props || { } ,
props : config.props || { } ,
idPrefix : config.id_prefix
idPrefix : config.id_prefix
} ) ;
} ) ;
} catch ( error ) {
if ( config . error ) {
assert . deepEqual ( ( error as Error ) . message , config . error ) ;
return ;
} else {
throw error ;
}
}
const { body , head } = rendered ;
const { body , head } = rendered ;
fs . writeFileSync ( ` ${ test_dir } /_output/rendered.html ` , body ) ;
const prefix = variant === 'async' ? 'async_' : '' ;
fs . writeFileSync ( ` ${ test_dir } /_output/ ${ prefix } rendered.html ` , body ) ;
if ( head ) {
if ( head ) {
fs . writeFileSync ( ` ${ test_dir } /_output/rendered_head.html ` , head ) ;
fs . writeFileSync ( ` ${ test_dir } /_output/ ${ prefix } rendered_head.html` , head ) ;
}
}
try {
try {
@ -113,13 +129,8 @@ const { test, run } = suite<SSRTest>(async (config, test_dir) => {
}
}
}
}
}
}
if ( errors . length > 0 ) {
assert . deepEqual ( config . errors , errors ) ;
}
}
) ;
console . error = console_error ;
} ) ;
export { test } ;
export { test } ;