-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: tests/integration/630.serving functions go.test.cjs (#5914)
* refactor: move and rename test file functions-go * refactor: convert file to esm * refactor: replace ava with vitest * refactor: replace got with vitest * feat: run tests concurrently * style: apply lint rules * refactor: remove old file use functions-go instead
- Loading branch information
Showing
2 changed files
with
185 additions
and
178 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,185 @@ | ||
import fetch from 'node-fetch' | ||
import { describe, test } from 'vitest' | ||
|
||
import { tryAndLogOutput, withDevServer } from '../utils/dev-server.cjs' | ||
import { createMock as createExecaMock } from '../utils/mock-execa.cjs' | ||
import { pause } from '../utils/pause.cjs' | ||
import { withSiteBuilder } from '../utils/site-builder.cjs' | ||
|
||
const WAIT_WRITE = 1000 | ||
|
||
describe.concurrent('serve/functions-go', () => { | ||
test('Updates a Go function when a file is modified', async (t) => { | ||
const originalBody = 'Hello, world!' | ||
const updatedBody = 'Hello, Netlify!' | ||
const [execaMock, removeExecaMock] = await createExecaMock(` | ||
const { writeFileSync } = require('fs') | ||
let proxyCallCount = 0 | ||
const handler = (...args) => { | ||
if (args[0] === 'go') { | ||
const binaryPath = args[1][2] | ||
writeFileSync(binaryPath, '') | ||
return { | ||
stderr: '', | ||
stdout: '' | ||
} | ||
} | ||
if (args[0].includes('local-functions-proxy')) { | ||
proxyCallCount++ | ||
const response = { | ||
body: proxyCallCount === 1 ? '${originalBody}' : '${updatedBody}', | ||
statusCode: 200 | ||
} | ||
return { | ||
stderr: '', | ||
stdout: JSON.stringify(response) | ||
} | ||
} | ||
} | ||
module.exports = (...args) => ({ | ||
...handler(...args) || {}, | ||
stderr: { pipe: () => {} } | ||
}) | ||
`) | ||
|
||
await withSiteBuilder('go-function-update', async (builder) => { | ||
try { | ||
await builder | ||
.withNetlifyToml({ | ||
config: { | ||
build: { publish: 'public' }, | ||
functions: { directory: 'functions' }, | ||
}, | ||
}) | ||
.withContentFiles([ | ||
{ | ||
path: 'functions/go-func/go.mod', | ||
content: `<mock go.mod>`, | ||
}, | ||
{ | ||
path: 'functions/go-func/go.sum', | ||
content: `<mock go.sum>`, | ||
}, | ||
{ | ||
path: 'functions/go-func/main.go', | ||
content: `<mock main.go>`, | ||
}, | ||
]) | ||
.buildAsync() | ||
|
||
await withDevServer( | ||
{ | ||
cwd: builder.directory, | ||
env: execaMock, | ||
}, | ||
async ({ outputBuffer, port, waitForLogMatching }) => { | ||
await tryAndLogOutput(async () => { | ||
const response = await fetch(`http://localhost:${port}/.netlify/functions/go-func`).then((res) => | ||
res.text(), | ||
) | ||
t.expect(response).toEqual(originalBody) | ||
}, outputBuffer) | ||
|
||
await pause(WAIT_WRITE) | ||
|
||
await builder | ||
.withContentFile({ path: 'functions/go-func/main.go', content: `<updated mock main.go>` }) | ||
.buildAsync() | ||
|
||
await waitForLogMatching('Reloaded function go-func') | ||
|
||
const response = await fetch(`http://localhost:${port}/.netlify/functions/go-func`).then((res) => | ||
res.text(), | ||
) | ||
|
||
t.expect(response).toEqual(updatedBody) | ||
}, | ||
) | ||
} finally { | ||
await removeExecaMock() | ||
} | ||
}) | ||
}) | ||
|
||
// Reproduction test to verify the abscence/presence of a Go scheduled function | ||
test('Detects a Go scheduled function using netlify-toml config', async (t) => { | ||
const [execaMock, removeExecaMock] = await createExecaMock(` | ||
const assert = require('assert') | ||
const handler = (...args) => { | ||
if (args[0].includes('local-functions-proxy')) { | ||
const { body } = JSON.parse(args[1][1]) | ||
const { next_run } = JSON.parse(body) | ||
assert.ok(next_run) | ||
const response = { | ||
statusCode: 200 | ||
} | ||
return { | ||
stderr: '', | ||
stdout: JSON.stringify(response) | ||
} | ||
} | ||
} | ||
module.exports = (...args) => ({ | ||
...handler(...args) || {}, | ||
stderr: { pipe: () => {} } | ||
}) | ||
`) | ||
|
||
await withSiteBuilder('go-scheduled-function', async (builder) => { | ||
try { | ||
await builder | ||
.withNetlifyToml({ | ||
config: { | ||
build: { publish: 'public' }, | ||
functions: { directory: 'src/', 'go-scheduled-function': { schedule: '@daily' } }, | ||
}, | ||
}) | ||
.withContentFiles([ | ||
{ | ||
path: 'go.mod', | ||
content: `<mock go.mod>`, | ||
}, | ||
{ | ||
path: 'go.sum', | ||
content: `<mock go.sum>`, | ||
}, | ||
{ | ||
path: 'src/go-scheduled-function/main.go', | ||
content: `<mock main.go>`, | ||
}, | ||
]) | ||
.buildAsync() | ||
|
||
await withDevServer( | ||
{ | ||
cwd: builder.directory, | ||
env: execaMock, | ||
}, | ||
async ({ port }) => { | ||
const response = await fetch(`http://localhost:${port}/.netlify/functions/go-scheduled-function`) | ||
const responseBody = await response.text() | ||
t.expect(responseBody).toMatch(/You performed an HTTP request/) | ||
t.expect(responseBody).toMatch(/Your function returned `body`/) | ||
|
||
t.expect(response.status).toBe(200) | ||
}, | ||
) | ||
} finally { | ||
await removeExecaMock() | ||
} | ||
}) | ||
}) | ||
}) |
867f322
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.
📊 Benchmark results