import { describe, expectTypeOf, test } from 'vitest' import type { NetlifyFunction } from '../main.js' describe('NetlifyFunction', () => { describe('regular', () => { test('requires `fetch` to return a Response', () => { expectTypeOf({ fetch: () => new Response('hi'), config: { path: '/hi' } } as const).toExtend() expectTypeOf({ fetch: () => {}, config: { path: '/hi' } } as const).not.toExtend() }) test('keeps the regular contract with `background: false`', () => { expectTypeOf({ fetch: () => new Response('hi'), config: { path: '/hi', background: false }, } as const).toExtend() }) test('requires a Response when there is no config', () => { expectTypeOf({ fetch: () => new Response('hi') } as const).toExtend() }) }) describe('background', () => { test('accepts `fetch` returning void or Promise', () => { expectTypeOf({ fetch: () => {}, config: { path: '/hi', background: true } } as const).toExtend() expectTypeOf({ fetch: async () => {}, config: { path: '/hi', background: true }, } as const).toExtend() }) // The runtime discards the response, so a Response return type would be misleading. test('rejects `fetch` returning a Response', () => { expectTypeOf({ fetch: () => new Response('hi'), config: { path: '/hi', background: true }, } as const).not.toExtend() }) }) test('allows event handlers alongside either variant', () => { expectTypeOf({ fetch: () => new Response('hi'), deploySucceeded: () => {}, config: { path: '/hi' }, } as const).toExtend() expectTypeOf({ fetch: () => {}, deploySucceeded: () => {}, config: { path: '/hi', background: true }, } as const).toExtend() }) })