Skip to content

Commit 3d4425a

Browse files
committed
add more tests
1 parent 48316f4 commit 3d4425a

File tree

3 files changed

+339
-10
lines changed

3 files changed

+339
-10
lines changed

package.json

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "koa-404-handler",
3-
"description":
4-
"404 handler for Lad and Koa (best used with koa-better-error-handler)",
3+
"description": "404 handler for Lad and Koa (best used with koa-better-error-handler)",
54
"version": "0.0.1",
65
"author": "Nick Baugh <[email protected]> (http://niftylettuce.com/)",
76
"bugs": {
@@ -14,17 +13,21 @@
1413
"dependencies": {},
1514
"devDependencies": {
1615
"ava": "^0.22.0",
16+
"boom": "^7.1.1",
1717
"codecov": "^2.3.0",
1818
"cross-env": "^5.0.5",
1919
"eslint": "^4.5.0",
2020
"eslint-config-prettier": "^2.3.0",
2121
"eslint-plugin-prettier": "^2.2.0",
2222
"husky": "^0.14.3",
23+
"koa": "^2.4.1",
24+
"koa-router": "^7.3.0",
2325
"lint-staged": "^4.0.4",
2426
"nyc": "^11.1.0",
2527
"prettier": "^1.6.1",
2628
"remark-cli": "^4.0.0",
2729
"remark-preset-github": "^0.0.6",
30+
"supertest": "^3.0.0",
2831
"xo": "^0.19.0"
2932
},
3033
"engines": {
@@ -48,11 +51,16 @@
4851
"prettier --write --single-quote --trailing-comma none",
4952
"git add"
5053
],
51-
"*.md": ["remark . -qfo", "git add"]
54+
"*.md": [
55+
"remark . -qfo",
56+
"git add"
57+
]
5258
},
5359
"main": "index.js",
5460
"remarkConfig": {
55-
"plugins": ["preset-github"]
61+
"plugins": [
62+
"preset-github"
63+
]
5664
},
5765
"repository": {
5866
"type": "git",
@@ -67,7 +75,9 @@
6775
},
6876
"xo": {
6977
"extends": "prettier",
70-
"plugins": ["prettier"],
78+
"plugins": [
79+
"prettier"
80+
],
7181
"parserOptions": {
7282
"sourceType": "script"
7383
},

test/test.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,66 @@
1+
const Koa = require('koa');
2+
const Router = require('koa-router');
3+
const Boom = require('boom');
14
const test = require('ava');
5+
const request = require('supertest');
6+
const koa404Handler = require('..');
27

3-
const koa404Handler = require('../');
8+
const ok = ctx => {
9+
ctx.status = 200;
10+
ctx.body = { ok: 'ok' };
11+
};
12+
13+
const error = () => {
14+
throw new Error('Big Bad Error!');
15+
};
416

517
test('returns a function', t => {
618
t.true(typeof koa404Handler === 'function');
719
});
20+
21+
test.failing('middleware can be added to the app at any stage', async t => {
22+
const app = new Koa();
23+
const router = new Router();
24+
25+
router.get('/', ok);
26+
27+
// Mount the app's defined and nested routes
28+
app.use(router.routes());
29+
30+
// Options method
31+
app.use(
32+
router.allowedMethods({
33+
throw: true,
34+
notImplemented: () => new Boom.notImplemented(), // eslint-disable-line new-cap,max-len
35+
methodNotAllowed: () => new Boom.methodNotAllowed() // eslint-disable-line new-cap,max-len
36+
})
37+
);
38+
39+
// 404 handler
40+
app.use(koa404Handler);
41+
42+
const res = await request(app.listen()).options('/');
43+
44+
t.is(200, res.status);
45+
t.is(res.body.ok, 'ok');
46+
});
47+
48+
test('emits error on app instance', async t => {
49+
const app = new Koa();
50+
const router = new Router();
51+
52+
router.get('/', error);
53+
54+
// Mount the app's defined and nested routes
55+
app.use(router.routes());
56+
57+
// 404 handler
58+
app.use(koa404Handler);
59+
60+
app.on('error', error => {
61+
t.truthy(error);
62+
t.is(error.message, 'Big Bad Error!');
63+
});
64+
65+
await request(app.listen()).get('/');
66+
});

0 commit comments

Comments
 (0)