mirror of https://github.com/sveltejs/svelte
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.7 KiB
94 lines
2.7 KiB
8 years ago
|
import assert from 'assert';
|
||
|
import * as fs from 'fs';
|
||
|
|
||
8 years ago
|
import { addLineNumbers, exists, loadConfig, setupHtmlEqual, svelte, tryToLoadJson } from '../helpers.js';
|
||
8 years ago
|
|
||
|
function tryToReadFile ( file ) {
|
||
|
try {
|
||
|
return fs.readFileSync( file, 'utf-8' );
|
||
|
} catch ( err ) {
|
||
|
if ( err.code !== 'ENOENT' ) throw err;
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
describe( 'ssr', () => {
|
||
|
before( () => {
|
||
|
require( process.env.COVERAGE ?
|
||
8 years ago
|
'../../src/server-side-rendering/register.js' :
|
||
|
'../../ssr/register' );
|
||
8 years ago
|
|
||
|
return setupHtmlEqual();
|
||
8 years ago
|
});
|
||
|
|
||
8 years ago
|
fs.readdirSync( 'test/server-side-rendering/samples' ).forEach( dir => {
|
||
8 years ago
|
if ( dir[0] === '.' ) return;
|
||
|
|
||
8 years ago
|
const solo = exists( `test/server-side-rendering/samples/${dir}/solo` );
|
||
8 years ago
|
|
||
8 years ago
|
if ( solo && process.env.CI ) {
|
||
|
throw new Error( 'Forgot to remove `solo: true` from test' );
|
||
|
}
|
||
|
|
||
8 years ago
|
( solo ? it.only : it )( dir, () => {
|
||
8 years ago
|
const component = require( `./samples/${dir}/main.html` );
|
||
8 years ago
|
|
||
8 years ago
|
const expectedHtml = tryToReadFile( `test/server-side-rendering/samples/${dir}/_expected.html` );
|
||
|
const expectedCss = tryToReadFile( `test/server-side-rendering/samples/${dir}/_expected.css` ) || '';
|
||
8 years ago
|
|
||
8 years ago
|
const data = tryToLoadJson( `test/server-side-rendering/samples/${dir}/data.json` );
|
||
8 years ago
|
const html = component.render( data );
|
||
|
const { css } = component.renderCss();
|
||
|
|
||
8 years ago
|
fs.writeFileSync( `test/server-side-rendering/samples/${dir}/_actual.html`, html );
|
||
|
if ( css ) fs.writeFileSync( `test/server-side-rendering/samples/${dir}/_actual.css`, css );
|
||
8 years ago
|
|
||
|
assert.htmlEqual( html, expectedHtml );
|
||
|
assert.equal( css.replace( /^\s+/gm, '' ), expectedCss.replace( /^\s+/gm, '' ) );
|
||
|
});
|
||
|
});
|
||
8 years ago
|
|
||
|
// duplicate client-side tests, as far as possible
|
||
8 years ago
|
fs.readdirSync( 'test/generator/samples' ).forEach( dir => {
|
||
8 years ago
|
if ( dir[0] === '.' ) return;
|
||
|
|
||
8 years ago
|
const config = loadConfig( `./generator/samples/${dir}/_config.js` );
|
||
8 years ago
|
|
||
|
if ( config.solo && process.env.CI ) {
|
||
|
throw new Error( 'Forgot to remove `solo: true` from test' );
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if ( config['skip-ssr'] ) return;
|
||
8 years ago
|
|
||
8 years ago
|
( config.skip ? it.skip : config.solo ? it.only : it )( dir, () => {
|
||
8 years ago
|
let compiled;
|
||
|
|
||
8 years ago
|
try {
|
||
8 years ago
|
const source = fs.readFileSync( `test/generator/samples/${dir}/main.html`, 'utf-8' );
|
||
8 years ago
|
compiled = svelte.compile( source, { generate: 'ssr' });
|
||
8 years ago
|
} catch ( err ) {
|
||
|
if ( config.compileError ) {
|
||
|
config.compileError( err );
|
||
|
return;
|
||
|
} else {
|
||
|
throw err;
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
const component = require( `../generator/samples/${dir}/main.html` );
|
||
8 years ago
|
let html;
|
||
8 years ago
|
|
||
8 years ago
|
try {
|
||
|
html = component.render( config.data );
|
||
8 years ago
|
|
||
8 years ago
|
if ( config.html ) {
|
||
|
assert.htmlEqual( html, config.html );
|
||
|
}
|
||
|
} catch ( err ) {
|
||
8 years ago
|
console.log( addLineNumbers( compiled.code ) ); // eslint-disable-line no-console
|
||
8 years ago
|
throw err;
|
||
8 years ago
|
}
|
||
|
});
|
||
|
});
|
||
8 years ago
|
});
|