From d8060e20b169041c65bba04b89ffab93edcc3826 Mon Sep 17 00:00:00 2001 From: Nishant Aanjaney Jalan Date: Sun, 14 Apr 2024 14:00:30 +0530 Subject: [PATCH 1/4] feat: introduced proper testing --- bun.lockb | Bin 21200 -> 22030 bytes src/index.ts | 10 ++-- src/presets/__tests__/common.test.ts | 42 +++++++++++++++++ test/logestic.test.ts | 66 --------------------------- 4 files changed, 46 insertions(+), 72 deletions(-) create mode 100644 src/presets/__tests__/common.test.ts delete mode 100644 test/logestic.test.ts diff --git a/bun.lockb b/bun.lockb index b22a787f80f8d2525a6fab77074588fa8b27abbc..d9a95033161bdd9c8e101e95d3fcad13404b700b 100755 GIT binary patch delta 1121 zcmcbxl(BCO;{-iT9q)s`WW;Aw)TzDWyp(t4qqO<&-`TH>gZC*`iyhaVoLv{n00x$m z3x$OzX7Mmxwb)qUz^IwT&A`CRz|ar`qy>PqC6JZ`(mGJ_B$cn%@tE~ zDvL7{fjpqBAW&5&A4C^ie6uFgaz+8?1&@E+v%T3WKbPr&iO`nklP@suU}T-#$gaM5 z1Irl>UQ?h}Mu>YDctOIG4OloB^C$1*wddRfG};3sKe>_1obwlu7X{`yvYB&+@Gvkq z0eP&Gfjq`(lXvpj3!Z|Cfs9iCViA~moIF5(S%LK)F-SZBh(RD= zbE5EKB9%^-6g}gx_mD-CGcx?+{|AgpU2teX2$lf3hH2X`oq0Jayn@MZ=eF|n zeapYa$yS{$+bAvC=zZ_a-X%>Xwg_C(9Yi%IJLvE* zUft}g)5kTrK_iJv!V=<=GnSK|n%PcPG2g)X11j`j@<$8x$t)H&jP8>iEtYc$SVNQs z*i61>t~Oc1@{TGfo;88k5Qz1FSRaTDpct5P8H|D0WU``_G^YiSWd_9NARbqP4Uh>0 z3tT3TqC_Opb-k4Em@_c(qM$5^H4w8&PlY1Q{8IvZz1&V`2&f`}z`GbQF zXAm%vf-)&y)3`pkKM$^su&UIW&hQ^!M`Mzgl pG@5MarZ9P*zXX$^-sYSB#f+2Z1d0G1wK33f@;rZq%|3xGi~#B~Ph|iA delta 844 zcmb7CZAg<*6n^h~YuoFlzRlS-`*2!rOSiN&_u;GVLnE_bnj(mmQeu9Pl2D3RQQ63- z%tIyRkA{iS2W1x`qrZvPs)Z)a7Gfx@{mCSdaAsF$Kl{;v!*kDb&w1|Qo^$0P^7f%l zon}jxi|1S)cq5qW^5|sq-ca7u9j()Sc{4qgbzs+NleLIiF(W8rCD&?2yw4c}%~qtT zjHDuRP);yql|S6jP*+n$Q3{wsM)qP;d@W*iI82H3F!U3YOn`j|$Ge+Sr5|yn)m1~W{{UVC8lV}Z)2wewXR1R-M1PV5r zjU@eKE+>XNrj+33t8i!ZUIgVVC;Ucw*cn#k8_`Dau{o$7%h!z}DUVbTQHoDIf3-Rv zv$4JZxB{QlW8H?o_~&j3UM$QG^_@)>_J{{F8u+~GMq^8N;f0F$P@<G3z z&eblINF^s+E!^PZP-?4NG)!;8GjRzbXj2tnXVNPKA5{hWw5x9~1HJvWO}SJNd}8Zb zP(DaWoSukuj8?ZNjg8+N`B@%xs{(DV<27%`wfXN7r?d4v6Te@pOB`1x_GC-$`gzT!5D62 z%m|aFEKr!AGL$Jy1xygMBtyu|AuE_I{fx{UzThl4XyGAxi5Sdg%2++&lZj^#C;v_v zfaT(uMhiI-o3KF{mkeL+akz^M&`1Hup+_n3*d|9NNVm)5|8CaICr#ZXcSFKM2h(-w zx*Zf%35z!UO4fUL=13L#$z+#z@xL_~_43q-9ks*#++3K=wKCnSl&J_Tc`uP3&*z { return; } - // Custom file destination + // Custom file destination // afterAll(async () => { + // await unlink(tempFilePath); + // }); this.createFileIfNotExists(dest) .then(file => (this.dest = file)) .catch(err => { @@ -175,11 +177,7 @@ export class Logestic { } const sanitised = removeAnsi(msgNewLine); - fs.appendFile(this.dest.name, sanitised, err => { - if (err) { - throw err; - } - }); + fs.appendFileSync(this.dest.name, sanitised); } /** diff --git a/src/presets/__tests__/common.test.ts b/src/presets/__tests__/common.test.ts new file mode 100644 index 0000000..9bac5f8 --- /dev/null +++ b/src/presets/__tests__/common.test.ts @@ -0,0 +1,42 @@ +import { describe, test, expect, beforeAll } from 'bun:test'; +// import { unlink } from 'node:fs/promises'; +import { Logestic } from '../..'; +import Elysia from 'elysia'; +import { edenTreaty } from '@elysiajs/eden'; + +const tempFilePath = 'test.log'; +const BASE_URL = 'http://127.0.0.1'; +const PORT = 3000; + +describe('Testing common preset', () => { + let client = edenTreaty(`${BASE_URL}:${PORT}`); + let tempFile = Bun.file(tempFilePath); + let app = new Elysia() + .use( + Logestic.preset('common', { + dest: tempFile + }) + ) + .get('/', () => { + return 'Hello, world!'; + }); + + beforeAll(async () => { + app.listen(PORT); + await Bun.write(tempFile, ''); + }); + + // afterAll(async () => { + // await unlink(tempFilePath); + // }); + + test('should log request and response', async () => { + const { error } = await client.index.get(); + expect(error).toBeFalsy(); + + const log = await tempFile.text(); + expect(log).toMatch( + /\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\] GET \/ 200/ + ); + }); +}); diff --git a/test/logestic.test.ts b/test/logestic.test.ts deleted file mode 100644 index f8a0095..0000000 --- a/test/logestic.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { describe, it, expect, beforeAll, beforeEach } from 'bun:test'; -import { Elysia } from 'elysia'; -import { Logestic } from '../src'; -import { edenTreaty } from '@elysiajs/eden'; - -describe('Logestic', () => { - describe('Custom formatting', () => { - let app: Elysia; - let client: any; - let logs: string[] = []; - - beforeAll(() => { - const logestic = new Logestic(msg => logs.push(msg)) - .use('method') - .use('path') - .use('contentLength'); - - const logger = logestic.custom(({ method, path, contentLength }) => { - return `${method} ${path} ${contentLength}`; - }); - - app = new Elysia() - .use(logger) - .get('/api/:id', () => 'Hello, World!') - .listen(3000); - - client = edenTreaty('http://127.0.0.1:3000'); - }); - - beforeEach(() => (logs = [])); - - it('Custom formatting', async () => { - await client.api['hi'].get(); - - console.log('Custom logs', logs); - expect(logs.length).toBe(1); - expect(logs[0]).toBe('GET /api/hi 0'); - }); - }); - - describe('Preset formatting', () => { - let app: Elysia; - let client: any; - let logs: string[] = []; - - beforeAll(() => { - const logger = Logestic.preset('common', msg => logs.push(msg)); - - app = new Elysia() - .use(logger) - .get('/api/:id', () => 'Hello, World!') - .listen(3000); - - client = edenTreaty('http://127.0.0.1:3000'); - }); - - beforeEach(() => (logs = [])); - - it('Preset formatting', async () => { - await client.api['hi'].get(); - - expect(logs.length).toBe(1); - expect(logs[0]).toBe(' GET /api/hi 200 0'); - }); - }); -}); From b12261f514c3eb856bd4170437c2b543d054b12a Mon Sep 17 00:00:00 2001 From: Nishant Aanjaney Jalan Date: Wed, 17 Apr 2024 22:43:47 +0530 Subject: [PATCH 2/4] added common testing --- .gitignore | 2 + package.json | 2 +- src/index.ts | 2 +- src/presets/__tests__/common.test.ts | 60 +++++++++++++++++++++++----- 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 824b84f..65df6c2 100644 --- a/.gitignore +++ b/.gitignore @@ -174,3 +174,5 @@ dist # Finder (MacOS) folder config .DS_Store + +test.log \ No newline at end of file diff --git a/package.json b/package.json index 369dbd1..5bf3a8d 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "type": "module", "scripts": { "build": "rimraf dist && bun build src/index.ts --outdir dist --target bun --minify -e elysia && tsc", - "test": "bun test", + "test": "echo ' ' > test.log && bun test", "prepublishOnly": "bun run build" }, "peerDependencies": { diff --git a/src/index.ts b/src/index.ts index 778a8f0..bbec8e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -172,7 +172,7 @@ export class Logestic { const msgNewLine = `${msg}\n`; if (!this.dest.name || !this.dest.name.length) { // This is either stdout or stderr - Bun.write(this.dest, msgNewLine); + await Bun.write(this.dest, msgNewLine); return; } diff --git a/src/presets/__tests__/common.test.ts b/src/presets/__tests__/common.test.ts index 9bac5f8..74eea2f 100644 --- a/src/presets/__tests__/common.test.ts +++ b/src/presets/__tests__/common.test.ts @@ -1,16 +1,32 @@ -import { describe, test, expect, beforeAll } from 'bun:test'; -// import { unlink } from 'node:fs/promises'; +import { + describe, + test, + expect, + beforeAll, + afterAll, + beforeEach +} from 'bun:test'; +import { unlink } from 'node:fs/promises'; +import { writeFileSync } from 'node:fs'; import { Logestic } from '../..'; import Elysia from 'elysia'; import { edenTreaty } from '@elysiajs/eden'; const tempFilePath = 'test.log'; +const tempFile = Bun.file(tempFilePath); const BASE_URL = 'http://127.0.0.1'; const PORT = 3000; +beforeAll(() => { + writeFileSync(tempFilePath, ' \n'); +}); + +afterAll(async () => { + await unlink(tempFilePath); +}); + describe('Testing common preset', () => { let client = edenTreaty(`${BASE_URL}:${PORT}`); - let tempFile = Bun.file(tempFilePath); let app = new Elysia() .use( Logestic.preset('common', { @@ -19,17 +35,16 @@ describe('Testing common preset', () => { ) .get('/', () => { return 'Hello, world!'; - }); + }) + .get('/error', ({ set }) => { + set.status = 400; + }) + .listen(PORT); - beforeAll(async () => { - app.listen(PORT); - await Bun.write(tempFile, ''); + beforeEach(() => { + writeFileSync(tempFilePath, ' \n'); }); - // afterAll(async () => { - // await unlink(tempFilePath); - // }); - test('should log request and response', async () => { const { error } = await client.index.get(); expect(error).toBeFalsy(); @@ -39,4 +54,27 @@ describe('Testing common preset', () => { /\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\] GET \/ 200/ ); }); + + test('multiple logs', async () => { + const { error } = await client.index.get(); + expect(error).toBeFalsy(); + + const { error: error2 } = await client.index.get(); + expect(error2).toBeFalsy(); + + const log = await tempFile.text(); + expect(log).toMatch( + /(\s*\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\] GET \/ 200\s*){2}/ + ); + }); + + test('log error', async () => { + const { error } = await client.error.get(); + expect(error).toBeTruthy(); + + const log = await tempFile.text(); + expect(log).toMatch( + /\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\] GET \/error 400/ + ); + }); }); From a59ea60c5f91b5c6136d3396ed4948ef4c8977ee Mon Sep 17 00:00:00 2001 From: Nishant Aanjaney Jalan Date: Wed, 17 Apr 2024 22:51:03 +0530 Subject: [PATCH 3/4] added fancy testing --- src/presets/__tests__/fancy.test.ts | 80 +++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/presets/__tests__/fancy.test.ts diff --git a/src/presets/__tests__/fancy.test.ts b/src/presets/__tests__/fancy.test.ts new file mode 100644 index 0000000..174fa49 --- /dev/null +++ b/src/presets/__tests__/fancy.test.ts @@ -0,0 +1,80 @@ +import { + describe, + test, + expect, + beforeAll, + afterAll, + beforeEach +} from 'bun:test'; +import { unlink } from 'node:fs/promises'; +import { writeFileSync } from 'node:fs'; +import { Logestic } from '../..'; +import Elysia from 'elysia'; +import { edenTreaty } from '@elysiajs/eden'; + +const tempFilePath = 'test.log'; +const tempFile = Bun.file(tempFilePath); +const BASE_URL = 'http://127.0.0.1'; +const PORT = 3000; + +beforeAll(() => { + writeFileSync(tempFilePath, ' \n'); +}); + +afterAll(async () => { + await unlink(tempFilePath); +}); + +describe('Testing fancy preset', () => { + let client = edenTreaty(`${BASE_URL}:${PORT}`); + let app = new Elysia() + .use( + Logestic.preset('fancy', { + dest: tempFile + }) + ) + .get('/', () => { + return 'Hello, world!'; + }) + .get('/error', ({ set }) => { + set.status = 400; + }) + .listen(PORT); + + beforeEach(() => { + writeFileSync(tempFilePath, ' \n'); + }); + + test('should log request and response', async () => { + const { error } = await client.index.get(); + expect(error).toBeFalsy(); + + const log = await tempFile.text(); + expect(log).toMatch( + /\s*HTTP\s+\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} GET \/ \d{1,4}μs\s*/ + ); + }); + + test('multiple logs', async () => { + const { error } = await client.index.get(); + expect(error).toBeFalsy(); + + const { error: error2 } = await client.index.get(); + expect(error2).toBeFalsy(); + + const log = await tempFile.text(); + expect(log).toMatch( + /(\s*HTTP\s+\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} GET \/ \d{1,4}μs\s*){2}/ + ); + }); + + test('log error', async () => { + const { error } = await client.error.get(); + expect(error).toBeTruthy(); + + const log = await tempFile.text(); + expect(log).toMatch( + /\s*HTTP\s+\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{1,2}:\d{1,2} GET \/error \d{1,4}μs\s*/ + ); + }); +}); From 8437cc2a511cb5f68080353c18c2d5d5bc87a436 Mon Sep 17 00:00:00 2001 From: Nishant Aanjaney Jalan Date: Wed, 17 Apr 2024 23:03:54 +0530 Subject: [PATCH 4/4] Setup logestic testing --- preview/src/index.ts | 15 +++++++++++++-- tests/logestic.test.ts | 7 +++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 tests/logestic.test.ts diff --git a/preview/src/index.ts b/preview/src/index.ts index a354ce9..43d89aa 100644 --- a/preview/src/index.ts +++ b/preview/src/index.ts @@ -1,8 +1,17 @@ import { Elysia } from 'elysia'; import { Logestic } from 'logestic'; +const logger = new Logestic().use(['method', 'status']).format({ + onSuccess({ method, status }) { + return `${method} ${status}`; + }, + onFailure({ request, code }) { + return `${request.method} ${code}`; + } +}); + const app = new Elysia() - .use(Logestic.preset('fancy')) + .use(logger) .get('/', () => 'Hello, world!') .get('/hello/:name', ({ params: { name } }) => `Hello, ${name}!`) .get('/returnBad', ({ set, logestic }) => { @@ -16,5 +25,7 @@ const app = new Elysia() return 'Server crashed'; }) .listen(3000, () => { - // console.log('Server is running on port 3000'); + console.log('Server is running on port 3000'); }); + +export default app; diff --git a/tests/logestic.test.ts b/tests/logestic.test.ts new file mode 100644 index 0000000..9667e69 --- /dev/null +++ b/tests/logestic.test.ts @@ -0,0 +1,7 @@ +import { describe, expect, test } from 'bun:test'; +import preview from '../preview/src'; +import { edenTreaty } from '@elysiajs/eden'; + +describe('Logestic', () => { + const client = edenTreaty('http://127.0.0.1:3000'); +});