From 7eb4f2d1c0f34a3d8617434f3683105a08688a53 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 25 Jun 2017 17:58:49 -0400 Subject: [PATCH] add dev mode hydration error (closes #664) --- src/generators/dom/index.ts | 1 + test/runtime/index.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 17c77b14d2..8921c6cabe 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -247,6 +247,7 @@ export default function dom( nodes.forEach( ${generator.helper('detachNode')} ); ` : deindent` + ${options.dev && `if ( options.hydrate ) throw new Error( 'options.hydrate only works if the component was compiled with the \`hydratable: true\` option' );`} this._fragment.create(); `} this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}( options.target, null ); diff --git a/test/runtime/index.js b/test/runtime/index.js index 6c599ce96d..16d5820f0b 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -230,4 +230,23 @@ describe("runtime", () => { new SvelteComponent(); }, /'target' is a required option/); }); + + it("fails if options.hydrate is true but the component is non-hydratable", () => { + const { code } = svelte.compile(`
`, { + format: "iife", + name: "SvelteComponent", + dev: true + }); + + const SvelteComponent = eval( + `(function () { ${code}; return SvelteComponent; }())` + ); + + assert.throws(() => { + new SvelteComponent({ + target: {}, + hydrate: true + }); + }, /options.hydrate only works if the component was compiled with the `hydratable: true` option/); + }); });