Skip to content

Commit 51da8dd

Browse files
authored
test: run postgres.js tests with deno and bun (#713)
Deno ran out of the box. Bun needed some changes due to error stacktraces differences.
1 parent 16bcdf7 commit 51da8dd

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed

.github/workflows/elixir.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ jobs:
160160
with:
161161
node-version: 'lts/*'
162162
cache-dependency-path: 'test/integration/js/'
163+
- uses: denoland/setup-deno@v2
164+
with:
165+
deno-version: 'v2.x'
166+
- uses: oven-sh/setup-bun@v2
167+
with:
168+
bun-version: latest
163169
- name: Set up Rust
164170
uses: dtolnay/rust-toolchain@v1
165171
with:

test/integration/external_test.exs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,25 @@ defmodule Supavisor.Integration.ExternalTest do
5252
assert_run(ctx, ~w[postgres/index.js])
5353
end
5454

55-
# These currently do not pass
56-
# @tag runtime: "bun", mode: "session"
57-
# test "Bun session", ctx do
58-
# assert_run ctx, ~w[postgres/index.js], suite: "js"
59-
# end
60-
#
61-
# @tag runtime: "bun", mode: "transaction"
62-
# test "Bun transaction", ctx do
63-
# assert_run ctx, ~w[postgres/index.js], suite: "js"
64-
# end
65-
#
66-
# @tag runtime: "deno", mode: "session"
67-
# test "Deno session", ctx do
68-
# assert_run ctx, ~w[run --allow-all postgres/index.js], suite: "js"
69-
# end
70-
#
71-
# @tag runtime: "deno", mode: "transaction"
72-
# test "Deno transaction", ctx do
73-
# assert_run ctx, ~w[run --allow-all postgres/index.js], suite: "js"
74-
# end
55+
@tag runtime: "bun", mode: "session"
56+
test "Bun session", ctx do
57+
assert_run(ctx, ~w[postgres/index.js], suite: "js")
58+
end
59+
60+
@tag runtime: "bun", mode: "transaction"
61+
test "Bun transaction", ctx do
62+
assert_run(ctx, ~w[postgres/index.js], suite: "js")
63+
end
64+
65+
@tag runtime: "deno", mode: "session"
66+
test "Deno session", ctx do
67+
assert_run(ctx, ~w[run --allow-all postgres/index.js], suite: "js")
68+
end
69+
70+
@tag runtime: "deno", mode: "transaction"
71+
test "Deno transaction", ctx do
72+
assert_run(ctx, ~w[run --allow-all postgres/index.js], suite: "js")
73+
end
7574
end
7675

7776
defp assert_run(ctx, args, opts \\ []) do

test/integration/js/postgres/index.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,29 +1575,48 @@ t('numeric is returned as string', async() => [
15751575
typeof (await sql`select 1.2 as x`)[0].x
15761576
])
15771577

1578+
// This test needed changes to be compatible with Bun,
1579+
// because Bun stacktraces behave differently from Node.js/deno ones
15781580
t('Async stack trace', async() => {
15791581
const sql = postgres({ ...options, debug: false })
1582+
1583+
const extractLine = (stackLine) => {
1584+
const match = stackLine.match(/:(\d+)(?::|$)/)
1585+
return match ? parseInt(match[1]) : 0
1586+
}
1587+
15801588
return [
1581-
parseInt(new Error().stack.split('\n')[1].match(':([0-9]+):')[1]) + 1,
1582-
parseInt(await sql`error`.catch(x => x.stack.split('\n').pop().match(':([0-9]+):')[1]))
1589+
extractLine(new Error().stack.split('\n')[1]) + 1,
1590+
await sql`error`.catch(x => {
1591+
const lines = x.stack.split('\n')
1592+
for (const line of lines) {
1593+
if (line.includes('postgres/index.js') && !line.includes('native:')) {
1594+
return extractLine(line)
1595+
}
1596+
}
1597+
return extractLine(lines[lines.length - 1])
1598+
})
15831599
]
15841600
})
15851601

15861602
t('Debug has long async stack trace', async() => {
15871603
const sql = postgres({ ...options, debug: true })
15881604

1589-
return [
1590-
'watyo',
1591-
await yo().catch(x => x.stack.match(/wat|yo/g).join(''))
1592-
]
1593-
15941605
function yo() {
15951606
return wat()
15961607
}
15971608

15981609
function wat() {
15991610
return sql`error`
16001611
}
1612+
1613+
return [
1614+
true,
1615+
await yo().catch(x => {
1616+
const stackLines = x.stack.split('\n').filter(line => line.includes('postgres/index.js'))
1617+
return stackLines.length >= 2
1618+
})
1619+
]
16011620
})
16021621

16031622
t('Error contains query string', async() => [

test/integration/js/shared/test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,20 @@ t.timeout = (process.env.TIMEOUT || 5) | 0
1919

2020
async function test(o, name, options, fn) {
2121
typeof options !== 'object' && (fn = options, options = {})
22-
const line = new Error().stack.split('\n')[3].match(':([0-9]+):')[1]
22+
23+
const stack = new Error().stack.split('\n')
24+
let line
25+
26+
for (let i = 2; i < stack.length; i++) {
27+
const stackLine = stack[i]
28+
if (!stackLine.includes('test.js') && !stackLine.includes('native:')) {
29+
const match = stackLine.match(/:(\d+)(?::|$)/)
30+
if (match) {
31+
line = match[1]
32+
break
33+
}
34+
}
35+
}
2336

2437
await 1
2538

0 commit comments

Comments
 (0)