-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(middleware): added tests for sync middlewares
- Loading branch information
1 parent
6d37e60
commit 982eb22
Showing
3 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,67 @@ | ||
'use strict'; | ||
|
||
const middleware = require('..'); | ||
const {createMiddleware, createAsyncMiddleware} = require('../lib/middleware'); | ||
|
||
describe('@rapidcode/middleware', () => { | ||
it('needs tests'); | ||
test('should invoke middleware synchronous and execute', () => { | ||
let mockObj = false; | ||
const mockFn = ()=>{ | ||
mockObj=true; | ||
} | ||
const mw = createMiddleware({log:true, func:mockFn}) | ||
mw({},{}, ()=>{}); | ||
expect(mockObj).toBe(true); | ||
}); | ||
|
||
test('should invoke next if successful', () => { | ||
let mockObj = false; | ||
const mockFn = ()=>{ | ||
} | ||
const mw = createMiddleware({func:mockFn}) | ||
mw({},{}, ()=>{mockObj=true;}); | ||
expect(mockObj).toBe(true); | ||
}); | ||
|
||
test('should attach the return of the function to res.locals', () => { | ||
const mockFn = ()=>{ | ||
return true; | ||
} | ||
const mw = createMiddleware({func:mockFn}) | ||
const res = {locals:{}}; | ||
mw({},res, ()=>{}); | ||
// console.log(res); | ||
expect(res).toEqual({locals:{ | ||
mockFnResponse:true | ||
}}); | ||
}); | ||
|
||
test('should handle the error, with no status', () => { | ||
const mockFn = ()=>{ | ||
throw Error("Random Error"); | ||
} | ||
const mw = createMiddleware({log:false, func:mockFn}); | ||
let errorObj = {}; | ||
mw({},{}, (err)=>{ | ||
errorObj = err; | ||
}); | ||
expect(errorObj).toEqual( { | ||
status: 500, | ||
message: "Random Error" | ||
}); | ||
}); | ||
|
||
test('should handle the error with status', () => { | ||
const mockFn = ()=>{ | ||
throw ({message:"Random Error", status: 400}); | ||
} | ||
const mw = createMiddleware({log:false, func:mockFn}); | ||
let errorObj = {}; | ||
mw({},{}, (err)=>{ | ||
errorObj = {message:err.message, status:err.status}; | ||
}); | ||
expect(errorObj).toEqual( { | ||
status: 400, | ||
message: "Random Error" | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const config = { | ||
verbose: true, | ||
collectCoverage: true, | ||
collectCoverageFrom: [ | ||
"**/*.{js,jsx}", | ||
"!**/node_modules/**", | ||
"!**/dist/**", | ||
"!**/test/**", | ||
"!**/coverage/**", | ||
], | ||
coverageDirectory: "coverage", | ||
coverageReporters: ["json", "lcov", "text"], | ||
coverageThreshold: { | ||
global: { | ||
branches: 90, | ||
functions: 90, | ||
lines: 90, | ||
statements: 90, | ||
}, | ||
}, | ||
rootDir: ".", | ||
roots: ["lib", "__tests__"], | ||
testEnvironment: "node", | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters