Skip to content

Commit

Permalink
refactor: replace got with node-fetch on functions-with-args.test.js
Browse files Browse the repository at this point in the history
related to netlify#5695
  • Loading branch information
hereje committed Dec 5, 2023
1 parent 3268f29 commit d2f807b
Showing 1 changed file with 8 additions and 21 deletions.
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

0 comments on commit d2f807b

Please sign in to comment.