Skip to content

Commit 859f3ff

Browse files
tommy-mitchellsindresorhusnovemberborn
authored
Update examples to use AVA 6
Co-authored-by: Sindre Sorhus <[email protected]> Co-authored-by: Mark Wubben <[email protected]>
1 parent eb2b48d commit 859f3ff

File tree

28 files changed

+115
-116
lines changed

28 files changed

+115
-116
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/test-tap/**/node_modules/
66
/test/**/fixtures/**/node_modules/*/
77
/examples/**/node_modules/
8+
/examples/**/package-lock.json

docs/05-command-line.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,6 @@ test('moo will also run', t => {
144144
test.only('boo will run but not exclusively', t => {
145145
t.pass();
146146
});
147-
148-
// Won't run, no title
149-
test(function (t) {
150-
t.fail();
151-
});
152-
153-
// Won't run, no explicit title
154-
test(function foo(t) {
155-
t.fail();
156-
});
157147
```
158148

159149
## Running tests at specific line numbers

docs/recipes/endpoint-testing.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ Translations: [Español](https://github.com/avajs/ava-docs/blob/main/es_ES/docs/
44

55
[![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)
66

7-
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).
7+
AVA doesn't have a built-in method for testing endpoints, but you can use any HTTP client of your choosing, for example [`ky`](https://github.com/sindresorhus/ky). 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 [`async-listen`](https://github.com/vercel/async-listen).
88

99
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.
1010

1111
Check out the example below:
1212

1313
```js
14-
import http from 'node:http';
14+
import {createServer} from 'node:http';
15+
16+
import {listen} from 'async-listen';
1517
import test from 'ava';
16-
import got from 'got';
17-
import listen from 'test-listen';
18-
import app from '../app';
18+
import ky, {HTTPError} from 'ky';
19+
20+
import app from './app.js';
1921

2022
test.before(async t => {
21-
t.context.server = http.createServer(app);
23+
t.context.server = createServer(app);
2224
t.context.prefixUrl = await listen(t.context.server);
2325
});
2426

@@ -27,9 +29,17 @@ test.after.always(t => {
2729
});
2830

2931
test.serial('get /user', async t => {
30-
const {email} = await got('user', {prefixUrl: t.context.prefixUrl}).json();
32+
const {email} = await ky('user', {prefixUrl: t.context.prefixUrl}).json();
33+
3134
t.is(email, '[email protected]');
3235
});
36+
37+
test.serial('404', async t => {
38+
await t.throwsAsync(
39+
ky('password', {prefixUrl: t.context.prefixUrl}),
40+
{message: /Request failed with status code 404 Not Found/, instanceOf: HTTPError},
41+
);
42+
});
3343
```
3444

3545
Other libraries you may find useful:

examples/endpoint-testing/app.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
'use strict';
2-
3-
const app = (request, response) => {
1+
export default function app(request, response) {
42
if (request.url === '/user') {
53
response.setHeader('Content-Type', 'application/json');
64
response.end(JSON.stringify({email: '[email protected]'}));
75
} else {
86
response.writeHead('404');
97
response.end();
108
}
11-
};
12-
13-
module.exports = app;
9+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "ava-endpoint-testing",
33
"description": "Example for endpoint testing",
4+
"type": "module",
45
"scripts": {
5-
"test": "ava"
6+
"test": "ava test.js"
67
},
78
"devDependencies": {
8-
"ava": "^3.15.0",
9-
"got": "^11.8.2",
10-
"test-listen": "^1.1.0"
9+
"ava": "^6",
10+
"ky": "^1.4.0",
11+
"async-listen": "^3.0.1"
1112
}
1213
}

examples/endpoint-testing/test.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
'use strict';
2-
const http = require('node:http');
1+
import {createServer} from 'node:http';
32

4-
const test = require('ava');
5-
const got = require('got');
6-
const listen = require('test-listen');
3+
import {listen} from 'async-listen';
4+
import test from 'ava';
5+
import ky, {HTTPError} from 'ky';
76

8-
const app = require('./app.js');
7+
import app from './app.js';
98

109
test.before(async t => {
11-
t.context.server = http.createServer(app);
10+
t.context.server = createServer(app);
1211
t.context.prefixUrl = await listen(t.context.server);
1312
});
1413

@@ -17,7 +16,14 @@ test.after.always(t => {
1716
});
1817

1918
test.serial('get /user', async t => {
20-
const {email} = await got('user', {prefixUrl: t.context.prefixUrl}).json();
19+
const {email} = await ky('user', {prefixUrl: t.context.prefixUrl}).json();
2120

2221
t.is(email, '[email protected]');
2322
});
23+
24+
test.serial('404', async t => {
25+
await t.throwsAsync(
26+
ky('password', {prefixUrl: t.context.prefixUrl}),
27+
{message: /Request failed with status code 404 Not Found/, instanceOf: HTTPError},
28+
);
29+
});

examples/macros/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
exports.sum = (a, b) => a + b;
1+
export function sum(a, b) {
2+
return a + b;
3+
}

examples/macros/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "ava-macros",
33
"description": "Example for reusing test logic through macros",
4+
"type": "module",
45
"scripts": {
5-
"test": "ava"
6+
"test": "ava test.js"
67
},
78
"devDependencies": {
8-
"ava": "^3.15.0"
9+
"ava": "^6"
910
}
1011
}

examples/macros/test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const test = require('ava');
1+
import test from 'ava';
22

3-
const {sum} = require('./index.js');
3+
import {sum} from './index.js';
44

5-
function macro(t, a, b, expected) {
6-
t.is(sum(a, b), expected);
7-
}
8-
9-
macro.title = (providedTitle, a, b, expected) => `${providedTitle ?? ''} ${a}+${b} = ${expected}`.trim();
5+
const macro = test.macro({
6+
exec(t, a, b, expected) {
7+
t.is(sum(a, b), expected);
8+
},
9+
title: (providedTitle, a, b, expected) => `${providedTitle ?? ''} ${a}+${b} = ${expected}`.trim(),
10+
});
1011

1112
test(macro, 2, 2, 4);
1213
test(macro, 3, 3, 6);

examples/matching-titles/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "ava-matching-titles",
33
"description": "Example for running tests with matching titles",
4+
"type": "module",
45
"scripts": {
5-
"test": "ava --match='*oo*'"
6+
"test": "ava test.js --match='*oo*'"
67
},
78
"devDependencies": {
8-
"ava": "^3.15.0"
9+
"ava": "^6"
910
}
1011
}

examples/matching-titles/test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
'use strict';
2-
const test = require('ava');
1+
import test from 'ava';
32

43
test('foo will run', t => {
54
t.pass();
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "ava-specific-line-numbers",
33
"description": "Example for running tests at specific line numbers",
4+
"type": "module",
45
"scripts": {
56
"test": "ava test.js:5"
67
},
78
"devDependencies": {
8-
"ava": "^3.15.0"
9+
"ava": "^6"
910
}
1011
}

examples/specific-line-numbers/test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
'use strict';
2-
const test = require('ava');
1+
import test from 'ava';
32

43
test('unicorn', t => {
54
t.pass();

examples/tap-reporter/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "ava-tap-reporter",
33
"description": "Example for a custom TAP reporter",
4+
"type": "module",
45
"scripts": {
5-
"test": "ava --tap | tap-nyan"
6+
"test": "ava test.js --tap | tap-nyan"
67
},
78
"devDependencies": {
8-
"ava": "^3.15.0",
9+
"ava": "^6",
910
"tap-nyan": "^1.1.0"
1011
}
1112
}

examples/tap-reporter/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Running tests at specific line numbers
22

3-
> Example for a [custom TAP reporter](https://github.com/avajs/ava/blob/main/docs/05-command-line.md#running-tests-at-specific-line-numbers)
3+
> Example for a [custom TAP reporter](https://github.com/avajs/ava/blob/main/docs/05-command-line.md#tap-reporter)
44
55
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/tap-reporter?file=test.js&terminal=test&view=editor)

examples/tap-reporter/test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
'use strict';
2-
const test = require('ava');
1+
import test from 'ava';
32

43
test('unicorn', t => {
54
t.pass();

examples/timeouts/index.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
1-
'use strict';
2-
31
const delay = ms => new Promise(resolve => {
42
setTimeout(resolve, ms);
53
});
64

7-
exports.fetchUsers = async () => {
5+
export async function fetchUsers() {
86
await delay(50);
97

10-
return [
11-
{
12-
id: 1,
13-
firstName: 'Ava',
14-
name: 'Rocks',
15-
16-
},
17-
];
18-
};
8+
return [{
9+
id: 1,
10+
firstName: 'Ava',
11+
name: 'Rocks',
12+
13+
}];
14+
}
1915

20-
exports.fetchPosts = async userId => {
16+
export async function fetchPosts(userId) {
2117
await delay(200);
2218

23-
return [
24-
{
25-
id: 1,
26-
userId,
27-
message: 'AVA Rocks 🚀',
28-
},
29-
];
30-
};
19+
return [{
20+
id: 1,
21+
userId,
22+
message: 'AVA Rocks 🚀',
23+
}];
24+
}
3125

32-
exports.createPost = async message => {
26+
export async function createPost(message) {
3327
await delay(3000);
3428

3529
return {
3630
id: 2,
3731
userId: 1,
3832
message,
3933
};
40-
};
34+
}

examples/timeouts/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "ava-timeouts",
33
"description": "Example for test timeouts",
4+
"type": "module",
45
"scripts": {
5-
"test": "ava --timeout=2s --verbose"
6+
"test": "ava test.js --timeout=2s --verbose"
67
},
78
"devDependencies": {
8-
"ava": "^3.15.0"
9+
"ava": "^6"
910
}
1011
}

examples/timeouts/test.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1-
'use strict';
2-
const test = require('ava');
1+
import test from 'ava';
32

4-
const {fetchUsers, fetchPosts, createPost} = require('./index.js');
3+
import {fetchUsers, fetchPosts, createPost} from './index.js';
54

65
test('retrieve users', async t => {
76
t.timeout(100);
87

98
const users = await fetchUsers();
109

11-
t.deepEqual(users, [
12-
{
13-
id: 1,
14-
firstName: 'Ava',
15-
name: 'Rocks',
16-
17-
},
18-
]);
10+
t.deepEqual(users, [{
11+
id: 1,
12+
firstName: 'Ava',
13+
name: 'Rocks',
14+
15+
}]);
1916
});
2017

2118
test('retrieve posts', async t => {
2219
t.timeout(100, 'retrieving posts is too slow');
2320

2421
const posts = await fetchPosts(1);
2522

26-
t.deepEqual(posts, [
27-
{
28-
id: 1,
29-
userId: 1,
30-
message: 'AVA Rocks 🚀',
31-
},
32-
]);
23+
t.deepEqual(posts, [{
24+
id: 1,
25+
userId: 1,
26+
message: 'AVA Rocks 🚀',
27+
}]);
3328
});
3429

3530
test('create post', async t => {

examples/typescript-basic/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

examples/typescript-basic/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "ava-typescript-basic",
33
"description": "Basic example for AVA with TypeScript",
4+
"type": "module",
45
"scripts": {
56
"test": "ava"
67
},
78
"devDependencies": {
8-
"@ava/typescript": "^2.0.0",
9-
"@sindresorhus/tsconfig": "^1.0.2",
10-
"ava": "^3.15.0",
11-
"typescript": "^4.3.4"
9+
"@ava/typescript": "^5.0.0",
10+
"@sindresorhus/tsconfig": "^6.0.0",
11+
"ava": "^6",
12+
"typescript": "~5.5.3"
1213
},
1314
"ava": {
1415
"typescript": {

0 commit comments

Comments
 (0)