-
Notifications
You must be signed in to change notification settings - Fork 365
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
refactor: tests/integration/20.command.functions.test.cjs #5889
Merged
lukasholzer
merged 3 commits into
netlify:main
from
hereje:refactor_test_esm/tests/integration/20.command.functions.test.cjs
Jul 27, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
156 changes: 156 additions & 0 deletions
156
tests/integration/commands/functions-serve/functions-serve.test.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import execa from 'execa' | ||
import getPort from 'get-port' | ||
import fetch from 'node-fetch' | ||
import { describe, test } from 'vitest' | ||
import waitPort from 'wait-port' | ||
|
||
import cliPath from '../../utils/cli-path.cjs' | ||
import { killProcess } from '../../utils/process.cjs' | ||
import { withSiteBuilder } from '../../utils/site-builder.cjs' | ||
|
||
const DEFAULT_PORT = 9999 | ||
const SERVE_TIMEOUT = 180_000 | ||
|
||
const withFunctionsServer = async ({ args = [], builder, port = DEFAULT_PORT }, testHandler) => { | ||
let ps | ||
try { | ||
ps = execa(cliPath, ['functions:serve', ...args], { | ||
cwd: builder.directory, | ||
}) | ||
|
||
ps.stdout.on('data', (data) => console.log(data.toString())) | ||
ps.stderr.on('data', (data) => console.log(data.toString())) | ||
|
||
const { open } = await waitPort({ | ||
port, | ||
output: 'silent', | ||
timeout: SERVE_TIMEOUT, | ||
}) | ||
if (!open) { | ||
throw new Error('Timed out waiting for functions server') | ||
} | ||
return await testHandler() | ||
} finally { | ||
await killProcess(ps) | ||
} | ||
} | ||
|
||
describe.concurrent('functions:serve command', () => { | ||
test('should serve functions on default port', async (t) => { | ||
await withSiteBuilder('site-with-ping-function', async (builder) => { | ||
await builder | ||
.withNetlifyToml({ config: { functions: { directory: 'functions' } } }) | ||
.withFunction({ | ||
path: 'ping.js', | ||
handler: async () => ({ | ||
statusCode: 200, | ||
body: 'ping', | ||
}), | ||
}) | ||
.buildAsync() | ||
|
||
await withFunctionsServer({ builder }, async () => { | ||
const response = await fetch(`http://localhost:9999/.netlify/functions/ping`) | ||
t.expect(await response.text()).toEqual('ping') | ||
}) | ||
}) | ||
}) | ||
|
||
test('should serve functions on custom port', async (t) => { | ||
await withSiteBuilder('site-with-ping-function', async (builder) => { | ||
await builder | ||
.withNetlifyToml({ config: { functions: { directory: 'functions' } } }) | ||
.withFunction({ | ||
path: 'ping.js', | ||
handler: async () => ({ | ||
statusCode: 200, | ||
body: 'ping', | ||
}), | ||
}) | ||
.buildAsync() | ||
|
||
const port = await getPort() | ||
await withFunctionsServer({ builder, args: ['--port', port], port }, async () => { | ||
const response = await fetch(`http://localhost:${port}/.netlify/functions/ping`) | ||
t.expect(await response.text()).toEqual('ping') | ||
}) | ||
}) | ||
}) | ||
|
||
test('should use settings from netlify.toml dev', async (t) => { | ||
await withSiteBuilder('site-with-ping-function', async (builder) => { | ||
const port = await getPort() | ||
await builder | ||
.withNetlifyToml({ | ||
config: { functions: { directory: 'functions' }, dev: { functions: 'other', functionsPort: port } }, | ||
}) | ||
.withFunction({ | ||
pathPrefix: 'other', | ||
path: 'ping.js', | ||
handler: async () => ({ | ||
statusCode: 200, | ||
body: 'ping', | ||
}), | ||
}) | ||
.buildAsync() | ||
|
||
await withFunctionsServer({ builder, port }, async () => { | ||
const response = await fetch(`http://localhost:${port}/.netlify/functions/ping`) | ||
t.expect(await response.text()).toEqual('ping') | ||
}) | ||
}) | ||
}) | ||
|
||
test('should inject env variables', async (t) => { | ||
await withSiteBuilder('site-with-env-function', async (builder) => { | ||
await builder | ||
.withNetlifyToml({ | ||
config: { | ||
build: { environment: { MY_CONFIG: 'FROM_CONFIG_FILE' } }, | ||
functions: { directory: 'functions' }, | ||
}, | ||
}) | ||
.withFunction({ | ||
path: 'echo-env.js', | ||
handler: async () => ({ | ||
statusCode: 200, | ||
// eslint-disable-next-line n/prefer-global/process | ||
body: `${process.env.MY_CONFIG}`, | ||
}), | ||
}) | ||
.buildAsync() | ||
|
||
const port = await getPort() | ||
await withFunctionsServer({ builder, args: ['--port', port], port }, async () => { | ||
const response = await fetch(`http://localhost:${port}/.netlify/functions/echo-env`) | ||
t.expect(await response.text()).toEqual('FROM_CONFIG_FILE') | ||
}) | ||
}) | ||
}) | ||
|
||
test('should handle content-types with charset', async (t) => { | ||
await withSiteBuilder('site-with-env-function', async (builder) => { | ||
await builder | ||
.withNetlifyToml({ | ||
config: { functions: { directory: 'functions' } }, | ||
}) | ||
.withFunction({ | ||
path: 'echo-event.js', | ||
handler: async (event) => ({ | ||
statusCode: 200, | ||
body: JSON.stringify(event), | ||
}), | ||
}) | ||
.buildAsync() | ||
|
||
const port = await getPort() | ||
await withFunctionsServer({ builder, args: ['--port', port], port }, async () => { | ||
const response = await fetch(`http://localhost:${port}/.netlify/functions/echo-event`, { | ||
headers: { 'content-type': 'application/json; charset=utf-8' }, | ||
}) | ||
const jsonResponse = await response.json() | ||
t.expect(jsonResponse.isBase64Encoded).toBe(false) | ||
}) | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for using
fetch
and getting rid ofgot
!