Skip to content

Commit

Permalink
Typecheck tests (#1139)
Browse files Browse the repository at this point in the history
* start typechecking tests dir

* aliases

* add some types

* fix some stuff

* fix a bunch of types

* fix types

* Update packages/kit/test/apps/basics/src/routes/store/__tests__.js

Co-authored-by: Ignatius Bagus <ignatius.mbs@gmail.com>

* undefined -> void

Co-authored-by: Ignatius Bagus <ignatius.mbs@gmail.com>
  • Loading branch information
Rich Harris and ignatiusmb authored Apr 20, 2021
1 parent cac3f3e commit 41ed727
Show file tree
Hide file tree
Showing 77 changed files with 257 additions and 139 deletions.
5 changes: 5 additions & 0 deletions packages/kit/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"extends": "@sveltejs",
"parserOptions": {
"sourceType": "module"
},
"globals": {
"goto": true,
"prefetch": true,
"prefetchRoutes": true
}
}
4 changes: 2 additions & 2 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@rollup/plugin-replace": "^2.4.1",
"@sveltejs/kit": "workspace:*",
"@types/amphtml-validator": "^1.0.1",
"@types/cookie": "^0.4.0",
"@types/mime": "^2.0.3",
"@types/node": "^14.14.33",
"@types/rimraf": "^3.0.0",
Expand Down Expand Up @@ -50,8 +51,7 @@
"dev": "rm -rf assets/runtime && rollup -cw",
"build": "rm -rf assets/runtime && rollup -c",
"lint": "eslint --ignore-path .gitignore \"src/**/*.{ts,mjs,js,svelte}\" && npm run check-format",
"check": "npx tsc -p tsconfig.enforced.json",
"check-all": "npx tsc -p tsconfig.json",
"check": "npx tsc",
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"prepublishOnly": "npm run build",
Expand Down
19 changes: 19 additions & 0 deletions packages/kit/test/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
declare global {
interface Window {
navigated: Promise<void>;
started: boolean;
}

const goto: (
href: string,
opts?: {
replaceState?: boolean;
noScroll?: boolean;
}
) => Promise<void>;

const prefetch: (url: string) => Promise<void>;
const prefetchRoutes: (urls?: string[]) => Promise<void>;
}

