Skip to content

Commit f798cd8

Browse files
committedJan 11, 2019
Refactor tests using ava snapshot tests
1 parent 799ffe8 commit f798cd8

File tree

6 files changed

+108
-40
lines changed

6 files changed

+108
-40
lines changed
 

‎test/assets/index.js

-1
This file was deleted.

‎test/assets/simple-markdown.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# heading 1
2+
3+
- buy pineapple
4+
5+
## heading 2
6+
7+
_italic_ is the new __bold__
8+
9+
| name | type |
10+
| --- | --- |
File renamed without changes.

‎test/index.test.js

+48-39
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,58 @@
33
import test from "ava";
44
import webpack from "webpack";
55
import path from "path";
6-
import fs from "fs";
7-
import marked from "marked";
86
import markdownOptions from "./markdown-options";
97

10-
test.cb(t => {
11-
webpack({
12-
entry: path.resolve(__dirname, "./assets/markdown.md"),
13-
mode: "development",
14-
module: {
15-
rules: [{
16-
test: /\.md$/,
17-
use: [
18-
{
19-
loader: "html-loader"
20-
},
21-
{
22-
loader: require.resolve("../index.js"),
23-
options: markdownOptions
24-
}
25-
]
26-
}]
27-
},
28-
output: {
29-
libraryTarget: "commonjs2",
30-
path: __dirname + "/output",
31-
filename: "bundle.js"
32-
}
33-
}, function onCompilationFinished(err, stats) {
34-
if (err) {
35-
return t.end(err);
36-
}
37-
if (stats.hasErrors()) {
38-
return t.end(stats.compilation.errors[0]);
39-
}
40-
if (stats.hasWarnings()) {
41-
return t.end(stats.compilation.warnings[0]);
42-
}
8+
function createBundle(markdownFile, bundleName) {
9+
return new Promise((resolve, reject) => {
10+
webpack({
11+
entry: path.resolve(__dirname, markdownFile),
12+
mode: "development",
13+
module: {
14+
rules: [{
15+
test: /\.md$/,
16+
use: [
17+
{
18+
loader: "html-loader"
19+
},
20+
{
21+
loader: require.resolve("../index.js"),
22+
options: markdownOptions
23+
}
24+
]
25+
}]
26+
},
27+
output: {
28+
libraryTarget: "commonjs2",
29+
path: __dirname + "/output",
30+
filename: bundleName
31+
}
32+
}, function onCompilationFinished(err, stats) {
33+
if (err) {
34+
return reject(err);
35+
}
36+
if (stats.hasErrors()) {
37+
return reject(stats.compilation.errors[0]);
38+
}
39+
if (stats.hasWarnings()) {
40+
return reject(stats.compilation.warnings[0]);
41+
}
42+
43+
resolve(require(`./output/${bundleName}`));
44+
});
45+
});
46+
}
4347

44-
const bundle = require("./output/bundle");
45-
t.is(bundle, '<h1 id=\"heading-1\">heading 1</h1>\n<ul>\n<li>buy pineapple</li>\n</ul>\n<h2 id=\"heading-2\">heading 2</h2>\n<p><em>italic</em> is the new <strong>bold</strong></p>\n<pre><code class=\"language-javascript\"><span class=\"hljs-attribute\">const i</span> = 100;</code></pre>\n<table>\n<thead>\n<tr>\n<th>name</th>\n<th>type</th>\n</tr>\n</thead>\n<tbody><tr>\n<td>key</td>\n<td><code>string|number</code></td>\n</tr>\n</tbody></table>\n');
48+
test("plain markdown", async (t) => {
49+
t.plan(1);
50+
const bundle = await createBundle("./assets/simple-markdown.md", "simpleMarkdown.js");
51+
t.snapshot(bundle);
52+
});
4653

47-
t.end();
48-
});
54+
test("with code", async (t) => {
55+
t.plan(1);
56+
const bundle = await createBundle("./assets/with-code.md", "withCode.js");
57+
t.snapshot(bundle);
4958
});
5059

5160

‎test/snapshots/index.test.js.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Snapshot report for `test/index.test.js`
2+
3+
The actual snapshot is saved in `index.test.js.snap`.
4+
5+
Generated by [AVA](https://ava.li).
6+
7+
## plain markdown
8+
9+
> Snapshot 1
10+
11+
`<h1 id="heading-1">heading 1</h1>␊
12+
<ul>␊
13+
<li>buy pineapple</li>␊
14+
</ul>␊
15+
<h2 id="heading-2">heading 2</h2>␊
16+
<p><em>italic</em> is the new <strong>bold</strong></p>␊
17+
<table>␊
18+
<thead>␊
19+
<tr>␊
20+
<th>name</th>␊
21+
<th>type</th>␊
22+
</tr>␊
23+
</thead>␊
24+
</table>␊
25+
`
26+
27+
## with code
28+
29+
> Snapshot 1
30+
31+
`<h1 id="heading-1">heading 1</h1>␊
32+
<ul>␊
33+
<li>buy pineapple</li>␊
34+
</ul>␊
35+
<h2 id="heading-2">heading 2</h2>␊
36+
<p><em>italic</em> is the new <strong>bold</strong></p>␊
37+
<pre><code class="language-javascript"><span class="hljs-keyword">const</span> i = <span class="hljs-number">100</span>;</code></pre>␊
38+
<table>␊
39+
<thead>␊
40+
<tr>␊
41+
<th>name</th>␊
42+
<th>type</th>␊
43+
</tr>␊
44+
</thead>␊
45+
<tbody><tr>␊
46+
<td>key</td>␊
47+
<td><code>string|number</code></td>␊
48+
</tr>␊
49+
</tbody></table>␊
50+
`

‎test/snapshots/index.test.js.snap

401 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.