Skip to content

Commit 54209ab

Browse files
committed
chore: add basic types tests for ssr
1 parent 45b627e commit 54209ab

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Metadata } from 'next'
2+
3+
export const metadata: Metadata = {
4+
title: 'Supabase Integration Test',
5+
description: 'Testing Supabase integration with Next.js',
6+
}
7+
8+
export default function RootLayout({ children }: { children: React.ReactNode }) {
9+
return (
10+
<html lang="en">
11+
<body>{children}</body>
12+
</html>
13+
)
14+
}

test/integration/next/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"lint": "next lint",
88
"test": "playwright test",
99
"test:ui": "playwright test --ui",
10-
"test:debug": "playwright test --debug"
10+
"test:debug": "playwright test --debug",
11+
"test:types": "npx tsd --files tests/types/*.test-d.ts"
1112
},
1213
"dependencies": {
1314
"@radix-ui/react-checkbox": "^1.3.1",
@@ -37,6 +38,7 @@
3738
"postcss": "^8",
3839
"tailwindcss": "^3.4.1",
3940
"tailwindcss-animate": "^1.0.7",
41+
"tsd": "^0.33.0",
4042
"typescript": "^5"
4143
}
4244
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { createServerClient, createBrowserClient } from '@supabase/ssr'
2+
import { expectType } from 'tsd'
3+
4+
// Copied from ts-expect
5+
// https://github.com/TypeStrong/ts-expect/blob/master/src/index.ts#L23-L27
6+
export type TypeEqual<Target, Value> = (<T>() => T extends Target ? 1 : 2) extends <
7+
T
8+
>() => T extends Value ? 1 : 2
9+
? true
10+
: false
11+
12+
type Database = {
13+
public: {
14+
Tables: {
15+
shops: {
16+
Row: {
17+
address: string | null
18+
id: number
19+
shop_geom: unknown | null
20+
}
21+
Insert: {
22+
address?: string | null
23+
id: number
24+
shop_geom?: unknown | null
25+
}
26+
Update: {
27+
address?: string | null
28+
id?: number
29+
shop_geom?: unknown | null
30+
}
31+
Relationships: []
32+
}
33+
}
34+
Views: {
35+
[_ in never]: never
36+
}
37+
Functions: {
38+
[_ in never]: never
39+
}
40+
Enums: {
41+
[_ in never]: never
42+
}
43+
CompositeTypes: {
44+
[_ in never]: never
45+
}
46+
}
47+
}
48+
49+
{
50+
// createBrowserClient should return a typed client
51+
const pg12Client = createBrowserClient<Database>('HTTP://localhost:3000', '')
52+
const res12 = await pg12Client.from('shops').select('*')
53+
expectType<
54+
TypeEqual<
55+
| {
56+
address: string | null
57+
id: number
58+
shop_geom: unknown | null
59+
}[]
60+
| null,
61+
typeof res12.data
62+
>
63+
>(true)
64+
}
65+
66+
{
67+
// createBrowserClient should infer everything to any without types provided
68+
const pg12Client = createBrowserClient('HTTP://localhost:3000', '')
69+
const res12 = await pg12Client.from('shops').select('address, id, relation(field)')
70+
expectType<
71+
TypeEqual<
72+
| {
73+
address: any
74+
id: any
75+
relation: {
76+
field: any
77+
}[]
78+
}[]
79+
| null,
80+
typeof res12.data
81+
>
82+
>(true)
83+
}
84+
85+
{
86+
// createServerClient should return a typed client
87+
const pg12Server = createServerClient<Database>('HTTP://localhost:3000', '')
88+
const res12 = await pg12Server.from('shops').select('*')
89+
expectType<
90+
TypeEqual<
91+
| {
92+
address: string | null
93+
id: number
94+
shop_geom: unknown | null
95+
}[]
96+
| null,
97+
typeof res12.data
98+
>
99+
>(true)
100+
}
101+
102+
{
103+
// createServerClient should infer everything to any without types provided
104+
const pg12Server = createServerClient('HTTP://localhost:3000', '')
105+
const res12 = await pg12Server.from('shops').select('address, id, relation(field)')
106+
expectType<
107+
TypeEqual<
108+
| {
109+
address: any
110+
id: any
111+
relation: {
112+
field: any
113+
}[]
114+
}[]
115+
| null,
116+
typeof res12.data
117+
>
118+
>(true)
119+
}
120+
121+
// should default to postgrest 12 for untyped client
122+
{
123+
const pg12ServerClient = createServerClient('HTTP://localhost:3000', '')
124+
const res12Server = await pg12ServerClient.from('shops').update({ id: 21 }).maxAffected(1)
125+
const pg12BrowserClient = createBrowserClient('HTTP://localhost:3000', '')
126+
const res12Browser = await pg12BrowserClient.from('shops').update({ id: 21 }).maxAffected(1)
127+
expectType<typeof res12Server.Error>('maxAffected method only available on postgrest 13+')
128+
expectType<typeof res12Browser.Error>('maxAffected method only available on postgrest 13+')
129+
}

test/integration/next/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
}
2525
},
2626
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
27-
"exclude": ["node_modules"]
27+
"exclude": ["node_modules", "test/types/*.test-d.ts"]
2828
}

0 commit comments

Comments
 (0)