Skip to content

Commit

Permalink
test(middleware): added tests for sync middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
tirthaguha committed Aug 21, 2021
1 parent 6d37e60 commit 982eb22
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
64 changes: 62 additions & 2 deletions packages/middleware/__tests__/middleware.test.js
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"
});
});
});
26 changes: 26 additions & 0 deletions packages/middleware/jest.config.js
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;
2 changes: 2 additions & 0 deletions packages/middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"url": "git+https://github.com/tirthaguha/rapidcode.git"
},
"scripts": {
"test:local": "rimraf ./coverage && jest --coverage --watchAll",
"test": "jest --coverage",
"build": "rimraf ./dist && webpack"
},
"bugs": {
Expand Down

0 comments on commit 982eb22

Please sign in to comment.