Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples to use AVA 6 #3335

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/test-tap/**/node_modules/
/test/**/fixtures/**/node_modules/*/
/examples/**/node_modules/
/examples/**/package-lock.json
1 change: 0 additions & 1 deletion .xo-config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ module.exports = {
rules: {
'ava/no-ignored-test-files': 'off',
'ava/no-only-test': 'off',
'unicorn/prefer-module': 'off',
},
},
{
Expand Down
10 changes: 0 additions & 10 deletions docs/05-command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,6 @@ test('moo will also run', t => {
test.only('boo will run but not exclusively', t => {
t.pass();
});

// Won't run, no title
test(function (t) {
t.fail();
});

// Won't run, no explicit title
test(function foo(t) {
t.fail();
});
```

## Running tests at specific line numbers
Expand Down
24 changes: 17 additions & 7 deletions docs/recipes/endpoint-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ Translations: [Español](https://github.com/avajs/ava-docs/blob/main/es_ES/docs/

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

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).
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).

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:

```js
import http from 'node:http';
import {createServer} from 'node:http';

import {listen} from 'async-listen';
import test from 'ava';
import got from 'got';
import listen from 'test-listen';
import app from '../app';
import ky, {HTTPError} from 'ky';

import app from './app.js';

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

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

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

t.is(email, '[email protected]');
});

test.serial('404', async t => {
await t.throwsAsync(
ky('password', {prefixUrl: t.context.prefixUrl}),
{message: /Request failed with status code 404 Not Found/, instanceOf: HTTPError},
);
});
```

Other libraries you may find useful:
Expand Down
6 changes: 2 additions & 4 deletions examples/endpoint-testing/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

module.exports = (request, response) => {
export default function app(request, response) {
if (request.url === '/user') {
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify({email: '[email protected]'}));
} else {
response.writeHead('404');
response.end();
}
};
}
9 changes: 5 additions & 4 deletions examples/endpoint-testing/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "ava-endpoint-testing",
"description": "Example for endpoint testing",
"type": "module",
"scripts": {
"test": "ava"
"test": "ava test.js"
},
"devDependencies": {
"ava": "^3.15.0",
"got": "^11.8.2",
"test-listen": "^1.1.0"
"ava": "^6.1.3",
"ky": "^1.4.0",
"async-listen": "^3.0.1"
}
}
22 changes: 14 additions & 8 deletions examples/endpoint-testing/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
'use strict';
const http = require('node:http');
import {createServer} from 'node:http';

const test = require('ava');
const got = require('got');
const listen = require('test-listen');
import {listen} from 'async-listen';
import test from 'ava';
import ky, {HTTPError} from 'ky';

const app = require('./app.js');
import app from './app.js';

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

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

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

t.is(email, '[email protected]');
});

test.serial('404', async t => {
await t.throwsAsync(
ky('password', {prefixUrl: t.context.prefixUrl}),
{message: /Request failed with status code 404 Not Found/, instanceOf: HTTPError},
);
});
4 changes: 3 additions & 1 deletion examples/macros/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
exports.sum = (a, b) => a + b;
export function sum(a, b) {
return a + b;
}
5 changes: 3 additions & 2 deletions examples/macros/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "ava-macros",
"description": "Example for reusing test logic through macros",
"type": "module",
"scripts": {
"test": "ava"
"test": "ava test.js"
},
"devDependencies": {
"ava": "^3.15.0"
"ava": "^6.1.3"
}
}
15 changes: 8 additions & 7 deletions examples/macros/test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const test = require('ava');
import test from 'ava';

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

function macro(t, a, b, expected) {
t.is(sum(a, b), expected);
}

macro.title = (providedTitle, a, b, expected) => `${providedTitle ?? ''} ${a}+${b} = ${expected}`.trim();
const macro = test.macro({
exec(t, a, b, expected) {
t.is(sum(a, b), expected);
},
title: (providedTitle, a, b, expected) => `${providedTitle ?? ''} ${a}+${b} = ${expected}`.trim(),
});

test(macro, 2, 2, 4);
test(macro, 3, 3, 6);
Expand Down
5 changes: 3 additions & 2 deletions examples/matching-titles/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "ava-matching-titles",
"description": "Example for running tests with matching titles",
"type": "module",
"scripts": {
"test": "ava --match='*oo*'"
"test": "ava test.js --match='*oo*'"
},
"devDependencies": {
"ava": "^3.15.0"
"ava": "^6.1.3"
}
}
3 changes: 1 addition & 2 deletions examples/matching-titles/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const test = require('ava');
import test from 'ava';

test('foo will run', t => {
t.pass();
Expand Down
3 changes: 2 additions & 1 deletion examples/specific-line-numbers/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "ava-specific-line-numbers",
"description": "Example for running tests at specific line numbers",
"type": "module",
"scripts": {
"test": "ava test.js:5"
},
"devDependencies": {
"ava": "^3.15.0"
"ava": "^6.1.3"
}
}
3 changes: 1 addition & 2 deletions examples/specific-line-numbers/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const test = require('ava');
import test from 'ava';

test('unicorn', t => {
t.pass();
Expand Down
5 changes: 3 additions & 2 deletions examples/tap-reporter/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "ava-tap-reporter",
"description": "Example for a custom TAP reporter",
"type": "module",
"scripts": {
"test": "ava --tap | tap-nyan"
"test": "ava test.js --tap | tap-nyan"
},
"devDependencies": {
"ava": "^3.15.0",
"ava": "^6.1.3",
"tap-nyan": "^1.1.0"
}
}
2 changes: 1 addition & 1 deletion examples/tap-reporter/readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Running tests at specific line numbers

> Example for a [custom TAP reporter](https://github.com/avajs/ava/blob/main/docs/05-command-line.md#running-tests-at-specific-line-numbers)
> Example for a [custom TAP reporter](https://github.com/avajs/ava/blob/main/docs/05-command-line.md#tap-reporter)

[![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)
3 changes: 1 addition & 2 deletions examples/tap-reporter/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const test = require('ava');
import test from 'ava';

test('unicorn', t => {
t.pass();
Expand Down
40 changes: 17 additions & 23 deletions examples/timeouts/index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
'use strict';

const delay = ms => new Promise(resolve => {
setTimeout(resolve, ms);
});

exports.fetchUsers = async () => {
export async function fetchUsers() {
await delay(50);

return [
{
id: 1,
firstName: 'Ava',
name: 'Rocks',
email: '[email protected]',
},
];
};
return [{
id: 1,
firstName: 'Ava',
name: 'Rocks',
email: '[email protected]',
}];
}

exports.fetchPosts = async userId => {
export async function fetchPosts(userId) {
await delay(200);

return [
{
id: 1,
userId,
message: 'AVA Rocks 🚀',
},
];
};
return [{
id: 1,
userId,
message: 'AVA Rocks 🚀',
}];
}

exports.createPost = async message => {
export async function createPost(message) {
await delay(3000);

return {
id: 2,
userId: 1,
message,
};
};
}
5 changes: 3 additions & 2 deletions examples/timeouts/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "ava-timeouts",
"description": "Example for test timeouts",
"type": "module",
"scripts": {
"test": "ava --timeout=2s --verbose"
"test": "ava test.js --timeout=2s --verbose"
},
"devDependencies": {
"ava": "^3.15.0"
"ava": "^6.1.3"
}
}
31 changes: 13 additions & 18 deletions examples/timeouts/test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
'use strict';
const test = require('ava');
import test from 'ava';

const {fetchUsers, fetchPosts, createPost} = require('./index.js');
import {fetchUsers, fetchPosts, createPost} from './index.js';

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

const users = await fetchUsers();

t.deepEqual(users, [
{
id: 1,
firstName: 'Ava',
name: 'Rocks',
email: '[email protected]',
},
]);
t.deepEqual(users, [{
id: 1,
firstName: 'Ava',
name: 'Rocks',
email: '[email protected]',
}]);
});

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

const posts = await fetchPosts(1);

t.deepEqual(posts, [
{
id: 1,
userId: 1,
message: 'AVA Rocks 🚀',
},
]);
t.deepEqual(posts, [{
id: 1,
userId: 1,
message: 'AVA Rocks 🚀',
}]);
});

test('create post', async t => {
Expand Down
1 change: 1 addition & 0 deletions examples/typescript-basic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
Loading
Loading