Skip to content

Commit

Permalink
Refactor tests using ava snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meaku committed Jan 11, 2019
1 parent 799ffe8 commit f798cd8
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 40 deletions.
1 change: 0 additions & 1 deletion test/assets/index.js

This file was deleted.

10 changes: 10 additions & 0 deletions test/assets/simple-markdown.md
@@ -0,0 +1,10 @@
# heading 1

- buy pineapple

## heading 2

_italic_ is the new __bold__

| name | type |
| --- | --- |
File renamed without changes.
87 changes: 48 additions & 39 deletions test/index.test.js
Expand Up @@ -3,49 +3,58 @@
import test from "ava";
import webpack from "webpack";
import path from "path";
import fs from "fs";
import marked from "marked";
import markdownOptions from "./markdown-options";

test.cb(t => {
webpack({
entry: path.resolve(__dirname, "./assets/markdown.md"),
mode: "development",
module: {
rules: [{
test: /\.md$/,
use: [
{
loader: "html-loader"
},
{
loader: require.resolve("../index.js"),
options: markdownOptions
}
]
}]
},
output: {
libraryTarget: "commonjs2",
path: __dirname + "/output",
filename: "bundle.js"
}
}, function onCompilationFinished(err, stats) {
if (err) {
return t.end(err);
}
if (stats.hasErrors()) {
return t.end(stats.compilation.errors[0]);
}
if (stats.hasWarnings()) {
return t.end(stats.compilation.warnings[0]);
}
function createBundle(markdownFile, bundleName) {
return new Promise((resolve, reject) => {
webpack({
entry: path.resolve(__dirname, markdownFile),
mode: "development",
module: {
rules: [{
test: /\.md$/,
use: [
{
loader: "html-loader"
},
{
loader: require.resolve("../index.js"),
options: markdownOptions
}
]
}]
},
output: {
libraryTarget: "commonjs2",
path: __dirname + "/output",
filename: bundleName
}
}, function onCompilationFinished(err, stats) {
if (err) {
return reject(err);
}
if (stats.hasErrors()) {
return reject(stats.compilation.errors[0]);
}
if (stats.hasWarnings()) {
return reject(stats.compilation.warnings[0]);
}

resolve(require(`./output/${bundleName}`));
});
});
}

const bundle = require("./output/bundle");
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');
test("plain markdown", async (t) => {
t.plan(1);
const bundle = await createBundle("./assets/simple-markdown.md", "simpleMarkdown.js");
t.snapshot(bundle);
});

t.end();
});
test("with code", async (t) => {
t.plan(1);
const bundle = await createBundle("./assets/with-code.md", "withCode.js");
t.snapshot(bundle);
});


50 changes: 50 additions & 0 deletions test/snapshots/index.test.js.md
@@ -0,0 +1,50 @@
# Snapshot report for `test/index.test.js`

The actual snapshot is saved in `index.test.js.snap`.

Generated by [AVA](https://ava.li).

## plain markdown

> Snapshot 1
`<h1 id="heading-1">heading 1</h1>␊
<ul>␊
<li>buy pineapple</li>␊
</ul>␊
<h2 id="heading-2">heading 2</h2>␊
<p><em>italic</em> is the new <strong>bold</strong></p>␊
<table>␊
<thead>␊
<tr>␊
<th>name</th>␊
<th>type</th>␊
</tr>␊
</thead>␊
</table>␊
`

## with code

> Snapshot 1
`<h1 id="heading-1">heading 1</h1>␊
<ul>␊
<li>buy pineapple</li>␊
</ul>␊
<h2 id="heading-2">heading 2</h2>␊
<p><em>italic</em> is the new <strong>bold</strong></p>␊
<pre><code class="language-javascript"><span class="hljs-keyword">const</span> i = <span class="hljs-number">100</span>;</code></pre>␊
<table>␊
<thead>␊
<tr>␊
<th>name</th>␊
<th>type</th>␊
</tr>␊
</thead>␊
<tbody><tr>␊
<td>key</td>␊
<td><code>string|number</code></td>␊
</tr>␊
</tbody></table>␊
`
Binary file added test/snapshots/index.test.js.snap
Binary file not shown.

0 comments on commit f798cd8

Please sign in to comment.