0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-13 22:48:31 -05:00
verdaccio/packages/logger/logger-commons/test/logger.spec.ts
renovate[bot] 51e571a1ba
chore(deps): update dependency @crowdin/cli to v4.4.1 (master) (#4979)
* chore(deps): update dependency @crowdin/cli to v4.4.1

* clean up

* test

* missing package migrate

* clean up

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Juan Picado <juanpicado19@gmail.com>
2024-12-07 22:35:48 +01:00

129 lines
3.9 KiB
TypeScript

import { readFile } from 'fs/promises';
import { join } from 'path';
import pino from 'pino';
import { setTimeout } from 'timers/promises';
import { describe, expect, test } from 'vitest';
import { fileUtils } from '@verdaccio/core';
import { prepareSetup } from '../src';
async function readLogFile(path: string) {
await setTimeout(1000, 'resolved');
return readFile(path, 'utf8');
}
async function createLogFile() {
const folder = await fileUtils.createTempFolder('logger');
const file = join(folder, 'logger.log');
return file;
}
const defaultOptions = {
format: 'json',
level: 'http',
colors: false,
};
describe('logger test', () => {
describe('basic', () => {
test('should include default level', async () => {
const file = await createLogFile();
const logger = prepareSetup({ type: 'file', path: file, colors: false }, pino);
logger.info({ packageName: 'test' }, `testing @{packageName}`);
// Note: this should not be logged
logger.debug(`this should not be logged`);
// Note: this should not be logged
logger.trace(`this should not be logged`);
logger.error(`this should logged`);
const content = await readLogFile(file);
expect(content).toBe('info --- testing test\nerror--- this should logged\n');
});
test('should include all logging level', async () => {
const file = await createLogFile();
const logger = prepareSetup(
{ type: 'file', level: 'trace', path: file, colors: false },
pino
);
logger.info({ packageName: 'test' }, `testing @{packageName}`);
logger.debug(`this should not be logged`);
logger.trace(`this should not be logged`);
logger.error(`this should logged`);
const content = await readLogFile(file);
expect(content).toBe(
'info --- testing test\ndebug--- this should not be logged\ntrace--- this should not be logged\nerror--- this should logged\n'
);
});
});
describe('json format', () => {
test('should log into a file with json format', async () => {
const file = await createLogFile();
const logger = prepareSetup(
{
...defaultOptions,
format: 'json',
type: 'file',
path: file,
level: 'info',
},
pino
);
logger.info(
{ packageName: 'test' },
`publishing or updating a new version for @{packageName}`
);
const content = await readLogFile(file);
expect(JSON.parse(content)).toEqual(
expect.objectContaining({
level: 30,
msg: 'publishing or updating a new version for test',
})
);
});
});
describe('pretty format', () => {
test('should log into a file with pretty', async () => {
const file = await createLogFile();
const logger = prepareSetup(
{
format: 'pretty',
type: 'file',
path: file,
level: 'trace',
colors: false,
},
pino
);
logger.info(
{ packageName: 'test' },
`publishing or updating a new version for @{packageName}`
);
const content = await readLogFile(file);
expect(content).toEqual('info --- publishing or updating a new version for test\n');
});
test('should log into a file with pretty-timestamped', async () => {
const file = await createLogFile();
const logger = prepareSetup(
{
format: 'pretty-timestamped',
type: 'file',
path: file,
level: 'trace',
colors: false,
},
pino
);
logger.info(
{ packageName: 'test' },
`publishing or updating a new version for @{packageName}`
);
const content = await readLogFile(file);
// TODO: we might want mock time for testing
expect(content).toMatch('info --- publishing or updating a new version for test\n');
});
});
});