export {};
2 changes: 1 addition & 1 deletion packages/kit/test/apps/amp/src/routes/host/__tests__.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
// this test lives in the AMP app because `basics` has `hostHeader`
// configured, and `options` has `host` configured
Expand Down
1 change: 1 addition & 0 deletions packages/kit/test/apps/amp/src/routes/host/index.json.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get({ host }) {
return {
body: { host }
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/amp/src/routes/invalid/__tests__.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
test('prints validation errors', '/invalid', async ({ page }) => {
const body = await page.innerHTML('body');
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/amp/src/routes/valid/__tests__.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
test('amp is true', '/valid', async ({ page }) => {
assert.equal(
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/test/apps/basics/src/hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cookie from 'cookie';

/** @type {import('../../../../types').GetContext} */
/** @type {import('@sveltejs/kit').GetContext} */
export function getContext(request) {
const cookies = cookie.parse(request.headers.cookie || '');

Expand All @@ -10,12 +10,12 @@ export function getContext(request) {
};
}

/** @type {import('../../../../types').GetSession} */
/** @type {import('@sveltejs/kit').GetSession} */
export function getSession({ context }) {
return context;
}

/** @type {import('../../../../types').Handle} */
/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ request, render }) {
const response = await render(request);

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/src/routes/$error.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
/** @type {import('../../../../../types').ErrorLoad} */
/** @type {import('@sveltejs/kit').ErrorLoad} */
export function load({ status, error }) {
return {
props: { status, error }
Expand Down
1 change: 1 addition & 0 deletions packages/kit/test/apps/basics/src/routes/$layout.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script context="module">
/** @type {import('@sveltejs/kit').Load} */
export async function load() {
return {
props: {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/src/routes/__tests__.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
test('fetch in root index.svelte works', '/', async ({ page }) => {
assert.equal(await page.textContent('h1'), 'the answer is 42');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
// TODO unskip this
test.skip('resets focus', '/accessibility/a', async ({ page, clicknav }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
test('handles static asset imports', '/asset-import', async ({ base, page, js }) => {
const sources = await page.evaluate(() =>
Expand Down
20 changes: 12 additions & 8 deletions packages/kit/test/apps/basics/src/routes/caching/__tests__.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
test('caches pages', async ({ fetch }) => {
test('caches pages', null, async ({ fetch }) => {
const { headers } = await fetch('/caching');

assert.equal(headers.get('cache-control'), 'public, max-age=30');
});

test('sets cache-control: private if page uses session', async ({ fetch }) => {
test('sets cache-control: private if page uses session', null, async ({ fetch }) => {
const { headers } = await fetch('/caching/private/uses-session');

assert.equal(headers.get('cache-control'), 'private, max-age=30');
});

test('sets cache-control: private if page uses fetch', async ({ fetch }) => {
test('sets cache-control: private if page uses fetch', null, async ({ fetch }) => {
const { headers } = await fetch('/caching/private/uses-fetch?credentials=include');

assert.equal(headers.get('cache-control'), 'private, max-age=30');
});

test('sets cache-control: public if page uses fetch without credentials', async ({ fetch }) => {
const { headers } = await fetch('/caching/private/uses-fetch?credentials=omit');
test(
'sets cache-control: public if page uses fetch without credentials',
null,
async ({ fetch }) => {
const { headers } = await fetch('/caching/private/uses-fetch?credentials=omit');

assert.equal(headers.get('cache-control'), 'public, max-age=30');
});
assert.equal(headers.get('cache-control'), 'public, max-age=30');
}
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
test('sets Content-Type', async ({ fetch }) => {
test('sets Content-Type', null, async ({ fetch }) => {
const { headers } = await fetch('/content-type-header');

assert.equal(headers.get('content-type'), 'text/html');
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/src/routes/css/__tests__.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
test('applies imported styles', '/css', async ({ page }) => {
assert.equal(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export function del(req, res) {
/** @type {import('@sveltejs/kit').RequestHandler} */
export function del(req) {
return {
status: 200,
body: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
test('calls a delete handler', '/delete-route', async ({ page, js }) => {
if (js) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
if (is_dev) {
// TODO unskip this test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
throw new Error('nope');
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
test(
'renders the closest error page',
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/test/apps/basics/src/routes/host/__tests__.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
test('can access host through page store', async ({ base, page }) => {
test('can access host through page store', null, async ({ base, page }) => {
page.setExtraHTTPHeaders({
'x-forwarded-host': 'forwarded.com'
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
test('imports from node_modules', '/imports', async ({ page, clicknav }) => {
await clicknav('[href="/imports/markdown"]');
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
/** @type {import('../../../../../types').Load}*/
/** @type {import('@sveltejs/kit').Load}*/
export async function load({ fetch }) {
const res = await fetch('/answer.json');
const { answer } = await res.json();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get({ params }) {
return {
body: {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/basics/src/routes/load/__tests__.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import http from 'http';
import * as ports from 'port-authority';
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
test('loads', '/load', async ({ page }) => {
assert.equal(await page.textContent('h1'), 'bar == bar?');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get(request) {
return {
body: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
/** @type {import('../../../../../../types').Load} */
/** @type {import('@sveltejs/kit').Load} */
export async function load({ fetch }) {
const res = await fetch('/load/fetch-credentialed.json');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script context="module">
import { browser } from '$app/env';
/** @type {import('../../../../../../types').Load} */
/** @type {import('@sveltejs/kit').Load} */
export async function load({ fetch }) {
const url = '/load/fetch-request.json';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
/** @type {import('../../../../../../types').Load} */
/** @type {import('@sveltejs/kit').Load} */
export async function load({ page, fetch }) {
const res = await fetch(`http://localhost:${page.query.get('port')}/large-response.json`)
const text = await res.text();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('../../../../../../types').RequestHandler} */
/** @type {import('@sveltejs/kit').RequestHandler} */
export function post(request) {
return {
body: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
/** @type {import('../../../../../../types').Load} */
/** @type {import('@sveltejs/kit').Load} */
export async function load({ fetch }) {
const res = await fetch('/load/raw-body.json', {
method: 'POST',
Expand Down
18 changes: 0 additions & 18 deletions packages/kit/test/apps/basics/src/routes/middleware/__tests__.js

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions packages/kit/test/apps/basics/src/routes/middleware/index_.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
test('renders a nested layout', '/nested-layout', async ({ page }) => {
assert.equal(await page.textContent('footer'), 'Custom layout');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test) {
test('resets layout', '/nested-layout/reset', async ({ page }) => {
assert.ok(await page.evaluate(() => !document.querySelector('footer')));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
test('does not hydrate page with hydrate=false', '/no-hydrate', async ({ page, js }) => {
await page.click('button');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script context="module">
export const hydrate = false;
/** @type {import('../../../../../../types').Load} */
/** @type {import('@sveltejs/kit').Load} */
export async function load({ fetch }) {
const res = await fetch('/no-hydrate.json');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
test('disables router if router=false', '/no-router/a', async ({ page, clicknav, js }) => {
if (js) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'uvu/assert';

/** @type {import('../../../../../types').TestMaker} */
/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
test('does not SSR page with ssr=false', '/no-ssr', async ({ page, js }) => {
if (js) {
Expand Down
Loading

1 comment on commit 41ed727

@vercel
Copy link

@vercel vercel bot commented on 41ed727 Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.