Skip to content

Commit

Permalink
refactor: tests/integration/630.serving functions go.test.cjs (#5914)
Browse files Browse the repository at this point in the history
* 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
hereje authored Aug 3, 2023
1 parent f503059 commit 867f322
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 178 deletions.
178 changes: 0 additions & 178 deletions tests/integration/630.serving-functions-go.test.cjs

This file was deleted.

185 changes: 185 additions & 0 deletions tests/integration/serve/functions-go.test.mjs
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()
}
})
})
})

1 comment on commit 867f322

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Dependency count: 1,303
  • Package size: 271 MB

Please sign in to comment.