fix test and update server-side-rendering test

pull/4251/head
tanhauhau 4 years ago
parent ac9e977a92
commit 130aab9621

@ -40,9 +40,12 @@ describe('hydration', () => {
});
register.setCompile(svelte.compile);
register.setOutputFolderName('hydratable');
register.clearRequireCache();
register.clearCompileOutputCache();
const window = env();
try {
global.window = window;
const SvelteComponent = require(`${cwd}/main.svelte`).default;
@ -98,6 +101,11 @@ describe('hydration', () => {
component.$destroy();
assert.equal(target.innerHTML, '');
}
} catch (err) {
// saves the compiled output into file system
register.writeCompileOutputCacheToFile();
throw err;
}
});
}

@ -56,22 +56,33 @@ function registerExtension(extension: string) {
} = compile(fs.readFileSync(filename, 'utf-8'), options);
if (!process.env.CI) {
saveGeneratedOutput(code, filename);
saveGeneratedOutputToCache(code, filename);
}
return module._compile(code, filename);
};
}
function saveGeneratedOutput(code: string, filename: string) {
const outputCache = new Map();
function saveGeneratedOutputToCache(code: string, filename: string) {
filename = filename.split('\\').join('/');
filename = filename.replace(
/samples\/([^/]+)\/(.*)\.svelte$/,
`samples/$1/_output/${folderName}/$2.js`
);
outputCache.set(filename, code);
}
export function clearCompileOutputCache() {
outputCache.clear();
}
export function writeCompileOutputCacheToFile() {
for (const [filename, code] of outputCache) {
mkdirp(path.dirname(filename));
fs.writeFileSync(filename, code, 'utf8');
}
clearCompileOutputCache();
}
extensions.forEach(registerExtension);

@ -65,6 +65,7 @@ describe('runtime', () => {
accessors: 'accessors' in config ? config.accessors : true
};
register.clearCompileOutputCache();
register.clearRequireCache();
register.setCompile((config.preserveIdentifiers ? svelte : svelte$).compile);
register.setCompileOptions(compileOptions);
@ -191,11 +192,10 @@ describe('runtime', () => {
}
}).catch(err => {
failed.add(dir);
throw err;
})
.catch(err => {
// print a clickable link to open the directory
err.stack += `\n\ncmd-click: ${path.relative(process.cwd(), cwd)}/main.svelte`;
// saves the compiled output into file system
register.writeCompileOutputCacheToFile();
throw err;
})
.then(() => {

@ -8,7 +8,6 @@ import {
loadSvelte,
setupHtmlEqual,
tryToLoadJson,
cleanRequireCache,
shouldUpdateExpected
} from '../helpers';
import { set_current_component } from '../../internal';
@ -55,19 +54,18 @@ describe('ssr', () => {
(solo ? it.only : it)(dir, (done) => {
try {
dir = path.resolve(`${__dirname}/samples`, dir);
cleanRequireCache();
register.clearCompileOutputCache();
register.clearRequireCache();
const compileOptions = {
sveltePath,
...config.compileOptions,
generate: 'ssr',
format: 'cjs'
};
require('../../register')(compileOptions);
register.setCompileOptions(compileOptions);
register.setOutputFolderName('ssr');
const Component = require(`${dir}/main.svelte`).default;
@ -134,6 +132,7 @@ describe('ssr', () => {
done();
} catch (err) {
err.stack += `\n\ncmd-click: ${path.relative(process.cwd(), dir)}/main.svelte`;
register.writeCompileOutputCacheToFile();
done(err);
} finally {
set_current_component(null);
@ -143,7 +142,7 @@ describe('ssr', () => {
// duplicate client-side tests, as far as possible
runRuntimeSamples('runtime');
runRuntimeSamples('runtime-puppeteer');
// runRuntimeSamples('runtime-puppeteer');
function runRuntimeSamples(suite) {
fs.readdirSync(`test/${suite}/samples`).forEach(dir => {
@ -162,6 +161,10 @@ describe('ssr', () => {
const cwd = path.resolve(`test/${suite}/samples`, dir);
delete global.window;
return Promise.resolve()
.then(() => {
register.clearCompileOutputCache();
register.clearRequireCache();
register.setCompileOptions({
sveltePath,
@ -169,10 +172,8 @@ describe('ssr', () => {
generate: 'ssr',
format: 'cjs'
});
register.setOutputFolderName('ssr');
try {
if (config.before_test) config.before_test();
const Component = require(`../${suite}/samples/${dir}/main.svelte`).default;
@ -191,9 +192,8 @@ describe('ssr', () => {
}
if (config.after_test) config.after_test();
} catch (err) {
err.stack += `\n\ncmd-click: ${path.relative(process.cwd(), cwd)}/main.svelte`;
set_current_component(null);
}).catch(err => {
if (config.error) {
if (typeof config.error === 'function') {
config.error(assert, err);
@ -203,9 +203,12 @@ describe('ssr', () => {
} else {
throw err;
}
} finally {
}).catch(err => {
err.stack += `\n\ncmd-click: ${path.relative(process.cwd(), cwd)}/main.svelte`;
register.writeCompileOutputCacheToFile();
set_current_component(null);
}
throw err;
});
});
});
}

Loading…
Cancel
Save