Importing Examples From Tests
The acquit-require plugin
handles dereferencing examples from your HTML or markdown. Drop in
[require:regexp$]
and the plugin will replace that with the
first test case it can find that ends with 'regexp'.
Given the below sample.md
:
Printing "Hello, World" in JavaScript is easy:
```
[require:bar]
```
This is how you print "Bye!" instead:
```
[require:baz]
```
And the below example.js
test file:
describe('foo', function() {
/* This is how you print "Hello, World!" in JavaScript */
it('bar', function() {
console.log('Hello, World!');
});
// You can print any string
it('baz', function() {
console.log('Bye!');
});
});
The below code will replace [require:bar]
and [require:baz]
with the
code of the corresponding tests from example.js
.
const fs = require('fs');
const transform = require('acquit-require');
const markdown = fs.readFileSync('./test/sample.md').toString();
const examples = fs.readFileSync('./test/example.js').toString();
const output = transform(markdown, acquit.parse(examples));
// `[require:foo]` and `[require:bar]` are replaced with the
// corresponding tests in `example.js`
console.log(output);
Below is the transformed markdown.
Printing "Hello, World" in JavaScript is easy:
```
console.log('Hello, World!');
```
This is how you print "Bye!" instead:
```
console.log('Bye!');
```
Transpiling Tests into Markdown
The acquit-markdown plugin lets you
compile your existing tests into markdown. If you already have
well-commented tests, this plugin will convert those into a GitHub
README.md
for you. The acquit-markdown plugin uses itself to generate its own README
.
You can use acquit-markdown from Node.js, or as an executable from the command line. To install:
npm install acquit acquit-markdown
Say you have a test file example.js
as shown below.
describe('foo', function() {
/* This is how you print "Hello, World!" in JavaScript */
it('bar', function() {
console.log('Hello, World!');
});
// You can print any string
it('baz', function() {
console.log('Bye!');
});
});
You can run the acquit-markdown
executable to compile this test
file into markdown.
./node_modules/.bin/acquit-markdown -p ./test/example.js > README.md
README.md
will now contain the below markdown.
# foo
## It bar
This is how you print "Hello, World!" in JavaScript
```javascript
console.log('Hello, World!');
```
## It baz
You can print any string
```javascript
console.log('Bye!');
```