@power-doctest/markdown
A Markdown parser for power-doctest.
Features
Run doctest for Markdown's CodeBlock.
This is markdown example.
Following code is tested by power-doctest.
`js` or `javascript` CodeBlock
```js
console.log(1); // => 1
```
Install
Install with npm:
npm install @power-doctest/markdown
Usage
fixtures/example.md
:
This Is Markdown
Valid JavaScript Example:
```js
console.log("ok"); // => "ok"
```
Invalid JavaScript Example
```js
const array = [1, 2, 3];
console.log(array); // => [1, 44, 3]
```
Example test code
import fs from "fs";
import path from "path";
import assert from "assert";
import { run } from "@power-doctest/markdown";
describe("run markdown", function() {
it("is example", () => {
const markdown = fs.readFileSync(path.join(__dirname, "fixtures/example.md"), "utf-8");
return run(markdown).then(() => {
// pass
}).catch(aggregatedError => {
assert.strictEqual(aggregatedError.message, "Throw 1 error in 2 code blocks");
assert.strictEqual(aggregatedError.errors.length, 1);
const [error] = aggregatedError.errors;
assert.strictEqual(error.message, ` # default.js:4
assert.deepStrictEqual(array, [1, 44, 3])
| |
| [1,44,3]
[1,2,3]
`)
});
});
});
Doctest Control Annotation
@power-doctest/markdown
support Doctest Control Annotation as HTML comments.
<!-- doctest:disable -->
Disable Doctest: Disable doctest for the codeblock.
This code block is not evaluated.
<!-- doctest:disable -->
```js
console.log(true); // => "not eval"
```
<!-- doctest:*Error -->
Expected error: If the error is not match the result, throw error.
<!-- doctest:SyntaxError -->
```js
++++++++;
```
<!-- doctest:options:{ ... } -->
Doctest options:Pass options
to @power-doctest/markdown
.
The inline options is preferred constructor options.
<!-- doctest:options:{ "runMode": "any" } -->
```js
if (1 === 1) {
console.log(1); // => 1
} else{
console.log(2); // => 2
}
```
<!-- doctest:meta:{ ... } -->
Metadata: It is useful for testing original behavior.
Attach Metadata to error
<!-- doctest:meta:{ "ECMAScript": 2020 } -->
```javascript
typeof 123n; // => "bigint"
```
And errors
include meta
property
{
"message": "Throw 1 error in 1 code blocks",
"errors": [
{
"meta": {
"ECMAScript": 2020
},
"message": "Identifier directly after number (1:10)"
}
]
}
Changelog
See Releases page.
Running tests
Install devDependencies and Run npm test
:
npm test
Contributing
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Author
License
MIT © azu