Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 1.67 KB

endpoint-testing.md

File metadata and controls

31 lines (23 loc) · 1.67 KB

Endpoint testing

Translations: Español, Français, Italiano, 日本語, Português, Русский, 简体中文

AVA doesn't have a builtin method for testing endpoints, but you can use any assertion library with it. Let's use supertest.

Since tests run concurrently, it's best to create a fresh server instance for each test, because if we referenced the same instance, it could be mutated between tests. This can be accomplished with a test.beforeEach and t.context, or with simply a factory function:

function makeApp() {
	const app = express();
	app.use(bodyParser.json());
	app.post('/signup', signupHandler);
	return app;
}

Next, just inject your server instance into supertest. The only gotcha is to use a promise or async/await syntax instead of the supertest end method:

test('signup:Success', async t => {
	t.plan(2);

	const res = await request(makeApp())
		.post('/signup')
		.send({email: '[email protected]', password: '123123'});

	t.is(res.status, 200);
	t.is(res.body.email, '[email protected]');
});