diff --git a/.gitignore b/.gitignore index 1f8ca5804..1d0a1618c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /test-tap/**/node_modules/ /test/**/fixtures/**/node_modules/*/ /examples/**/node_modules/ +/examples/**/package-lock.json diff --git a/.xo-config.cjs b/.xo-config.cjs index 4a84a1a58..acc03e8c1 100644 --- a/.xo-config.cjs +++ b/.xo-config.cjs @@ -43,7 +43,6 @@ module.exports = { rules: { 'ava/no-ignored-test-files': 'off', 'ava/no-only-test': 'off', - 'unicorn/prefer-module': 'off', }, }, { diff --git a/docs/05-command-line.md b/docs/05-command-line.md index 3154b52a1..a92ed2654 100644 --- a/docs/05-command-line.md +++ b/docs/05-command-line.md @@ -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 diff --git a/docs/recipes/endpoint-testing.md b/docs/recipes/endpoint-testing.md index 5eaa08394..bd5d18432 100644 --- a/docs/recipes/endpoint-testing.md +++ b/docs/recipes/endpoint-testing.md @@ -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); }); @@ -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, 'ava@rocks.com'); }); + +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: diff --git a/examples/endpoint-testing/app.js b/examples/endpoint-testing/app.js index f9f715df6..4c7eb75c6 100644 --- a/examples/endpoint-testing/app.js +++ b/examples/endpoint-testing/app.js @@ -1,6 +1,4 @@ -'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: 'ava@rocks.com'})); @@ -8,4 +6,4 @@ module.exports = (request, response) => { response.writeHead('404'); response.end(); } -}; +} diff --git a/examples/endpoint-testing/package.json b/examples/endpoint-testing/package.json index c1003774c..794d41ddc 100644 --- a/examples/endpoint-testing/package.json +++ b/examples/endpoint-testing/package.json @@ -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" } } diff --git a/examples/endpoint-testing/test.js b/examples/endpoint-testing/test.js index 413e68ccc..76d7bddc8 100644 --- a/examples/endpoint-testing/test.js +++ b/examples/endpoint-testing/test.js @@ -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); }); @@ -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, 'ava@rocks.com'); }); + +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}, + ); +}); diff --git a/examples/macros/index.js b/examples/macros/index.js index a5a11214b..c17afd446 100644 --- a/examples/macros/index.js +++ b/examples/macros/index.js @@ -1 +1,3 @@ -exports.sum = (a, b) => a + b; +export function sum(a, b) { + return a + b; +} diff --git a/examples/macros/package.json b/examples/macros/package.json index 44e851513..8dc675379 100644 --- a/examples/macros/package.json +++ b/examples/macros/package.json @@ -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" } } diff --git a/examples/macros/test.js b/examples/macros/test.js index 4b3648dde..b53d814eb 100644 --- a/examples/macros/test.js +++ b/examples/macros/test.js @@ -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); diff --git a/examples/matching-titles/package.json b/examples/matching-titles/package.json index 41ccb750a..bc0fd3e85 100644 --- a/examples/matching-titles/package.json +++ b/examples/matching-titles/package.json @@ -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" } } diff --git a/examples/matching-titles/test.js b/examples/matching-titles/test.js index 4c6cbd96b..c5672fd0c 100644 --- a/examples/matching-titles/test.js +++ b/examples/matching-titles/test.js @@ -1,5 +1,4 @@ -'use strict'; -const test = require('ava'); +import test from 'ava'; test('foo will run', t => { t.pass(); diff --git a/examples/specific-line-numbers/package.json b/examples/specific-line-numbers/package.json index 9aef3692c..347a3079d 100644 --- a/examples/specific-line-numbers/package.json +++ b/examples/specific-line-numbers/package.json @@ -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" } } diff --git a/examples/specific-line-numbers/test.js b/examples/specific-line-numbers/test.js index 56f6991d2..4c4977204 100644 --- a/examples/specific-line-numbers/test.js +++ b/examples/specific-line-numbers/test.js @@ -1,5 +1,4 @@ -'use strict'; -const test = require('ava'); +import test from 'ava'; test('unicorn', t => { t.pass(); diff --git a/examples/tap-reporter/package.json b/examples/tap-reporter/package.json index 38c62a14e..c107e57eb 100644 --- a/examples/tap-reporter/package.json +++ b/examples/tap-reporter/package.json @@ -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" } } diff --git a/examples/tap-reporter/readme.md b/examples/tap-reporter/readme.md index 816f0c5e3..c276727d3 100644 --- a/examples/tap-reporter/readme.md +++ b/examples/tap-reporter/readme.md @@ -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) diff --git a/examples/tap-reporter/test.js b/examples/tap-reporter/test.js index 224061b89..a6b1bb9b3 100644 --- a/examples/tap-reporter/test.js +++ b/examples/tap-reporter/test.js @@ -1,5 +1,4 @@ -'use strict'; -const test = require('ava'); +import test from 'ava'; test('unicorn', t => { t.pass(); diff --git a/examples/timeouts/index.js b/examples/timeouts/index.js index 92709dd7b..2ba9a3648 100644 --- a/examples/timeouts/index.js +++ b/examples/timeouts/index.js @@ -1,35 +1,29 @@ -'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: 'ava@rocks.com', - }, - ]; -}; + return [{ + id: 1, + firstName: 'Ava', + name: 'Rocks', + email: 'ava@rocks.com', + }]; +} -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 { @@ -37,4 +31,4 @@ exports.createPost = async message => { userId: 1, message, }; -}; +} diff --git a/examples/timeouts/package.json b/examples/timeouts/package.json index ec288c771..50bb66fa7 100644 --- a/examples/timeouts/package.json +++ b/examples/timeouts/package.json @@ -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" } } diff --git a/examples/timeouts/test.js b/examples/timeouts/test.js index f16945c10..5f67fbd8e 100644 --- a/examples/timeouts/test.js +++ b/examples/timeouts/test.js @@ -1,21 +1,18 @@ -'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: 'ava@rocks.com', - }, - ]); + t.deepEqual(users, [{ + id: 1, + firstName: 'Ava', + name: 'Rocks', + email: 'ava@rocks.com', + }]); }); test('retrieve posts', async t => { @@ -23,13 +20,11 @@ test('retrieve posts', async t => { 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 => { diff --git a/examples/typescript-basic/.gitignore b/examples/typescript-basic/.gitignore new file mode 100644 index 000000000..378eac25d --- /dev/null +++ b/examples/typescript-basic/.gitignore @@ -0,0 +1 @@ +build diff --git a/examples/typescript-basic/package.json b/examples/typescript-basic/package.json index ad85254fc..af1ab949d 100644 --- a/examples/typescript-basic/package.json +++ b/examples/typescript-basic/package.json @@ -1,14 +1,15 @@ { "name": "ava-typescript-basic", "description": "Basic example for AVA with TypeScript", + "type": "module", "scripts": { "test": "ava" }, "devDependencies": { - "@ava/typescript": "^2.0.0", - "@sindresorhus/tsconfig": "^1.0.2", - "ava": "^3.15.0", - "typescript": "^4.3.4" + "@ava/typescript": "^5.0.0", + "@sindresorhus/tsconfig": "^6.0.0", + "ava": "^6.1.3", + "typescript": "~5.5.3" }, "ava": { "typescript": { diff --git a/examples/typescript-basic/source/test.ts b/examples/typescript-basic/source/test.ts index 339a2321a..aa9bead68 100644 --- a/examples/typescript-basic/source/test.ts +++ b/examples/typescript-basic/source/test.ts @@ -1,6 +1,6 @@ import test from 'ava'; -import {sum, subtract} from '.'; +import {sum, subtract} from './index.js'; test('sum', t => { t.is(sum(0, 0), 0); diff --git a/examples/typescript-basic/tsconfig.json b/examples/typescript-basic/tsconfig.json index 60833d016..7b7de8c66 100644 --- a/examples/typescript-basic/tsconfig.json +++ b/examples/typescript-basic/tsconfig.json @@ -1,7 +1,6 @@ { "extends": "@sindresorhus/tsconfig", "compilerOptions": { - "module": "commonjs", "outDir": "build", } } diff --git a/examples/typescript-context/.gitignore b/examples/typescript-context/.gitignore new file mode 100644 index 000000000..378eac25d --- /dev/null +++ b/examples/typescript-context/.gitignore @@ -0,0 +1 @@ +build diff --git a/examples/typescript-context/package.json b/examples/typescript-context/package.json index f577a30ec..9e0f89cf9 100644 --- a/examples/typescript-context/package.json +++ b/examples/typescript-context/package.json @@ -1,14 +1,15 @@ { "name": "ava-typescript-context", "description": "TypeScript example for typing t.context", + "type": "module", "scripts": { "test": "ava" }, "devDependencies": { - "@ava/typescript": "^2.0.0", - "@sindresorhus/tsconfig": "^1.0.2", - "ava": "^3.15.0", - "typescript": "^4.3.4" + "@ava/typescript": "^5.0.0", + "@sindresorhus/tsconfig": "^6.0.0", + "ava": "^6.1.3", + "typescript": "~5.5.3" }, "ava": { "typescript": { diff --git a/examples/typescript-context/source/test.ts b/examples/typescript-context/source/test.ts index feccdaac4..b73a4ee3b 100644 --- a/examples/typescript-context/source/test.ts +++ b/examples/typescript-context/source/test.ts @@ -1,10 +1,10 @@ -import anyTest, {TestInterface} from 'ava'; +import anyTest, {type TestFn} from 'ava'; -import {concat} from '.'; +import {concat} from './index.js'; -const test = anyTest as TestInterface<{sort: (a: string, b: string) => number}>; +const test = anyTest as TestFn<{sort: (a: string, b: string) => number}>; -test.beforeEach(t => { +test.before(t => { t.context = { sort: (a: string, b: string) => a.localeCompare(b) }; diff --git a/examples/typescript-context/tsconfig.json b/examples/typescript-context/tsconfig.json index 60833d016..7b7de8c66 100644 --- a/examples/typescript-context/tsconfig.json +++ b/examples/typescript-context/tsconfig.json @@ -1,7 +1,6 @@ { "extends": "@sindresorhus/tsconfig", "compilerOptions": { - "module": "commonjs", "outDir": "build", } } diff --git a/examples/typescript-loader/.gitignore b/examples/typescript-loader/.gitignore new file mode 100644 index 000000000..6626d2979 --- /dev/null +++ b/examples/typescript-loader/.gitignore @@ -0,0 +1 @@ +.tsimp diff --git a/examples/typescript-loader/index.ts b/examples/typescript-loader/index.ts new file mode 100644 index 000000000..45b80413c --- /dev/null +++ b/examples/typescript-loader/index.ts @@ -0,0 +1 @@ +export const multiply = (a: number, b: number) => a * b; diff --git a/examples/typescript-loader/package.json b/examples/typescript-loader/package.json new file mode 100644 index 000000000..686626c6c --- /dev/null +++ b/examples/typescript-loader/package.json @@ -0,0 +1,22 @@ +{ + "name": "ava-typescript-context", + "description": "TypeScript example for typing t.context", + "type": "module", + "scripts": { + "test": "ava" + }, + "devDependencies": { + "@sindresorhus/tsconfig": "^6.0.0", + "ava": "^6.1.3", + "tsimp": "^2.0.11", + "typescript": "~5.5.3" + }, + "ava": { + "extensions": { + "ts": "module" + }, + "nodeArguments": [ + "--import=tsimp" + ] + } +} diff --git a/examples/typescript-loader/readme.md b/examples/typescript-loader/readme.md new file mode 100644 index 000000000..9f395bdd6 --- /dev/null +++ b/examples/typescript-loader/readme.md @@ -0,0 +1,5 @@ +# TypeScript loader example + +> TypeScript example for configuring AVA with [ESM loaders](https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md#enabling-avas-support-for-typescript-test-files) + +[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-loader?file=test.ts&terminal=test&view=editor) diff --git a/examples/typescript-loader/test.ts b/examples/typescript-loader/test.ts new file mode 100644 index 000000000..a7dedf718 --- /dev/null +++ b/examples/typescript-loader/test.ts @@ -0,0 +1,7 @@ +import test from 'ava'; +import {multiply} from './index.js'; + +test('multiply', t => { + t.is(multiply(1, 0), 0); + t.is(multiply(2, 3), 6); +}); diff --git a/examples/typescript-loader/tsconfig.json b/examples/typescript-loader/tsconfig.json new file mode 100644 index 000000000..e49282769 --- /dev/null +++ b/examples/typescript-loader/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@sindresorhus/tsconfig" +}