Skip to content

Commit 048ebb9

Browse files
mandariniavallete
andauthored
test(postgrest): stabilize tests and setup prettier (#1813)
Co-authored-by: Andrew Valleteau <avallete@users.noreply.github.com>
1 parent e8a644e commit 048ebb9

File tree

5 files changed

+70
-53
lines changed

5 files changed

+70
-53
lines changed

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/postgrest-js/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
},
3535
"scripts": {
3636
"build": "npm run build:cjs && npm run build:esm",
37+
"format": "node scripts/format.js",
38+
"format:check": "node scripts/format.js check",
3739
"build:cjs": "tsc -p tsconfig.json",
3840
"build:esm": "cpy wrapper.mjs dist/esm/",
3941
"build:module": "npm run build:cjs",
@@ -50,7 +52,7 @@
5052
"type-check:test": "tsc --noEmit --project tsconfig.test.json",
5153
"db:clean": "cd test/db && docker compose down --volumes",
5254
"db:run": "cd test/db && docker compose up --detach && wait-for-localhost 3000",
53-
"db:generate-test-types": "cd test/db && docker compose up --detach && wait-for-localhost 8080 && wait-for-localhost 3000 && curl --location 'http://0.0.0.0:8080/generators/typescript?included_schemas=public,personal&detect_one_to_one_relationships=true' > ../types.generated.ts && node ../../scripts/update-json-type.js && cd ../../"
55+
"db:generate-test-types": "cd test/db && docker compose up --detach && wait-for-localhost 8080 && wait-for-localhost 3000 && curl --location 'http://0.0.0.0:8080/generators/typescript?included_schemas=public,personal&detect_one_to_one_relationships=true' > ../types.generated.ts && node ../scripts/update-json-type.js && cd ../../ && npm run format"
5456
},
5557
"dependencies": {
5658
"@supabase/node-fetch": "2.6.15",
@@ -65,6 +67,7 @@
6567
"tstyche": "^4.3.0",
6668
"type-fest": "^4.32.0",
6769
"wait-for-localhost-cli": "^3.0.0",
68-
"zod": "^3.25.76"
70+
"zod": "^3.25.76",
71+
"prettier": "^2.6.2"
6972
}
7073
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
3+
const { execSync } = require('child_process')
4+
const path = require('path')
5+
6+
// Get the monorepo root directory (go up from scripts/ to postgrest-js/ to core/ to packages/ to root)
7+
const monorepoRoot = path.resolve(__dirname, '../../../../')
8+
9+
// Run prettier from the monorepo root
10+
const command =
11+
process.argv[2] === 'check'
12+
? 'npx prettier --ignore-path packages/core/postgrest-js/.gitignore --check "packages/core/postgrest-js/**/*{ts,js,mjs,json,yml,yaml}"'
13+
: 'npx prettier --ignore-path packages/core/postgrest-js/.gitignore --write "packages/core/postgrest-js/**/*{ts,js,mjs,json,yml,yaml}"'
14+
15+
try {
16+
execSync(command, { cwd: monorepoRoot, stdio: 'inherit' })
17+
} catch (error) {
18+
process.exit(error.status || 1)
19+
}

