Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time

Endpoint testing

Translations: Espa帽ol, Fran莽ais, Italiano, 鏃ユ湰瑾, Portugu锚s, 袪褍褋褋泻懈泄, 绠浣撲腑鏂

Open in StackBlitz

AVA doesn't have a built-in method for testing endpoints, but you can use any HTTP client of your choosing, for example got. You'll also need to start an HTTP server, preferably on a unique port so that you can run tests in parallel. For that we recommend test-listen.

Since tests run concurrently, it's best to create a fresh server instance at least for each test file, but perhaps even for each test. This can be accomplished with test.before() and test.beforeEach() hooks and t.context. If you start your server using a test.before() hook you should make sure to execute your tests serially.

Check out the example below:

import http from 'http';
import test from 'ava';
import got from 'got';
import listen from 'test-listen';
import app from '../app';

test.before(async t => {
	t.context.server = http.createServer(app);
	t.context.prefixUrl = await listen(t.context.server);

test.after.always(t => {

test.serial('get /user', async t => {
	const {email} = await got('user', {prefixUrl: t.context.prefixUrl}).json();, '');

Other libraries you may find useful: