Skip to content
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: replace got with node-fetch on functions-with-args.test.js #6245

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fetch from 'node-fetch'
import { describe, test } from 'vitest'

import { tryAndLogOutput, withDevServer } from '../../utils/dev-server.ts'
import got from '../../utils/got.js'
import { pause } from '../../utils/pause.js'
import { withSiteBuilder } from '../../utils/site-builder.ts'

Expand All @@ -15,18 +14,6 @@ const testMatrix = [{ args: [] }, { args: ['esbuild'] }]

const WAIT_WRITE = 3000

// TODO: Remove function and got
const gotCatch404 = async (url, options) => {
try {
return await got(url, options)
} catch (error) {
if (error.response && error.response.statusCode === 404) {
return error.response
}
throw error
}
}

describe.concurrent.each(testMatrix)('withSiteBuilder with args: $args', ({ args }) => {
test('Updates a JavaScript function when its main file is modified', async (t) => {
await withSiteBuilder('js-function-update-main-file', async (builder) => {
Expand Down Expand Up @@ -303,9 +290,9 @@ describe.concurrent.each(testMatrix)('withSiteBuilder with args: $args', ({ args

await withDevServer({ cwd: builder.directory, args }, async ({ outputBuffer, port, waitForLogMatching }) => {
await tryAndLogOutput(async () => {
const unauthenticatedResponse = await gotCatch404(`http://localhost:${port}/.netlify/functions/hello`)
const unauthenticatedResponse = await fetch(`http://localhost:${port}/.netlify/functions/hello`)

t.expect(unauthenticatedResponse.statusCode).toBe(404)
t.expect(unauthenticatedResponse.status).toBe(404)
}, outputBuffer)

await pause(WAIT_WRITE)
Expand All @@ -322,7 +309,7 @@ describe.concurrent.each(testMatrix)('withSiteBuilder with args: $args', ({ args

await waitForLogMatching('Loaded function hello')

const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()
const response = await fetch(`http://localhost:${port}/.netlify/functions/hello`).then((res) => res.text())

t.expect(response).toEqual('Hello')
})
Expand Down Expand Up @@ -353,9 +340,9 @@ describe.concurrent.each(testMatrix)('withSiteBuilder with args: $args', ({ args

await withDevServer({ cwd: builder.directory, args }, async ({ outputBuffer, port, waitForLogMatching }) => {
await tryAndLogOutput(async () => {
const unauthenticatedResponse = await gotCatch404(`http://localhost:${port}/.netlify/functions/hello`)
const unauthenticatedResponse = await fetch(`http://localhost:${port}/.netlify/functions/hello`)

t.expect(unauthenticatedResponse.statusCode).toBe(404)
t.expect(unauthenticatedResponse.status).toBe(404)
}, outputBuffer)

await pause(WAIT_WRITE)
Expand Down Expand Up @@ -388,7 +375,7 @@ describe.concurrent.each(testMatrix)('withSiteBuilder with args: $args', ({ args

await waitForLogMatching('Loaded function hello')

const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()
const response = await fetch(`http://localhost:${port}/.netlify/functions/hello`).then((res) => res.text())

t.expect(response).toEqual('Modern Web Development on the Jamstack')
})
Expand Down Expand Up @@ -433,9 +420,9 @@ describe.concurrent.each(testMatrix)('withSiteBuilder with args: $args', ({ args

await waitForLogMatching('Removed function hello')

const { statusCode } = await gotCatch404(`http://localhost:${port}/.netlify/functions/hello`)
const { status } = await fetch(`http://localhost:${port}/.netlify/functions/hello`)

t.expect(statusCode).toBe(404)
t.expect(status).toBe(404)
})
})
})
Expand Down
Loading