import assert from "assert"; import { svelte, deindent, env, setupHtmlEqual } from "../helpers.js"; function testAmd(code, expectedId, dependencies, html) { const fn = new Function("define", code); const window = env(); function define(id, deps, factory) { assert.equal(id, expectedId); assert.deepEqual(deps, Object.keys(dependencies)); const SvelteComponent = factory( ...Object.keys(dependencies).map(key => dependencies[key]) ); const main = window.document.body.querySelector("main"); const component = new SvelteComponent({ target: main }); assert.htmlEqual(main.innerHTML, html); component.destroy(); } define.amd = true; fn(define); } function testCjs(code, dependencyById, html) { const fn = new Function("module", "exports", "require", code); const window = env(); const module = { exports: {} }; const require = id => { return dependencyById[id]; }; fn(module, module.exports, require); const SvelteComponent = module.exports; const main = window.document.body.querySelector("main"); const component = new SvelteComponent({ target: main }); assert.htmlEqual(main.innerHTML, html); component.destroy(); } function testIife(code, name, globals, html) { const fn = new Function(Object.keys(globals), `${code}\n\nreturn ${name};`); const window = env(); const SvelteComponent = fn( ...Object.keys(globals).map(key => globals[key]) ); const main = window.document.body.querySelector("main"); const component = new SvelteComponent({ target: main }); assert.htmlEqual(main.innerHTML, html); component.destroy(); } function testEval(code, name, globals, html) { const fn = new Function(Object.keys(globals), `return ${code};`); const window = env(); const SvelteComponent = fn( ...Object.keys(globals).map(key => globals[key]) ); const main = window.document.body.querySelector("main"); const component = new SvelteComponent({ target: main }); assert.htmlEqual(main.innerHTML, html); component.destroy(); } describe("formats", () => { before(setupHtmlEqual); describe("amd", () => { it("generates an AMD module", () => { const source = deindent`