11/* eslint-disable no-shadow */
22import compose from 'koa-compose' ;
33import validate , { object , string } from 'koa-context-validator' ;
4+ import createError from 'http-errors' ;
45
56import finalHandler from '../' ;
67
78const middleware = finalHandler ( ) ;
89
9- const createContext = ( { status } ) => ( {
10+ const createContext = ( { status } = { } ) => ( {
1011 request : {
1112 method : 'GET' ,
1213 url : '/private' ,
@@ -36,6 +37,7 @@ const createContext = ({ status }) => ({
3637const nodeEnv = process . env . NODE_ENV ;
3738
3839beforeEach ( ( ) => {
40+ console . error = jest . fn ( ) ;
3941 jest . resetModules ( ) ;
4042} ) ;
4143
@@ -74,9 +76,8 @@ describe('finalHandler', () => {
7476
7577 it ( 'call error if error happens' , async ( ) => {
7678 const error = new Error ( 'my error' ) ;
77- const ctx = createContext ( { status : 500 } ) ;
79+ const ctx = createContext ( ) ;
7880 const next = jest . fn ( ( ) => Promise . reject ( error ) ) ;
79- console . error = jest . fn ( ) ;
8081
8182 await middleware ( ctx , next ) ;
8283
@@ -91,50 +92,73 @@ describe('finalHandler', () => {
9192 const middleware = finalHandler ( ) ;
9293
9394 const error = new Error ( 'my error' ) ;
94- const ctx = createContext ( { status : 500 } ) ;
95+ const ctx = createContext ( ) ;
9596 const next = jest . fn ( ( ) => Promise . reject ( error ) ) ;
96- console . error = jest . fn ( ) ;
9797
9898 await middleware ( ctx , next ) ;
9999
100- expect ( ctx . response . body ) . toEqual ( { error } ) ;
100+ expect ( ctx . response . body ) . toEqual ( {
101+ error,
102+ } ) ;
101103 } ) ;
102104
103- it ( 'will not get error on response.body when NODE_ENV is `production`' , async ( ) => {
105+ it ( 'put status message to error message' , async ( ) => {
106+ process . env . NODE_ENV = 'development' ;
107+
108+ const finalHandler = require ( '../' ) ;
109+ const middleware = finalHandler ( ) ;
110+
111+ const error = createError ( 401 ) ;
112+ const ctx = createContext ( ) ;
113+ const next = jest . fn ( ( ) => Promise . reject ( error ) ) ;
114+
115+ await middleware ( ctx , next ) ;
116+
117+ expect ( ctx . response . body ) . toEqual ( {
118+ error : {
119+ message : 'Unauthorized' ,
120+ } ,
121+ } ) ;
122+ } ) ;
123+
124+ it ( 'will not get error details on response.body when NODE_ENV is `production`' , async ( ) => {
104125 process . env . NODE_ENV = 'production' ;
105126
106127 const finalHandler = require ( '../' ) ;
107128 const middleware = finalHandler ( ) ;
108129
109130 const error = new Error ( 'my error' ) ;
110- const ctx = createContext ( { status : 500 } ) ;
131+ const ctx = createContext ( ) ;
111132 const next = jest . fn ( ( ) => Promise . reject ( error ) ) ;
112- console . error = jest . fn ( ) ;
113133
114134 await middleware ( ctx , next ) ;
115135
116- expect ( ctx . response . body ) . toBeUndefined ( ) ;
136+ expect ( ctx . response . body ) . toEqual ( {
137+ error : {
138+ message : 'Internal Server Error' ,
139+ } ,
140+ } ) ;
117141 } ) ;
118- } ) ;
119142
120- it ( 'should support Joi errors' , async ( ) => {
121- const composed = compose ( [
122- middleware ,
123- validate ( {
124- body : object ( ) . keys ( {
125- username : string ( ) . required ( ) ,
143+ it ( 'should support Joi errors' , async ( ) => {
144+ const composed = compose ( [
145+ middleware ,
146+ validate ( {
147+ body : object ( ) . keys ( {
148+ username : string ( ) . required ( ) ,
149+ } ) ,
126150 } ) ,
127- } ) ,
128- ] ) ;
151+ ] ) ;
129152
130- const ctx = createContext ( { } ) ;
131- const next = jest . fn ( ( ) => Promise . resolve ( ) ) ;
153+ const ctx = createContext ( ) ;
154+ const next = jest . fn ( ( ) => Promise . resolve ( ) ) ;
132155
133- await composed ( ctx , next ) ;
156+ await composed ( ctx , next ) ;
134157
135- expect ( ctx . response . status ) . toEqual ( 400 ) ;
136- expect ( ctx . response . body ) . toHaveProperty (
137- 'error.message' ,
138- '"username" is required'
139- ) ;
158+ expect ( ctx . response . status ) . toEqual ( 400 ) ;
159+ expect ( ctx . response . body ) . toHaveProperty (
160+ 'error.message' ,
161+ '"username" is required'
162+ ) ;
163+ } ) ;
140164} ) ;
0 commit comments