Skip to content

Commit 37f9671

Browse files
authored
Merge pull request #16 from supabase/tests
add functions-js tests
2 parents 3ee9f45 + 666e4d8 commit 37f9671

25 files changed

+13428
-1
lines changed

.github/workflows/tests.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Tests
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
- rc
9+
workflow_dispatch:
10+
11+
jobs:
12+
autotests:
13+
name: Run tests
14+
strategy:
15+
matrix:
16+
node: ['18']
17+
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v3
22+
23+
- name: Set up Node
24+
uses: actions/setup-node@v3
25+
with:
26+
node-version: ${{ matrix.node }}
27+
28+
- name: Install dependencies
29+
working-directory: test
30+
run: npm ci
31+
32+
- id: meta
33+
uses: docker/metadata-action@v4
34+
with:
35+
images: |
36+
edge-runtime:test
37+
38+
- name: Set up Docker Buildx
39+
uses: docker/setup-buildx-action@v2
40+
41+
- name: Build test image
42+
uses: docker/build-push-action@v4
43+
with:
44+
file: ./Dockerfile
45+
push: false
46+
load: true
47+
platforms: linux/amd64
48+
tags: edge-runtime:test
49+
cache-from: type=gha
50+
cache-to: type=gha,mode=max
51+
52+
- name: Run Test
53+
working-directory: test
54+
run: npm run test

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ temp/
66
.idea/
77
.idea
88

9+
node_modules/
910
.DS_Store

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,24 @@ docker run -it --rm -p 9000:9000 -v /path/to/supabase/functions:/usr/services su
3939

4040
* Select the Deno version to upgrade and visit its tag on GitHub (eg: https://github.com/denoland/deno/blob/v1.30.3/Cargo.toml)
4141
* Open the `Cargo.toml` at the root of of this repo and modify all `deno_*` modules to match to the selected tag of Deno.
42+
43+
## How to run tests
44+
45+
make sure the docker daemon is running and create a docker image:
46+
47+
```bash
48+
docker build -t edge-runtime:test .
49+
```
50+
51+
install tests dependencies:
52+
53+
```bash
54+
cd test
55+
npm install
56+
```
57+
58+
run tests:
59+
60+
```bash
61+
npm run test
62+
```

examples/main/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ serve(async (req: Request) => {
4141
{ status: 500, headers: { "Content-Type": "application/json" } },
4242
)
4343
}
44-
})
44+
})

test/.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
package-lock.json
3+
docker*

test/.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true,
6+
"printWidth": 100
7+
}

test/functions/_shared/cors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const corsHeaders = {
2+
'Access-Control-Allow-Origin': '*',
3+
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
4+
}

test/functions/hello/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { serve } from 'https://deno.land/std/http/server.ts'
2+
import { corsHeaders } from 'shared_cors'
3+
4+
serve((req: Request) => {
5+
if (req.method === 'OPTIONS') {
6+
return new Response('ok', { headers: corsHeaders })
7+
}
8+
return new Response('Hello World', {
9+
headers: {
10+
...corsHeaders,
11+
'content-type': 'text/plain',
12+
},
13+
})
14+
})

test/functions/hijack/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { serve } from 'https://deno.land/[email protected]/http/server.ts'
2+
3+
serve((req) => {
4+
const p = Deno.upgradeHttp(req);
5+
6+
(
7+
// Run this async IIFE concurrently, first packet won't arrive
8+
// until we return HTTP101 response.
9+
async () => {
10+
const [conn, firstPacket] = await p
11+
const decoder = new TextDecoder()
12+
const text = decoder.decode(firstPacket)
13+
console.log(text)
14+
// Hello
15+
const uint8Array = new Uint8Array([72, 101, 108, 108, 111])
16+
conn.write(uint8Array)
17+
conn.close()
18+
}
19+
)()
20+
21+
// HTTP101 - Switching Protocols
22+
return new Response(null, { status: 101 })
23+
})

test/functions/import_map.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"imports": {
3+
"oak": "https://deno.land/x/[email protected]/mod.ts",
4+
"shared_cors": "./_shared/cors.ts"
5+
}
6+
}

0 commit comments

Comments
 (0)