Skip to content

Commit 4d3e220

Browse files
Add StackBlitz example for macros
1 parent 074373c commit 4d3e220

File tree

6 files changed

+33
-0
lines changed

6 files changed

+33
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/node_modules/
44
/test-tap/**/node_modules/
55
/test/**/fixtures/**/node_modules/*/
6+
/examples/**/node_modules/

docs/01-writing-tests.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ console.log('Test file currently being run:', test.meta.file);
290290

291291
## Reusing test logic through macros
292292

293+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/macros?file=test.js&terminal=test&view=editor)
294+
293295
Additional arguments passed to the test declaration will be passed to the test implementation. This is useful for creating reusable test macros.
294296

295297
```js

examples/macros/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.sum = (a, b) => a + b;

examples/macros/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "ava-macros",
3+
"description": "Example for reusing test logic through macros",
4+
"scripts": {
5+
"test": "ava"
6+
},
7+
"devDependencies": {
8+
"ava": "^3.15.0"
9+
}
10+
}

examples/macros/readme.md

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

examples/macros/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
const test = require('ava');
3+
4+
const {sum} = require('.');
5+
6+
function macro(t, a, b, expected) {
7+
t.is(sum(a, b), expected);
8+
}
9+
10+
macro.title = (providedTitle, a, b, expected) => `${providedTitle || ''} ${a}+${b} = ${expected}`.trim();
11+
12+
test(macro, 2, 2, 4);
13+
test(macro, 3, 3, 6);
14+
test('providedTitle', macro, 4, 4, 8);

0 commit comments

Comments
 (0)