packages/core/postgrest-js/test/advanced_rpc.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ describe('advanced rpc', () => {
782782

783783
test('polymorphic function with unnamed int param', async () => {
784784
const res = await postgrest.rpc(
785-
// @ts-expect-error Argument of type '"polymorphic_function_with_unnamed_integer"' is not assignable to parameter of type '"blurb_message" | "function_returning_row" | "function_returning_set_of_rows"
785+
// @ts-expect-error Argument of type '"polymorphic_function_with_unnamed_integer"' is not assignable to parameter of type
786786
'polymorphic_function_with_unnamed_integer',
787787
{
788788
'': 1,

packages/core/postgrest-js/test/embeded_functions_join.test.ts

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,36 +1229,19 @@ describe('embeded functions select', () => {
12291229
// test select the created_ago embeded function
12301230
test('select the created_ago embeded function', async () => {
12311231
const res = await postgrest.from('users_audit').select('id, created_ago')
1232-
expect(res).toMatchInlineSnapshot(`
1233-
Object {
1234-
"count": null,
1235-
"data": Array [
1236-
Object {
1237-
"created_ago": 7,
1238-
"id": 1,
1239-
},
1240-
Object {
1241-
"created_ago": 7,
1242-
"id": 2,
1243-
},
1244-
Object {
1245-
"created_ago": 7,
1246-
"id": 3,
1247-
},
1248-
Object {
1249-
"created_ago": 7,
1250-
"id": 4,
1251-
},
1252-
Object {
1253-
"created_ago": 7,
1254-
"id": 5,
1255-
},
1256-
],
1257-
"error": null,
1258-
"status": 200,
1259-
"statusText": "OK",
1260-
}
1261-
`)
1232+
// Don't snapshot time-based values - they change daily and cause flaky tests
1233+
expect(res.error).toBeNull()
1234+
expect(res.status).toBe(200)
1235+
expect(res.statusText).toBe('OK')
1236+
expect(res.count).toBeNull()
1237+
expect(res.data).toHaveLength(5)
1238+
// Verify structure of each record
1239+
expect(res.data?.[0]).toMatchObject({
1240+
id: expect.any(Number),
1241+
created_ago: expect.any(Number),
1242+
})
1243+
// Verify time-based value is reasonable
1244+
expect(res.data?.[0].created_ago).toBeGreaterThanOrEqual(0)
12621245
let result: Exclude<typeof res.data, null>
12631246
const ExpectedSchema = z.array(
12641247
z.object({
@@ -1271,7 +1254,7 @@ describe('embeded functions select', () => {
12711254
expectType<TypeEqual<typeof result, typeof expected>>(true)
12721255
ExpectedSchema.parse(res.data)
12731256
const use_rpc_call = await postgrest.rpc('created_ago', {
1274-
// @ts-expect-error - id is not a parameter of the created_ago function
1257+
// @ts-expect-error Object literal may only specify known properties
12751258
id: 1,
12761259
})
12771260
expect(use_rpc_call).toMatchInlineSnapshot(`
@@ -1301,24 +1284,19 @@ describe('embeded functions select', () => {
13011284
// Test the days_since_event embeded function over partitioned table
13021285
test('select the days_since_event embeded function over partitioned table', async () => {
13031286
const res = await postgrest.from('events').select('id, days_since_event')
1304-
expect(res).toMatchInlineSnapshot(`
1305-
Object {
1306-
"count": null,
1307-
"data": Array [
1308-
Object {
1309-
"days_since_event": 500,
1310-
"id": 1,
1311-
},
1312-
Object {
1313-
"days_since_event": 222,
1314-
"id": 2,
1315-
},
1316-
],
1317-
"error": null,
1318-
"status": 200,
1319-
"statusText": "OK",
1320-
}
1321-
`)
1287+
// Don't snapshot time-based values - they change daily and cause flaky tests
1288+
expect(res.error).toBeNull()
1289+
expect(res.status).toBe(200)
1290+
expect(res.statusText).toBe('OK')
1291+
expect(res.count).toBeNull()
1292+
expect(res.data).toHaveLength(2)
1293+
// Verify structure of each record
1294+
expect(res.data?.[0]).toMatchObject({
1295+
id: expect.any(Number),
1296+
days_since_event: expect.any(Number),
1297+
})
1298+
// Verify time-based value is reasonable
1299+
expect(res.data?.[0].days_since_event).toBeGreaterThanOrEqual(0)
13221300
let result: Exclude<typeof res.data, null>
13231301
const ExpectedSchema = z.array(
13241302
z.object({
@@ -1331,7 +1309,7 @@ describe('embeded functions select', () => {
13311309
expectType<TypeEqual<typeof result, typeof expected>>(true)
13321310
ExpectedSchema.parse(res.data)
13331311
const use_rpc_call = await postgrest.rpc('days_since_event', {
1334-
// @ts-expect-error - id is not a parameter of the days_since_event function
1312+
// @ts-expect-error Object literal may only specify known properties
13351313
id: 1,
13361314
})
13371315
expect(use_rpc_call).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)