Server
Testing

Testing

feTS natively supports HTTP injection, offering compatibility with any testing framework you prefer.

The fetch function on your router instance can be used to simulate an HTTP request.

💡

Using the router.fetch function doesn’t trigger a real HTTP request. Instead, it emulates the HTTP request in a manner that is fully compatible with the way Request/Response operate.

Using the fetch Function

The fetch function can be directly employed from the router instance, or it can be given to any client that accepts a fetch function.

The router.fetch is compliant with the WHATWG fetch API.

import { router } from './router'
 
const response = await router.fetch('/todo', {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    title: 'Buy milk',
    completed: false
  })
})
 
const responseJson = await response.json()
console.assert(responseJson.title === 'Buy milk', 'Title should be "Buy milk"')

Creating a feTS Client for Testing

You can use feTS Client to test your API in a type-safe way. It doesn’t require you to launch any actual HTTP server.

import { createClient } from 'fets'
import { router } from './router'
 
const client = createClient<typeof router>({
  fetchFn: router.fetch,
  endpoint: 'http://localhost:3000'
})
 
// Everything below is fully typed
const response = await client['/todo'].put({
  json: {
    title: 'Buy milk',
    completed: false
  }
})
 
const responseJson = await response.json()
console.assert(responseJson.title === 'Buy milk', 'Title should be "Buy milk"')