Skip to content
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

chore(tests): add auto types gen and override for testing #613

Merged
merged 4 commits into from
Apr 4, 2025
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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
"docs:json": "typedoc --json docs/v2/spec.json --excludeExternals src/index.ts",
"test": "run-s format:check test:types db:clean db:run test:run db:clean && node test/smoke.cjs && node test/smoke.mjs",
"test:run": "jest --runInBand --coverage",
"test:update": "run-s db:clean db:run && jest --runInBand --updateSnapshot && run-s db:clean",
"test:update": "run-s db:clean db:run db:generate-test-types && jest --runInBand --updateSnapshot && run-s db:clean",
"test:types": "run-s build && tsd --files 'test/**/*.test-d.ts'",
"test:types:watch": "run-s build && tsd --files 'test/**/*.test-d.ts' --watch",
"db:clean": "cd test/db && docker compose down --volumes",
"db:run": "cd test/db && docker compose up --detach && wait-for-localhost 3000"
"db:run": "cd test/db && docker compose up --detach && wait-for-localhost 3000",
"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 && sed -i 's/export type Json = .*/export type Json = unknown;/' ../types.generated.ts"
Copy link
Member Author

Choose a reason for hiding this comment

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

note

Here we replace the Json type definition by unkown as it allow MergeDeep to work better to override the column type with a custom definition.

Long term, we will deprecate our custom type in favor of unknown for all Json types including for the select-query-parser. This might be a breaking change in the types system for some users.

Related: supabase/postgres-meta#750

},
"dependencies": {
"@supabase/node-fetch": "^2.6.14"
Expand Down
2 changes: 1 addition & 1 deletion test/basic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PostgrestClient } from '../src/index'
import { CustomUserDataType, Database } from './types'
import { CustomUserDataType, Database } from './types.override'

const REST_URL = 'http://localhost:3000'
const postgrest = new PostgrestClient<Database>(REST_URL)
Expand Down
6 changes: 6 additions & 0 deletions test/db/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ services:
POSTGRES_PASSWORD: postgres
POSTGRES_HOST: /var/run/postgresql
POSTGRES_PORT: 5432
pgmeta:
image: supabase/postgres-meta:v0.87.1
ports:
- '8080:8080'
environment:
- PG_META_DB_URL=postgresql://postgres:postgres@db:5432/postgres
2 changes: 1 addition & 1 deletion test/filters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PostgrestClient } from '../src/index'
import { Database } from './types'
import { Database } from './types.override'

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

Expand Down
5 changes: 2 additions & 3 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { TypeEqual } from 'ts-expect'
import { expectError, expectType } from 'tsd'
import { PostgrestClient, PostgrestError } from '../src/index'
import { Prettify } from '../src/types'
import { Database, Json } from './types'
import { Json } from '../src/select-query-parser/types'
import { Database } from './types.override'

const REST_URL = 'http://localhost:3000'
const postgrest = new PostgrestClient<Database>(REST_URL)
Expand Down Expand Up @@ -189,8 +190,6 @@ const postgrest = new PostgrestClient<Database>(REST_URL)
if (result.error) {
throw new Error(result.error.message)
}
// getting this w/o the cast, not sure why:
// Parameter type Json is declared too wide for argument type Json
expectType<Json>(result.data.bar)
expectType<string>(result.data.baz)
}
Expand Down
3 changes: 2 additions & 1 deletion test/override-types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expectType } from 'tsd'
import { TypeEqual } from 'ts-expect'
import { PostgrestClient } from '../src'
import { CustomUserDataType, Database, Json } from './types'
import { CustomUserDataType, Database } from './types.override'
import { Json } from './types.generated'

const REST_URL = 'http://localhost:54321'
const postgrest = new PostgrestClient<Database>(REST_URL)
Expand Down
2 changes: 1 addition & 1 deletion test/relationships.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PostgrestClient } from '../src/index'
import { Database } from './types'
import { Database } from './types.override'

const REST_URL = 'http://localhost:3000'
export const postgrest = new PostgrestClient<Database>(REST_URL)
Expand Down
2 changes: 1 addition & 1 deletion test/resource-embedding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PostgrestClient } from '../src/index'
import { Database } from './types'
import { Database } from './types.override'

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

Expand Down
2 changes: 1 addition & 1 deletion test/returns.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectType } from 'tsd'
import { PostgrestBuilder, PostgrestClient } from '../src/index'
import { Database } from './types'
import { Database } from './types.override'
import { TypeEqual } from 'ts-expect'

const REST_URL = 'http://localhost:3000'
Expand Down
2 changes: 1 addition & 1 deletion test/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PostgrestClient } from '../src/index'
import { Database } from './types'
import { Database } from './types.override'

const REST_URL = 'http://localhost:3000'
export const postgrest = new PostgrestClient<Database>(REST_URL)
Expand Down
4 changes: 3 additions & 1 deletion test/select-query-parser/result.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Database, Json } from '../types'
import { Database } from '../types.override'
import { selectParams } from '../relationships'
import { GetResult } from '../../src/select-query-parser/result'
import { expectType } from 'tsd'
import { TypeEqual } from 'ts-expect'
import { SelectQueryError } from '../../src/select-query-parser/utils'
// TODO: should change this type in favor of unknown instead of the current one
import type { Json } from '../../src/select-query-parser/types'

Comment on lines +7 to +8
Copy link
Member Author

Choose a reason for hiding this comment

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

note

Since select-query-parser has it's own static Json type for json field selectors, we must use this one for type equality comparator.

type SelectQueryFromTableResult<
TableName extends keyof Database['public']['Tables'],
Expand Down
2 changes: 1 addition & 1 deletion test/select-query-parser/rpc.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { postgrest, selectParams, RPC_NAME } from '../rpc'
import { Database } from '../types'
import { Database } from '../types.override'
import { expectType } from 'tsd'
import { TypeEqual } from 'ts-expect'

Expand Down
2 changes: 1 addition & 1 deletion test/select-query-parser/select.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TypeEqual } from 'ts-expect'
import { Json } from '../../src/select-query-parser/types'
import { SelectQueryError } from '../../src/select-query-parser/utils'
import { Prettify } from '../../src/types'
import { CustomUserDataType, Database } from '../types'
import { CustomUserDataType, Database } from '../types.override'
import { selectQueries } from '../relationships'

// This test file is here to ensure that for a query against a specfic datatabase
Expand Down
2 changes: 1 addition & 1 deletion test/transforms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PostgrestClient } from '../src/index'
import { Database } from './types'
import { Database } from './types.override'

import { AbortController } from 'node-abort-controller'

Expand Down
Loading
Loading