Skip to content

Commit 8224306

Browse files
committed
Add first tests
Ref #623
1 parent 1cf6549 commit 8224306

File tree

16 files changed

+243
-1
lines changed

16 files changed

+243
-1
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"env": {
33
"node": true,
4+
"mocha": true,
45
"browser": true
56
},
67
"rules": {

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
/client/live.bundle.js
33
/client/index.bundle.js
44
/client/sockjs.bundle.js
5+
/coverage

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
"css-loader": "~0.25.0",
2626
"eslint": "^3.4.0",
2727
"file-loader": "~0.9.0",
28+
"istanbul": "^0.4.5",
2829
"jquery": "^2.2.0",
2930
"less": "^2.5.1",
3031
"less-loader": "~2.2.0",
32+
"mocha": "^3.0.2",
3133
"pug": "^2.0.0-beta5",
3234
"pug-loader": "^2.3.0",
3335
"style-loader": "~0.13.0",
36+
"supertest": "^2.0.0",
3437
"url-loader": "~0.5.6",
3538
"webpack": "^2.1.0-beta.1"
3639
},
@@ -55,6 +58,9 @@
5558
"client-sockjs": "webpack ./client/sockjs.js client/sockjs.bundle.js --color --config client/webpack.sockjs.config.js -p",
5659
"lint": "eslint bin lib test examples client/{index,live,socket,sockjs,webpack.config}.js",
5760
"beautify": "npm run lint -- --fix",
58-
"travis": "npm run lint && node lib/Server.js"
61+
"test": "mocha --full-trace --check-leaks",
62+
"posttest": "npm run -s lint",
63+
"cover": "istanbul cover node_modules/mocha/bin/_mocha",
64+
"travis": "npm run cover -- --report lcovonly && npm run lint"
5965
}
6066
}

test/Compress.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var request = require("supertest");
2+
var helper = require("./helper");
3+
var config = require("./fixtures/simple-config/webpack.config");
4+
5+
describe("Compress", function() {
6+
var server;
7+
var req;
8+
9+
before(function(done) {
10+
server = helper.start(config, {
11+
quiet: true,
12+
compress: true
13+
}, done);
14+
req = request(server.app);
15+
});
16+
17+
after(helper.close);
18+
19+
it("request to bundle file", function(done) {
20+
req.get("/bundle.js")
21+
.expect("Content-Encoding", "gzip")
22+
.expect(200, done);
23+
});
24+
});

test/ContentBase.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
var request = require("supertest");
2+
var path = require("path");
3+
var helper = require("./helper");
4+
var config = require("./fixtures/contentbase-config/webpack.config");
5+
6+
var contentBasePublic = path.join(__dirname, "fixtures/contentbase-config/public");
7+
var contentBaseOther = path.join(__dirname, "fixtures/contentbase-config/other");
8+
9+
describe("ContentBase", function() {
10+
var server;
11+
var req;
12+
afterEach(helper.close);
13+
14+
describe("to directory", function() {
15+
before(function(done) {
16+
server = helper.start(config, {
17+
quiet: true,
18+
contentBase: contentBasePublic,
19+
}, done);
20+
req = request(server.app);
21+
});
22+
23+
it("Request to index", function(done) {
24+
req.get("/")
25+
.expect(200, /Heyo/, done);
26+
});
27+
28+
it("Request to other file", function(done) {
29+
req.get("/other.html")
30+
.expect(200, /Other html/, done);
31+
});
32+
});
33+
34+
describe("to directories", function() {
35+
before(function(done) {
36+
server = helper.start(config, {
37+
quiet: true,
38+
contentBase: [contentBasePublic, contentBaseOther],
39+
}, done);
40+
req = request(server.app);
41+
});
42+
43+
it("Request to first directory", function(done) {
44+
req.get("/")
45+
.expect(200, /Heyo/, done);
46+
});
47+
48+
it("Request to second directory", function(done) {
49+
req.get("/foo.html")
50+
.expect(200, /Foo!/, done);
51+
});
52+
});
53+
54+
describe("to port", function() {
55+
before(function(done) {
56+
server = helper.start(config, {
57+
quiet: true,
58+
contentBase: 9099999,
59+
}, done);
60+
req = request(server.app);
61+
});
62+
63+
it("Request to page", function(done) {
64+
req.get("/other.html")
65+
.expect("Location", "//localhost:9099999/other.html")
66+
.expect(302, done);
67+
});
68+
});
69+
70+
describe("to external url", function() {
71+
before(function(done) {
72+
server = helper.start(config, {
73+
quiet: true,
74+
contentBase: "http://example.com/",
75+
}, done);
76+
req = request(server.app);
77+
});
78+
79+
it("Request to page", function(done) {
80+
req.get("/foo.html")
81+
// TODO: hmm, two slashes seems to be a bug?
82+
.expect("Location", "http://example.com//foo.html")
83+
.expect(302, done);
84+
});
85+
86+
it("Request to page with search params", function(done) {
87+
req.get("/foo.html?space=ship")
88+
// TODO: hmm, two slashes seems to be a bug?
89+
.expect("Location", "http://example.com//foo.html?space=ship")
90+
.expect(302, done);
91+
});
92+
});
93+
});

test/Routes.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var request = require("supertest");
2+
var fs = require("fs");
3+
var path = require("path");
4+
var helper = require("./helper");
5+
var config = require("./fixtures/simple-config/webpack.config");
6+
7+
var directoryIndex = fs.readFileSync(path.join(__dirname, "fixtures/directory-index.txt"), "utf-8");
8+
var magicHtml = fs.readFileSync(path.join(__dirname, "fixtures/magic-html.txt"), "utf-8");
9+
10+
describe("Routes", function() {
11+
var server;
12+
var req;
13+
14+
before(function(done) {
15+
server = helper.start(config, {
16+
quiet: true,
17+
headers: { "X-Foo": "1" }
18+
}, done);
19+
req = request(server.app);
20+
});
21+
22+
after(helper.close);
23+
24+
it("GET request to inline bundle", function(done) {
25+
req.get("/webpack-dev-server.js")
26+
.expect("Content-Type", "application/javascript")
27+
.expect(200, done);
28+
});
29+
30+
it("GET request to live bundle", function(done) {
31+
req.get("/__webpack_dev_server__/live.bundle.js")
32+
.expect("Content-Type", "application/javascript")
33+
.expect(200, done);
34+
});
35+
36+
it("GET request to sockjs bundle", function(done) {
37+
req.get("/__webpack_dev_server__/sockjs.bundle.js")
38+
.expect("Content-Type", "application/javascript")
39+
.expect(200, done);
40+
});
41+
42+
it("GET request to live html", function(done) {
43+
req.get("/webpack-dev-server/")
44+
.expect("Content-Type", "text/html")
45+
.expect(200, /__webpack_dev_server__/, done);
46+
});
47+
48+
it("GET request to directory index", function(done) {
49+
req.get("/webpack-dev-server")
50+
.expect("Content-Type", "text/html")
51+
.expect(200, directoryIndex.trim(), done);
52+
});
53+
54+
it("GET request to magic html", function(done) {
55+
req.get("/bundle")
56+
.expect(200, magicHtml.trim(), done);
57+
});
58+
59+
it("GET request with headers", function(done) {
60+
req.get("/bundle")
61+
.expect("X-Foo", "1")
62+
.expect(200, done);
63+
});
64+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require("./index.html");
2+
3+
console.log("Hey.");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Foo!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Heyo.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Other html

0 commit comments

Comments
 (0)