Skip to content

Commit

Permalink
feat: add tests for serve functionality (#116)
Browse files Browse the repository at this point in the history
* chore: add test for `serve`

* chore: remove unneeded `await`

* chore: try 0.0.0.0

* refactor: use getPort

* refactor: move files
  • Loading branch information
Skn0tt committed Sep 20, 2022
1 parent 7343917 commit ed001bc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion node/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const serve = async ({
}
}

const server = await prepareServer({
const server = prepareServer({
deno,
distDirectory,
flags,
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"del": "^6.0.0",
"env-paths": "^3.0.0",
"execa": "^6.0.0",
"get-port": "^6.1.2",
"find-up": "^6.3.0",
"glob-to-regexp": "^0.4.1",
"node-fetch": "^3.1.1",
Expand Down
1 change: 1 addition & 0 deletions test/node/fixtures/serve_test/echo_env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default async () => new Response(JSON.stringify(Deno.env.toObject()))
45 changes: 45 additions & 0 deletions test/node/serving.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { join, resolve } from 'path'
import { fileURLToPath } from 'url'

import test from 'ava'
import getPort from 'get-port'
import fetch from 'node-fetch'

import { serve } from '../src/index.js'

const url = new URL(import.meta.url)
const dirname = fileURLToPath(url)
const fixturesDir = resolve(dirname, '..', 'fixtures')

test.serial('bundler serving functionality', async (t) => {
const port = await getPort()
const server = await serve({
port,
})

const { success } = await server(
[
{
name: 'echo_env',
path: join(fixturesDir, 'serve_test', 'echo_env.ts'),
},
],
{
very_secret_secret: 'i love netlify',
},
)

t.true(success)

const response = await fetch(`http://0.0.0.0:${port}/foo`, {
headers: {
'x-deno-functions': 'echo_env',
'x-deno-pass': 'passthrough',
'X-NF-Request-ID': 'foo',
},
})
t.is(response.status, 200)

const body = (await response.json()) as Record<string, string>
t.is(body.very_secret_secret, 'i love netlify')
})

0 comments on commit ed001bc

Please sign in to comment.