diff --git a/packages/kit/src/api/dev/index.js b/packages/kit/src/api/dev/index.js index e92fcbb37d44..957772c10172 100644 --- a/packages/kit/src/api/dev/index.js +++ b/packages/kit/src/api/dev/index.js @@ -340,7 +340,7 @@ class Watcher extends EventEmitter { this.server.close(); this.cheapwatch.close(); - this.snowpack.shutdown(); + return this.snowpack.shutdown(); } } diff --git a/test/apps/amp/test/index.js b/test/apps/amp/test/index.js index 6e175f3c9eca..61ffde938cf6 100644 --- a/test/apps/amp/test/index.js +++ b/test/apps/amp/test/index.js @@ -12,7 +12,6 @@ runner( const tests = []; for (const file of modules) { - console.log(file); const mod = await import(`${cwd}/${file}`); tests.push(mod.default); } diff --git a/test/apps/basics/package.json b/test/apps/basics/package.json index 662638310b3f..c79b1408fd05 100644 --- a/test/apps/basics/package.json +++ b/test/apps/basics/package.json @@ -1,12 +1,13 @@ { "name": "test-basics", "private": true, + "type": "module", "version": "0.0.2", "scripts": { "dev": "svelte-kit dev", "build": "svelte-kit build", "start": "svelte-kit start", - "test": "uvu test" + "test": "node test" }, "devDependencies": { "@sveltejs/adapter-node": "workspace:*", diff --git a/test/apps/basics/snowpack.config.js b/test/apps/basics/snowpack.config.cjs similarity index 100% rename from test/apps/basics/snowpack.config.js rename to test/apps/basics/snowpack.config.cjs diff --git a/test/apps/basics/svelte.config.js b/test/apps/basics/svelte.config.cjs similarity index 100% rename from test/apps/basics/svelte.config.js rename to test/apps/basics/svelte.config.cjs diff --git a/test/apps/basics/test/index.js b/test/apps/basics/test/index.js index 5ada19d992be..6cdc843a255b 100644 --- a/test/apps/basics/test/index.js +++ b/test/apps/basics/test/index.js @@ -1,20 +1,32 @@ import path from 'path'; import glob from 'tiny-glob/sync.js'; import * as assert from 'uvu/assert'; -import { runner } from '../../../runner'; // TODO make this a package? +import { fileURLToPath } from 'url'; +import { runner } from '../../../runner.js'; // TODO make this a package? -runner((test, is_dev) => { - const cwd = path.join(__dirname, '../src/routes'); - const modules = glob('**/__tests__.js', { cwd }); - for (const module of modules) { - require(`../src/routes/${module}`).default(test, is_dev); - } +runner( + async () => { + const __filename = fileURLToPath(import.meta.url); + const cwd = path.join(__filename, '../../src/routes'); + const modules = glob('**/__tests__.js', { cwd }); + + const tests = []; + + for (const file of modules) { + const mod = await import(`${cwd}/${file}`); + tests.push(mod.default); + } - test('static files', async ({ fetch }) => { - let res = await fetch('/static.json'); - assert.equal(await res.json(), 'static file'); + tests.push(suite => { + suite('static files', async ({ fetch }) => { + let res = await fetch('/static.json'); + assert.equal(await res.json(), 'static file'); - res = await fetch('/subdirectory/static.json'); - assert.equal(await res.json(), 'subdirectory file'); - }); -}); + res = await fetch('/subdirectory/static.json'); + assert.equal(await res.json(), 'subdirectory file'); + }); + }); + + return tests; + } +); diff --git a/test/apps/options/package.json b/test/apps/options/package.json index 53f1bb9152c3..e558ae14ba2a 100644 --- a/test/apps/options/package.json +++ b/test/apps/options/package.json @@ -1,12 +1,13 @@ { "name": "test-options", "private": true, + "type": "module", "version": "0.0.1", "scripts": { "dev": "svelte-kit dev", "build": "svelte-kit build", "start": "svelte-kit start", - "test": "uvu" + "test": "node test" }, "devDependencies": { "@sveltejs/adapter-node": "workspace:*", diff --git a/test/apps/options/snowpack.config.js b/test/apps/options/snowpack.config.cjs similarity index 100% rename from test/apps/options/snowpack.config.js rename to test/apps/options/snowpack.config.cjs diff --git a/test/apps/options/svelte.config.js b/test/apps/options/svelte.config.cjs similarity index 100% rename from test/apps/options/svelte.config.js rename to test/apps/options/svelte.config.cjs diff --git a/test/apps/options/test/index.js b/test/apps/options/test/index.js index 0a2bbd0b7880..23a62fedd72c 100644 --- a/test/apps/options/test/index.js +++ b/test/apps/options/test/index.js @@ -1,24 +1,36 @@ import path from 'path'; import glob from 'tiny-glob/sync.js'; import * as assert from 'uvu/assert'; -import { runner } from '../../../runner'; // TODO make this a package? +import { fileURLToPath } from 'url'; +import { runner } from '../../../runner.js'; // TODO make this a package? -runner((test, is_dev) => { - const cwd = path.join(__dirname, '../source/pages'); - const modules = glob('**/__tests__.js', { cwd }); - for (const module of modules) { - require(`../source/pages/${module}`).default(test, is_dev); - } +runner( + async () => { + const __filename = fileURLToPath(import.meta.url); + const cwd = path.join(__filename, '../../source/pages'); + const modules = glob('**/__tests__.js', { cwd }); + + const tests = []; + + for (const file of modules) { + const mod = await import(`${cwd}/${file}`); + tests.push(mod.default); + } - test('serves /', async ({ visit, contains, js }) => { - await visit('/'); - assert.ok(await contains('I am in the template'), 'Should show custom template contents'); - assert.ok(await contains("We're on index.svelte"), 'Should show page contents'); - assert.ok( - await contains( - `Hello from the ${js ? 'client' : 'server'} in ${is_dev ? 'dev' : 'prod'} mode!` - ), - 'Should run JavaScript' - ); - }); -}); + tests.push((suite, is_dev) => { + suite('serves /', async ({ visit, contains, js }) => { + await visit('/'); + assert.ok(await contains('I am in the template'), 'Should show custom template contents'); + assert.ok(await contains("We're on index.svelte"), 'Should show page contents'); + assert.ok( + await contains( + `Hello from the ${js ? 'client' : 'server'} in ${is_dev ? 'dev' : 'prod'} mode!` + ), + 'Should run JavaScript' + ); + }); + }); + + return tests; + } +); diff --git a/test/runner.js b/test/runner.js index 31c0bcae0609..1b103c9e49ef 100644 --- a/test/runner.js +++ b/test/runner.js @@ -77,17 +77,23 @@ async function setup({ port }) { pathname: () => page.url().replace(base, ''), keyboard: page.keyboard, sleep: (ms) => new Promise((f) => setTimeout(f, ms)), + reset: () => browser && browser.close(), $: (selector) => page.$(selector) }; } -globalThis.UVU_DEFER = 1; - export async function runner(prepare_tests, options = {}) { + globalThis.UVU_DEFER = 1; const uvu = await import('uvu'); async function run(is_dev, { before, after }) { - const suite = uvu.suite(is_dev ? 'dev' : 'build'); + const name = is_dev ? 'dev' : 'build'; + + // manually replicate uvu global state + const count = globalThis.UVU_QUEUE.push([name]); + globalThis.UVU_INDEX = count - 1; + + const suite = uvu.suite(name); const tests = await prepare_tests(); suite.before(before); @@ -144,7 +150,7 @@ export async function runner(prepare_tests, options = {}) { }, async after(context) { await context.watcher.close(); - if (context.browser) await context.browser.close(); + await context.reset(); } }); @@ -167,7 +173,10 @@ export async function runner(prepare_tests, options = {}) { }, async after(context) { context.server.close(); - if (context.browser) await context.browser.close(); + await context.reset(); } }); + + await uvu.exec(); + process.exit(process.exitCode || 0); }