From 4d3e22038809fec40b2a9ba11a6b2b9d85b58f49 Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Wed, 30 Jun 2021 15:26:16 +0200 Subject: [PATCH] Add StackBlitz example for macros --- .gitignore | 1 + docs/01-writing-tests.md | 2 ++ examples/macros/index.js | 1 + examples/macros/package.json | 10 ++++++++++ examples/macros/readme.md | 5 +++++ examples/macros/test.js | 14 ++++++++++++++ 6 files changed, 33 insertions(+) create mode 100644 examples/macros/index.js create mode 100644 examples/macros/package.json create mode 100644 examples/macros/readme.md create mode 100644 examples/macros/test.js diff --git a/.gitignore b/.gitignore index 20a3cc178..d4bc6925c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /node_modules/ /test-tap/**/node_modules/ /test/**/fixtures/**/node_modules/*/ +/examples/**/node_modules/ diff --git a/docs/01-writing-tests.md b/docs/01-writing-tests.md index c6aae5fad..08b818cf4 100644 --- a/docs/01-writing-tests.md +++ b/docs/01-writing-tests.md @@ -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 diff --git a/examples/macros/index.js b/examples/macros/index.js new file mode 100644 index 000000000..a5a11214b --- /dev/null +++ b/examples/macros/index.js @@ -0,0 +1 @@ +exports.sum = (a, b) => a + b; diff --git a/examples/macros/package.json b/examples/macros/package.json new file mode 100644 index 000000000..44e851513 --- /dev/null +++ b/examples/macros/package.json @@ -0,0 +1,10 @@ +{ + "name": "ava-macros", + "description": "Example for reusing test logic through macros", + "scripts": { + "test": "ava" + }, + "devDependencies": { + "ava": "^3.15.0" + } +} diff --git a/examples/macros/readme.md b/examples/macros/readme.md new file mode 100644 index 000000000..916fcd4fe --- /dev/null +++ b/examples/macros/readme.md @@ -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) diff --git a/examples/macros/test.js b/examples/macros/test.js new file mode 100644 index 000000000..8f041cc70 --- /dev/null +++ b/examples/macros/test.js @@ -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);