2016-12-08 11:48:54 +01:00
|
|
|
import assert from 'assert';
|
|
|
|
import * as fs from 'fs';
|
2017-04-04 15:31:42 +02:00
|
|
|
import * as path from 'path';
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-04-04 15:31:42 +02:00
|
|
|
import { addLineNumbers, loadConfig, setupHtmlEqual, svelte, tryToLoadJson } from '../helpers.js';
|
2016-12-08 11:48:54 +01:00
|
|
|
|
|
|
|
function tryToReadFile ( file ) {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync( file, 'utf-8' );
|
|
|
|
} catch ( err ) {
|
|
|
|
if ( err.code !== 'ENOENT' ) throw err;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-04 16:21:19 +02:00
|
|
|
function capitalize ( str ) {
|
|
|
|
return str[0].toUpperCase() + str.slice( 1 );
|
|
|
|
}
|
|
|
|
|
2016-12-08 11:48:54 +01:00
|
|
|
describe( 'ssr', () => {
|
|
|
|
before( () => {
|
|
|
|
require( process.env.COVERAGE ?
|
2017-03-07 20:10:28 +01:00
|
|
|
'../../src/server-side-rendering/register.js' :
|
|
|
|
'../../ssr/register' );
|
2016-12-16 22:17:31 +01:00
|
|
|
|
|
|
|
return setupHtmlEqual();
|
2016-12-08 11:48:54 +01:00
|
|
|
});
|
|
|
|
|
2017-03-07 20:10:28 +01:00
|
|
|
fs.readdirSync( 'test/server-side-rendering/samples' ).forEach( dir => {
|
2016-12-08 11:48:54 +01:00
|
|
|
if ( dir[0] === '.' ) return;
|
|
|
|
|
2017-04-04 16:21:19 +02:00
|
|
|
// add .solo to a sample directory name to only run that test, or
|
|
|
|
// .show to always show the output. or both
|
|
|
|
const solo = /\.solo/.test( dir );
|
|
|
|
let show = /\.show/.test( dir );
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-01-04 20:07:05 +01:00
|
|
|
if ( solo && process.env.CI ) {
|
|
|
|
throw new Error( 'Forgot to remove `solo: true` from test' );
|
|
|
|
}
|
|
|
|
|
2016-12-08 11:48:54 +01:00
|
|
|
( solo ? it.only : it )( dir, () => {
|
2017-04-04 15:31:42 +02:00
|
|
|
dir = path.resolve( 'test/server-side-rendering/samples', dir );
|
|
|
|
const component = require( `${dir}/main.html` );
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-04-04 15:31:42 +02:00
|
|
|
const expectedHtml = tryToReadFile( `${dir}/_expected.html` );
|
|
|
|
const expectedCss = tryToReadFile( `${dir}/_expected.css` ) || '';
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-04-04 15:31:42 +02:00
|
|
|
const data = tryToLoadJson( `${dir}/data.json` );
|
|
|
|
let html;
|
|
|
|
let css;
|
2017-04-04 16:21:19 +02:00
|
|
|
let error;
|
2016-12-08 11:48:54 +01:00
|
|
|
|
2017-04-04 15:31:42 +02:00
|
|
|
try {
|
|
|
|
html = component.render( data );
|
|
|
|
css = component.renderCss().css;
|
2017-04-04 16:21:19 +02:00
|
|
|
} catch ( e ) {
|
|
|
|
show = true;
|
|
|
|
error = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( show ) {
|
2017-04-04 15:31:42 +02:00
|
|
|
fs.readdirSync( dir ).forEach( file => {
|
|
|
|
if ( file[0] === '_' ) return;
|
|
|
|
const source = fs.readFileSync( `${dir}/${file}`, 'utf-8' );
|
2017-04-04 16:21:19 +02:00
|
|
|
const name = capitalize( file.slice( 0, -path.extname( file ).length ) );
|
|
|
|
const { code } = svelte.compile( source, { generate: 'ssr', name });
|
2017-04-04 15:31:42 +02:00
|
|
|
console.group( file );
|
|
|
|
console.log( addLineNumbers( code ) );
|
|
|
|
console.groupEnd();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-04 16:21:19 +02:00
|
|
|
if ( error ) throw error;
|
|
|
|
|
2017-04-04 15:31:42 +02:00
|
|
|
fs.writeFileSync( `${dir}/_actual.html`, html );
|
|
|
|
if ( css ) fs.writeFileSync( `${dir}/_actual.css`, css );
|
2016-12-08 11:48:54 +01:00
|
|
|
|
|
|
|
assert.htmlEqual( html, expectedHtml );
|
|
|
|
assert.equal( css.replace( /^\s+/gm, '' ), expectedCss.replace( /^\s+/gm, '' ) );
|
|
|
|
});
|
|
|
|
});
|
2017-01-17 19:46:37 +01:00
|
|
|
|
|
|
|
// duplicate client-side tests, as far as possible
|
2017-04-04 18:05:54 +02:00
|
|
|
fs.readdirSync( 'test/runtime/samples' ).forEach( dir => {
|
2017-01-17 19:46:37 +01:00
|
|
|
if ( dir[0] === '.' ) return;
|
|
|
|
|
2017-04-04 18:05:54 +02:00
|
|
|
const config = loadConfig( `./runtime/samples/${dir}/_config.js` );
|
2017-01-17 19:46:37 +01:00
|
|
|
|
|
|
|
if ( config.solo && process.env.CI ) {
|
|
|
|
throw new Error( 'Forgot to remove `solo: true` from test' );
|
|
|
|
}
|
2017-01-31 23:35:03 +01:00
|
|
|
|
2017-01-24 18:04:20 +01:00
|
|
|
if ( config['skip-ssr'] ) return;
|
2017-01-17 19:46:37 +01:00
|
|
|
|
2017-01-24 18:04:20 +01:00
|
|
|
( config.skip ? it.skip : config.solo ? it.only : it )( dir, () => {
|
2017-01-31 23:35:03 +01:00
|
|
|
let compiled;
|
|
|
|
|
2017-01-17 19:46:37 +01:00
|
|
|
try {
|
2017-04-04 18:05:54 +02:00
|
|
|
const source = fs.readFileSync( `test/runtime/samples/${dir}/main.html`, 'utf-8' );
|
2017-01-31 23:35:03 +01:00
|
|
|
compiled = svelte.compile( source, { generate: 'ssr' });
|
2017-01-17 19:46:37 +01:00
|
|
|
} catch ( err ) {
|
|
|
|
if ( config.compileError ) {
|
|
|
|
config.compileError( err );
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 03:38:30 +02:00
|
|
|
const resolved = require.resolve( `../runtime/samples/${dir}/main.html` );
|
|
|
|
delete require.cache[ resolved ];
|
|
|
|
|
|
|
|
const component = require( resolved );
|
2017-01-31 23:35:03 +01:00
|
|
|
let html;
|
2017-01-17 19:46:37 +01:00
|
|
|
|
2017-01-31 23:35:03 +01:00
|
|
|
try {
|
|
|
|
html = component.render( config.data );
|
2017-01-17 19:46:37 +01:00
|
|
|
|
2017-01-31 23:35:03 +01:00
|
|
|
if ( config.html ) {
|
|
|
|
assert.htmlEqual( html, config.html );
|
|
|
|
}
|
|
|
|
} catch ( err ) {
|
2017-02-01 18:21:09 +01:00
|
|
|
console.log( addLineNumbers( compiled.code ) ); // eslint-disable-line no-console
|
2017-01-31 23:35:03 +01:00
|
|
|
throw err;
|
2017-01-17 19:46:37 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-12-08 11:48:54 +01:00
|
|
|
});
|