From 7752238ab8c1606e7564bd518f0ec71cb678715e Mon Sep 17 00:00:00 2001 From: Jineshdarjee Date: Mon, 15 Jan 2024 18:16:59 +0530 Subject: [PATCH] Initialize the unit test case --- backend/.eslintignore | 3 +- backend/.eslintrc.cjs | 1 + backend/jest.config.js | 26 ++++++++++++++-- backend/package.json | 3 ++ backend/src/server.ts | 1 - backend/test/helper.ts | 29 ------------------ backend/test/routes.test.ts | 53 +++++++++++++-------------------- backend/test/tsconfig.test.json | 19 ------------ backend/tsconfig.json | 1 + backend/tsconfig.test.json | 13 ++++++++ 10 files changed, 64 insertions(+), 85 deletions(-) delete mode 100644 backend/test/helper.ts delete mode 100644 backend/test/tsconfig.test.json create mode 100644 backend/tsconfig.test.json diff --git a/backend/.eslintignore b/backend/.eslintignore index a11444e..c20098d 100644 --- a/backend/.eslintignore +++ b/backend/.eslintignore @@ -1,4 +1,5 @@ coverage/* node_modules/* build/* -.eslintrc.js \ No newline at end of file +.eslintrc.js +.eslintrc.cjs \ No newline at end of file diff --git a/backend/.eslintrc.cjs b/backend/.eslintrc.cjs index 82a92c1..b4c9319 100644 --- a/backend/.eslintrc.cjs +++ b/backend/.eslintrc.cjs @@ -10,5 +10,6 @@ module.exports = { '@typescript-eslint/explicit-module-boundary-types': 0, '@typescript-eslint/camelcase': 0, '@typescript-eslint/no-var-requires': 0, + 'no-console': ['error'], }, }; diff --git a/backend/jest.config.js b/backend/jest.config.js index 6c1e9ae..7ed6e88 100644 --- a/backend/jest.config.js +++ b/backend/jest.config.js @@ -1,5 +1,27 @@ /** @type {import('ts-jest').JestConfigWithTsJest} */ export default { - preset: 'ts-jest', + preset: 'ts-jest/presets/js-with-ts-esm', + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1', + }, testEnvironment: 'node', -}; \ No newline at end of file + + testPathIgnorePatterns: [ + '/node_modules/', + '/build/' + ], + resetModules: true, + testTimeout: 30000, + coverageThreshold: { + global: { + statements: 95, + branches: 75, + functions: 100, + lines: 95, + }, + }, + coveragePathIgnorePatterns: [ + 'src/server.ts', + 'src/plugins/config.ts', + ], +} \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index 1b0db91..802d97e 100644 --- a/backend/package.json +++ b/backend/package.json @@ -30,6 +30,7 @@ "@account-abstraction/utils": "0.5.0", "@aws-sdk/client-secrets-manager": "3.410.0", "@fastify/cors": "8.4.1", + "@fastify/redis": "^6.1.1", "@sinclair/typebox": "0.31.28", "ajv": "8.11.2", "dotenv": "16.0.3", @@ -43,6 +44,7 @@ "devDependencies": { "@babel/core": "7.23.2", "@babel/preset-env": "7.23.2", + "@babel/preset-typescript": "^7.23.3", "@types/jest": "29.5.3", "@types/node": "18.11.15", "@typescript-eslint/eslint-plugin": "5.45.0", @@ -52,6 +54,7 @@ "eslint": "8.28.0", "eslint-config-prettier": "8.5.0", "eslint-plugin-prettier": "4.2.1", + "husky": "^8.0.3", "jest": "29.6.2", "pino-pretty": "8.1.0", "prettier": "2.8.0", diff --git a/backend/src/server.ts b/backend/src/server.ts index 5d92334..630785e 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -22,7 +22,6 @@ await server.register(cors, { preflightContinue: true }) - await server.register(config); await server.register(routes); await server.register(fastifyCron, { diff --git a/backend/test/helper.ts b/backend/test/helper.ts deleted file mode 100644 index 43f9263..0000000 --- a/backend/test/helper.ts +++ /dev/null @@ -1,29 +0,0 @@ -import fastify from 'Fastify'; -import fp from 'fastify-plugin'; -import { configPlugin } from '../src/plugins/config'; -import routes from '../src/routes/index' - - -// Automatically build and tear down our instance -function build () { - const app = fastify(); - - // fastify-plugin ensures that all decorators - // are exposed for testing purposes, this is - // different from the production setup - - - beforeAll(async () => { - void app.register(fp(configPlugin)); - app.register(routes) - await app.ready(); - }); - - afterAll(() => app.close()) - - return app -} - -export { - build -} \ No newline at end of file diff --git a/backend/test/routes.test.ts b/backend/test/routes.test.ts index 7ff5845..3386094 100644 --- a/backend/test/routes.test.ts +++ b/backend/test/routes.test.ts @@ -1,37 +1,24 @@ -import { build } from './helper' +import { FastifyInstance } from 'fastify'; +import server from '../src/server.js'; -describe('root tests', () => { - const app = build(); +const app = server as FastifyInstance; -test('default root route without params', async () => { - const res = await app.inject({ - url: '/', - method: "POST" - }) - expect(JSON.parse(res.payload)).toEqual({ error: 'Empty Body received'}) -}) +beforeEach(() => { + jest.clearAllMocks(); +}); -test('default etherspot paymaster with params', async () => { - const userOp = { - sender: "0x603Ef162f05dDa6e3B4717f4A951d6eF614a897f", - nonce: "0x1d", - initCode: "0x", - callData: "0x47e1da2a000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000080a1874e1046b1cc5defdf4d3153838b72ff94ac0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000002386f26fc10000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000", - callGasLimit: "0x8342", - verificationGasLimit: "0x16070", - maxFeePerGas: "0x7d912eba", - maxPriorityFeePerGas: "0x7d912eaa", - paymasterAndData: "0x", - preVerificationGas: "0xa96c", - signature: "0x" - }; - const res = await app.inject({ - url: '/', - method: "POST", - payload: {params: [userOp, "0x603Ef162f05dDa6e3B4717f4A951d6eF614a897f"]}, - }); - expect(res.statusCode).toEqual(200); - expect(JSON.parse(res.payload)).toHaveProperty('paymasterAndData'); -}) +afterAll(async () => { + await app.close(); + jest.clearAllMocks(); +}); + +describe('HealthCheck Routes', () => { + test('should respond with success status', async () => { + const response = await app.inject({ + method: 'GET', + url: '/healthCheck', + }); -}) \ No newline at end of file + expect(response.statusCode).toBe(200); + }); +}); diff --git a/backend/test/tsconfig.test.json b/backend/test/tsconfig.test.json deleted file mode 100644 index 87e1081..0000000 --- a/backend/test/tsconfig.test.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "compilerOptions": { - "baseUrl": "./", - "resolvePackageJsonImports": true, - "rootDir": "./", - "module": "esnext", - "target": "es2017", - "moduleResolution": "Node", - "lib": ["esnext", "ES2022", "ES2017"], - "esModuleInterop": true, - "allowSyntheticDefaultImports": true, - "sourceMap": true, - "skipLibCheck": true, - "strict": false - }, - "include": [ - "**/*.ts", "./*.ts", - ] -} \ No newline at end of file diff --git a/backend/tsconfig.json b/backend/tsconfig.json index 313eeda..e3df67c 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -4,6 +4,7 @@ "module": "ESNext", "moduleResolution": "Node", "lib": ["esnext"], + "allowJs": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "sourceMap": true, diff --git a/backend/tsconfig.test.json b/backend/tsconfig.test.json new file mode 100644 index 0000000..f03ec11 --- /dev/null +++ b/backend/tsconfig.test.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "isolatedModules": true, + }, + "include": [ + "**/*.test.ts", + "**/*.spec.ts" + ], + "exclude": [ + "./node_modules/", + "./build/" + ] +} \ No newline at end of file