Skip to content

Commit 88e5f30

Browse files
committed
chore(tests): add automatic typegen for testing according to database schema
1 parent 8ba8745 commit 88e5f30

File tree

9 files changed

+303
-184
lines changed

9 files changed

+303
-184
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939
"docs:json": "typedoc --json docs/v2/spec.json --excludeExternals src/index.ts",
4040
"test": "run-s format:check test:types db:clean db:run test:run db:clean && node test/smoke.cjs && node test/smoke.mjs",
4141
"test:run": "jest --runInBand --coverage",
42-
"test:update": "run-s db:clean db:run && jest --runInBand --updateSnapshot && run-s db:clean",
42+
"test:update": "run-s db:clean db:run db:generate-test-types && jest --runInBand --updateSnapshot && run-s db:clean",
4343
"test:types": "run-s build && tsd --files 'test/**/*.test-d.ts'",
44+
"test:types:watch": "run-s build && tsd --files 'test/**/*.test-d.ts' --watch",
4445
"db:clean": "cd test/db && docker compose down --volumes",
45-
"db:run": "cd test/db && docker compose up --detach && wait-for-localhost 3000"
46+
"db:run": "cd test/db && docker compose up --detach && wait-for-localhost 3000",
47+
"db:generate-test-types": "cd test/db && docker compose up --detach && wait-for-localhost 8080 && curl --location 'http://0.0.0.0:8080/generators/typescript?included_schemas=public,personal&detect_one_to_one_relationships=true' > ../types.generated.ts"
4648
},
4749
"dependencies": {
4850
"@supabase/node-fetch": "^2.6.14"

test/db/docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ services:
2828
POSTGRES_PASSWORD: postgres
2929
POSTGRES_HOST: /var/run/postgresql
3030
POSTGRES_PORT: 5432
31+
pgmeta:
32+
image: supabase/postgres-meta:v0.87.1
33+
ports:
34+
- '8080:8080'
35+
environment:
36+
- PG_META_DB_URL=postgresql://postgres:postgres@db:5432/postgres

test/relationships.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PostgrestClient } from '../src/index'
2-
import { Database } from './types'
2+
import { Database } from './types.override'
33

44
const REST_URL = 'http://localhost:3000'
55
export const postgrest = new PostgrestClient<Database>(REST_URL)

test/returns.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expectType } from 'tsd'
22
import { PostgrestBuilder, PostgrestClient } from '../src/index'
3-
import { Database } from './types'
3+
import { Database } from './types.override'
44
import { TypeEqual } from 'ts-expect'
55

66
const REST_URL = 'http://localhost:3000'

test/rpc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PostgrestClient } from '../src/index'
2-
import { Database } from './types'
2+
import { Database } from './types.override'
33

44
const REST_URL = 'http://localhost:3000'
55
export const postgrest = new PostgrestClient<Database>(REST_URL)

test/select-query-parser/result.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Database, Json } from '../types'
1+
import { Database, Json } from '../types.override'
22
import { selectParams } from '../relationships'
33
import { GetResult } from '../../src/select-query-parser/result'
44
import { expectType } from 'tsd'

test/select-query-parser/select.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TypeEqual } from 'ts-expect'
33
import { Json } from '../../src/select-query-parser/types'
44
import { SelectQueryError } from '../../src/select-query-parser/utils'
55
import { Prettify } from '../../src/types'
6-
import { CustomUserDataType, Database } from '../types'
6+
import { CustomUserDataType, Database } from '../types.override'
77
import { selectQueries } from '../relationships'
88

99
// This test file is here to ensure that for a query against a specfic datatabase
@@ -622,6 +622,7 @@ type Schema = Database['public']
622622
username: string
623623
}
624624
}
625+
//@ts-expect-error this should work once we use unknown instead of Json type definition
625626
expectType<TypeEqual<typeof result, typeof expected>>(true)
626627
}
627628

0 commit comments

Comments
 (0)