Skip to content

initial types testing #4124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@
"format": "npm run check-format -- --write",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"prepublishOnly": "npm run build",
"test": "npm run test:unit && npm run test:packaging && npm run test:prerendering && npm run test:integration",
"test": "npm run test:unit && npm run test:typings && npm run test:packaging && npm run test:prerendering && npm run test:integration",
"test:unit": "uvu src \"(spec\\.js|test[\\\\/]index\\.js)\" -i packaging",
"test:typings": "tsc --project test/typings",
"test:prerendering": "pnpm test:prerendering:basics && pnpm test:prerendering:options",
"test:prerendering:basics": "cd test/prerendering/basics && pnpm test",
"test:prerendering:options": "cd test/prerendering/options && pnpm test",
Expand Down
128 changes: 128 additions & 0 deletions packages/kit/test/typings/endpoint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { RequestHandler } from '@sveltejs/kit';

const valid_body = {
str: 'string',
num: 12345,
bool: true,
null: null,
custom: {
toJSON: () => 'custom toJSON function'
},
list: ['string', 12345, false, null],
nested: {
another: 'string',
big_num: 98765,
binary: false,
nullish: null,
custom: {
toJSON: () => 'hi mom'
},
list: [],
deeply: {
nested: {}
}
}
};

// valid - basic case of returning an object
export const base_case: RequestHandler = () => {
return {
body: valid_body
};
};

// valid - raw Response instance should be allowed
export const response_instance: RequestHandler = () => {
return new Response();
};

// valid - body instance of Uint8Array should be allowed
export const uint8array_body: RequestHandler = () => {
return {
body: new Uint8Array()
};
};

// valid - body instance of ReadableStream should be allowed
export const readable_stream_body: RequestHandler = () => {
return {
body: new ReadableStream()
};
};

// valid - body instance of stream.Readable should be allowed
export const stream_readable_body: RequestHandler = async () => {
const { Readable } = await import('stream');
return {
body: new Readable()
};
};

// valid - different header pairs should be allowed
export const differential_headers_assignment: RequestHandler = () => {
if (Math.random() < 0.5) {
return { headers: { foo: 'bar' } };
} else {
return { headers: { baz: 'foo' } };
}
};

// TODO https://github.com/sveltejs/kit/issues/1997
// interface ExamplePost {
// title: string;
// description: string;
// published_date?: string;
// author_name?: string;
// author_link?: string;
// }
// // valid - should not be any different
// export const generic_case: RequestHandler<Record<string, string>, ExamplePost> = () => {
// return {
// body: {} as ExamplePost
// };
// };

// --- invalid cases ---

// @ts-expect-error - should not have undefined (should it not?)
export const error_no_undefined: RequestHandler = () => {
return {
body: { no: Math.random() < 0.5 ? undefined : 'something' }
};
};

// @ts-expect-error - body must be JSON serializable
export const error_body_must_be_serializable: RequestHandler = () => {
return {
body: () => {}
};
};

// @ts-expect-error - body typed array must only be Uint8Array
export const error_other_typed_array_instances: RequestHandler = () => {
return {
body: new Uint16Array()
};
};

// @ts-expect-error - instances cannot be nested
export const error_nested_instances: RequestHandler = () => {
return {
body: { typed: new Uint8Array() }
};
};

// @ts-expect-error - fallthrough must be isolated
export const error_fallthrough_not_isolated: RequestHandler = () => {
return {
body: {},
fallthrough: true
};
};

// @ts-expect-error - fallthrough must be of value `true`
export const error_fallthrough_not_true: RequestHandler = () => {
return {
fallthrough: null
};
};
7 changes: 7 additions & 0 deletions packages/kit/test/typings/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": true
},
"include": ["**/*.test.ts"]
}