Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Testing Logestic plugin #27

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,5 @@ dist

# Finder (MacOS) folder config
.DS_Store

test.log
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
15 changes: 13 additions & 2 deletions preview/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 }) => {
Expand All @@ -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;
12 changes: 5 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export class Logestic<K extends keyof Attribute = keyof Attribute> {
return;
}

// Custom file destination
// Custom file destination // afterAll(async () => {
// await unlink(tempFilePath);
// });
this.createFileIfNotExists(dest)
.then(file => (this.dest = file))
.catch(err => {
Expand Down Expand Up @@ -170,16 +172,12 @@ export class Logestic<K extends keyof Attribute = keyof Attribute> {
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;
}

const sanitised = removeAnsi(msgNewLine);
fs.appendFile(this.dest.name, sanitised, err => {
if (err) {
throw err;
}
});
fs.appendFileSync(this.dest.name, sanitised);
}

/**
Expand Down
80 changes: 80 additions & 0 deletions src/presets/__tests__/common.test.ts
Original file line number Diff line number Diff line change
@@ -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 common preset', () => {
let client = edenTreaty<typeof app>(`${BASE_URL}:${PORT}`);
let app = new Elysia()
.use(
Logestic.preset('common', {
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(
/\[\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/
);
});
});
80 changes: 80 additions & 0 deletions src/presets/__tests__/fancy.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof app>(`${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*/
);
});
});
66 changes: 0 additions & 66 deletions test/logestic.test.ts

This file was deleted.

7 changes: 7 additions & 0 deletions tests/logestic.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof preview>('http://127.0.0.1:3000');
});