Skip to content

Commit 272a225

Browse files
Add unit testing example
1 parent e3d592f commit 272a225

File tree

6 files changed

+141
-1
lines changed

6 files changed

+141
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ node_modules
22
.vscode
33

44
examples/*/dist
5-
examples/*/dist-play
5+
examples/*/dist-*
66

77
# yarn.lock should not be committed in libraries
88
yarn.lock

examples/03-testing/package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "vue-extract-loader.03-testing",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"build": "webpack"
8+
},
9+
"devDependencies": {
10+
"css-loader": "^0.28.2",
11+
"html-webpack-plugin": "^2.28.0",
12+
"mocha": "^3.4.1",
13+
"mocha-loader": "^1.1.1",
14+
"vue-loader": "^12.1.0",
15+
"vue-template-compiler": "^2.3.3",
16+
"webpack": "^2.5.1"
17+
},
18+
"dependencies": {
19+
"vue": "^2.3.3"
20+
}
21+
}

examples/03-testing/src/component.vue

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<template>
2+
<p class="text">{{ message }}</p>
3+
</template>
4+
5+
<script>
6+
7+
export default {
8+
data: () => ({
9+
message: "Hello World!",
10+
}),
11+
}
12+
13+
</script>
14+
15+
<style>
16+
17+
.text {
18+
color: blue;
19+
}
20+
21+
</style>
22+
23+
<unit-test>
24+
25+
import assert from "assert";
26+
27+
import Vue from "vue";
28+
import Component from "./component.vue";
29+
30+
describe("component.vue", () => {
31+
it("exports an object", () => {
32+
assert.equal(typeof Component, "object");
33+
});
34+
35+
describe("#data", () => {
36+
it("is a function", () => {
37+
assert.equal(typeof Component.data, "function");
38+
});
39+
40+
it("sets the default values", () => {
41+
assert.equal(Component.data().message, "Hello World!");
42+
});
43+
});
44+
45+
describe("rendering", () => {
46+
it("renders the correct message", () => {
47+
const Ctor = Vue.extend(Component);
48+
const el = document.createElement("div");
49+
50+
const vm = new Ctor({ el }).$mount();
51+
52+
assert.equal(vm.$el.textContent, "Hello World!");
53+
});
54+
55+
it("renders the message with the correct class", () => {
56+
const Ctor = Vue.extend(Component);
57+
const el = document.createElement("div");
58+
59+
const vm = new Ctor({ el }).$mount();
60+
61+
assert(vm.$el.classList.contains("text"));
62+
});
63+
});
64+
});
65+
66+
</unit-test>

examples/03-testing/test/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>vue-extract-loader example</title>
6+
</head>
7+
<body>
8+
<div id="mocha"></div>
9+
</body>
10+
</html>

examples/03-testing/test/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "../src/component.vue";

examples/03-testing/webpack.config.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const HtmlWebpackPlugin = require("html-webpack-plugin");
5+
6+
module.exports = {
7+
entry: "./test/index.js",
8+
output: {
9+
filename: "./bundle.js",
10+
path: path.resolve("dist-test"),
11+
},
12+
13+
resolve: {
14+
extensions: [ ".js", ".vue" ],
15+
},
16+
17+
resolveLoader: {
18+
alias: {
19+
// This is only needed in order to alias "vue-extract-loader" to
20+
// the root folder of this package.
21+
// You shouldn't do this in your project.
22+
"vue-extract-loader": require.resolve("../../"),
23+
},
24+
},
25+
26+
module: {
27+
rules: [ {
28+
test: /.vue$/,
29+
issuer: path.join(__dirname, "test/index.js"),
30+
loader: "mocha-loader!vue-extract-loader?block=unit-test",
31+
}, {
32+
test: /\.vue$/,
33+
loader: "vue-loader",
34+
} ],
35+
},
36+
37+
plugins: [
38+
new HtmlWebpackPlugin({
39+
template: require.resolve("./test/index.html"),
40+
}),
41+
],
42+
};

0 commit comments

Comments
 (0)