Skip to content

Commit 9a106dd

Browse files
SamVerschuerennovemberborn
authored andcommitted
Add StackBlitz example endpoint testing
1 parent 0d5860f commit 9a106dd

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

docs/recipes/endpoint-testing.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Translations: [Español](https://github.com/avajs/ava-docs/blob/master/es_ES/docs/recipes/endpoint-testing.md), [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/endpoint-testing.md), [Italiano](https://github.com/avajs/ava-docs/blob/master/it_IT/docs/recipes/endpoint-testing.md), [日本語](https://github.com/avajs/ava-docs/blob/master/ja_JP/docs/recipes/endpoint-testing.md), [Português](https://github.com/avajs/ava-docs/blob/master/pt_BR/docs/recipes/endpoint-testing.md), [Русский](https://github.com/avajs/ava-docs/blob/master/ru_RU/docs/recipes/endpoint-testing.md), [简体中文](https://github.com/avajs/ava-docs/blob/master/zh_CN/docs/recipes/endpoint-testing.md)
44

5+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/endpoint-testing?file=test.js&terminal=test&view=editor)
6+
57
AVA doesn't have a built-in method for testing endpoints, but you can use any HTTP client of your choosing, for example [`got`](https://github.com/sindresorhus/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`](https://github.com/zeit/test-listen).
68

79
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.
@@ -30,7 +32,7 @@ test.serial('get /user', async t => {
3032
});
3133
```
3234

33-
Other libraries you may find useful:
35+
Other libraries you may find useful:
3436

3537
- [`supertest`](https://github.com/visionmedia/supertest)
3638
- [`get-port`](https://github.com/sindresorhus/get-port)

examples/endpoint-testing/app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports = (request, response) => {
4+
if (request.url === '/user') {
5+
response.setHeader('Content-Type', 'application/json');
6+
response.end(JSON.stringify({email: '[email protected]'}));
7+
} else {
8+
response.writeHead('404');
9+
response.end();
10+
}
11+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "ava-endpoint-testing",
3+
"description": "Example for endpoint testing",
4+
"scripts": {
5+
"test": "ava"
6+
},
7+
"devDependencies": {
8+
"ava": "^3.15.0",
9+
"got": "^11.8.2",
10+
"test-listen": "^1.1.0"
11+
}
12+
}

examples/endpoint-testing/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Endpoint testing
2+
3+
> Example for [endpoint testing](https://github.com/avajs/ava/blob/main/docs/recipes/endpoint-testing.md)
4+
5+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/endpoint-testing?file=test.js&terminal=test&view=editor)

examples/endpoint-testing/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const http = require('http');
3+
4+
const test = require('ava');
5+
const got = require('got');
6+
const listen = require('test-listen');
7+
8+
const app = require('./app');
9+
10+
test.before(async t => {
11+
t.context.server = http.createServer(app);
12+
t.context.prefixUrl = await listen(t.context.server);
13+
});
14+
15+
test.after.always(t => {
16+
t.context.server.close();
17+
});
18+
19+
test.serial('get /user', async t => {
20+
const {email} = await got('user', {prefixUrl: t.context.prefixUrl}).json();
21+
22+
t.is(email, '[email protected]');
23+
});

0 commit comments

Comments
 (0)