Skip to content

Commit

Permalink
Add StackBlitz example for macros
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored Jun 30, 2021
1 parent 074373c commit 4d3e220
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/node_modules/
/test-tap/**/node_modules/
/test/**/fixtures/**/node_modules/*/
/examples/**/node_modules/
2 changes: 2 additions & 0 deletions docs/01-writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ console.log('Test file currently being run:', test.meta.file);

## Reusing test logic through macros

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

Additional arguments passed to the test declaration will be passed to the test implementation. This is useful for creating reusable test macros.

```js
Expand Down
1 change: 1 addition & 0 deletions examples/macros/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.sum = (a, b) => a + b;
10 changes: 10 additions & 0 deletions examples/macros/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "ava-macros",
"description": "Example for reusing test logic through macros",
"scripts": {
"test": "ava"
},
"devDependencies": {
"ava": "^3.15.0"
}
}
5 changes: 5 additions & 0 deletions examples/macros/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Macros

> Example for [reusing test logic through macros](https://github.com/avajs/ava/blob/main/docs/01-writing-tests.md#reusing-test-logic-through-macros)
[![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)
14 changes: 14 additions & 0 deletions examples/macros/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
const test = require('ava');

const {sum} = require('.');

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

macro.title = (providedTitle, a, b, expected) => `${providedTitle || ''} ${a}+${b} = ${expected}`.trim();

test(macro, 2, 2, 4);
test(macro, 3, 3, 6);
test('providedTitle', macro, 4, 4, 8);

0 comments on commit 4d3e220

Please sign in to comment.