0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

chore: migrate vitest more package (#4778)

* chore: migrate proxy package

* chore: migrate logger-prettier
This commit is contained in:
Juan Picado 2024-08-03 22:52:48 +02:00 committed by GitHub
parent 0c7b19b192
commit 477f9bcba4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 103 additions and 132 deletions

View file

@ -1,10 +0,0 @@
const config = require('../../../jest/config');
module.exports = Object.assign({}, config, {
coverageThreshold: {
global: {
// FIXME: increase to 90
lines: 60,
},
},
});

View file

@ -30,7 +30,7 @@
},
"scripts": {
"clean": "rimraf ./build",
"test": "cross-env TZ=utc jest",
"test": "cross-env TZ=utc vitest run",
"type-check": "tsc --noEmit -p tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
"build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps",

View file

@ -1,21 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`formatter printMessage should display a bytes request 1`] = `"fatal<-- 200, user: null(127.0.0.1), req: 'GET /verdaccio', bytes: 0/150186"`;
exports[`formatter > printMessage > should display a bytes request 1`] = `"fatal<-- 200, user: null(127.0.0.1), req: 'GET /verdaccio', bytes: 0/150186"`;
exports[`formatter printMessage should display a resource request 1`] = `"info <-- 127.0.0.1 requested 'GET /verdaccio'"`;
exports[`formatter > printMessage > should display a resource request 1`] = `"info <-- 127.0.0.1 requested 'GET /verdaccio'"`;
exports[`formatter printMessage should display a streaming request 1`] = `"fatal--> 304, req: 'GET https://registry.npmjs.org/verdaccio' (streaming)"`;
exports[`formatter > printMessage > should display a streaming request 1`] = `"fatal--> 304, req: 'GET https://registry.npmjs.org/verdaccio' (streaming)"`;
exports[`formatter printMessage should display an error request 1`] = `"fatal--> ERR, req: 'GET https://registry.fake.org/aaa', error: getaddrinfo ENOTFOUND registry.fake.org"`;
exports[`formatter > printMessage > should display an error request 1`] = `"fatal--> ERR, req: 'GET https://registry.fake.org/aaa', error: getaddrinfo ENOTFOUND registry.fake.org"`;
exports[`formatter printMessage should display an fatal request 1`] = `"fatal--> ERR, req: 'GET https://registry.fake.org/aaa', error: fatal error"`;
exports[`formatter > printMessage > should display an fatal request 1`] = `"fatal--> ERR, req: 'GET https://registry.fake.org/aaa', error: fatal error"`;
exports[`formatter printMessage should display config file 1`] = `"warn --- config file - /Users/user/.config/verdaccio/config/config.yaml"`;
exports[`formatter > printMessage > should display config file 1`] = `"warn --- config file - /Users/user/.config/verdaccio/config/config.yaml"`;
exports[`formatter printMessage should display custom log message 1`] = `"fatal--- custom - foo - undefined"`;
exports[`formatter > printMessage > should display custom log message 1`] = `"fatal--- custom - foo - undefined"`;
exports[`formatter printMessage should display trace level 1`] = `"trace--- [trace] - foo"`;
exports[`formatter > printMessage > should display trace level 1`] = `"trace--- [trace] - foo"`;
exports[`formatter printMessage should display trace level with pretty stamp 1`] = `"[formatted-date] trace--- [trace] - foo"`;
exports[`formatter > printMessage > should display trace level with pretty stamp 1`] = `"[formatted-date] trace--- [trace] - foo"`;
exports[`formatter printMessage should display version and http address 1`] = `"warn --- http address - http://localhost:4873/ - verdaccio/5.0.0"`;
exports[`formatter > printMessage > should display version and http address 1`] = `"warn --- http address - http://localhost:4873/ - verdaccio/5.0.0"`;

View file

@ -1,7 +1,9 @@
import { describe, expect, test, vi } from 'vitest';
import { printMessage } from '../src/formatter';
import { LevelCode } from '../src/levels';
jest.mock('dayjs', () => ({
vi.mock('dayjs', () => ({
__esModule: true,
default: () => ({
format: () => 'formatted-date',

View file

@ -1,5 +1,6 @@
import pino from 'pino';
import { Writable } from 'stream';
import { describe, expect, test } from 'vitest';
import { buildPretty } from '../src';
@ -10,19 +11,20 @@ describe('prettyFactory', () => {
prettyStamp: false,
colors: false,
};
test('should return a function', (done) => {
const pretty = buildPretty(prettyfierOptions);
const log = pino(
new Writable({
objectMode: true,
write(chunk, enc, cb) {
const formatted = pretty(JSON.parse(chunk));
expect(formatted).toBe('info --- test message');
cb();
done();
},
})
);
log.info({ test: 'test' }, '@{test} message');
});
test('should return a function', () =>
new Promise((done) => {
const pretty = buildPretty(prettyfierOptions);
const log = pino(
new Writable({
objectMode: true,
write(chunk, enc, cb) {
const formatted = pretty(JSON.parse(chunk));
expect(formatted).toBe('info --- test message');
cb();
done(true);
},
})
);
log.info({ test: 'test' }, '@{test} message');
}));
});

View file

@ -1,3 +1,5 @@
import { describe, expect, test } from 'vitest';
import { formatLoggingDate, padRight } from '../src/utils';
describe('formatLoggingDate', () => {

View file

@ -1,10 +0,0 @@
const config = require('../../../jest/config');
module.exports = Object.assign({}, config, {
coverageThreshold: {
global: {
// FIXME: increase to 90
lines: 39,
},
},
});

View file

@ -1,12 +0,0 @@
const config = require('../../jest/config');
module.exports = Object.assign({}, config, {
coverageThreshold: {
global: {
branches: 79,
functions: 90,
lines: 86,
statements: 86,
},
},
});

View file

@ -26,11 +26,11 @@
"verdaccio"
],
"engines": {
"node": ">=18"
"node": ">=14"
},
"scripts": {
"clean": "rimraf ./build",
"test": "jest",
"test": "vitest run",
"type-check": "tsc --noEmit -p tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
"build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps",

View file

@ -1,3 +1,5 @@
import { describe, expect, test, vi } from 'vitest';
import { DEFAULT_REGISTRY } from '@verdaccio/config';
import { HEADERS, TOKEN_BASIC, TOKEN_BEARER, constants } from '@verdaccio/core';
import { Logger } from '@verdaccio/types';
@ -5,11 +7,11 @@ import { buildToken } from '@verdaccio/utils';
import { ProxyStorage } from '../src';
const mockDebug = jest.fn();
const mockInfo = jest.fn();
const mockHttp = jest.fn();
const mockError = jest.fn();
const mockWarn = jest.fn();
const mockDebug = vi.fn();
const mockInfo = vi.fn();
const mockHttp = vi.fn();
const mockError = vi.fn();
const mockWarn = vi.fn();
const logger = {
debug: mockDebug,

View file

@ -1,3 +1,5 @@
import { describe, expect, test } from 'vitest';
import { logger, setup } from '@verdaccio/logger';
import { ProxyStorage } from '../src';

View file

@ -1,4 +1,5 @@
import assert from 'assert';
import { describe, test } from 'vitest';
import { parseInterval } from '../src/proxy-utils';

View file

@ -1,6 +1,7 @@
import nock from 'nock';
import path from 'path';
import { setTimeout } from 'timers/promises';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { Config, parseConfigFile } from '@verdaccio/config';
import { API_ERROR, errorUtils } from '@verdaccio/core';
@ -10,11 +11,11 @@ import { ProxyStorage } from '../src';
const getConf = (name) => path.join(__dirname, '/conf', name);
const mockDebug = jest.fn();
const mockInfo = jest.fn();
const mockHttp = jest.fn();
const mockError = jest.fn();
const mockWarn = jest.fn();
const mockDebug = vi.fn();
const mockInfo = vi.fn();
const mockHttp = vi.fn();
const mockError = vi.fn();
const mockWarn = vi.fn();
const logger = {
debug: mockDebug,
@ -24,39 +25,25 @@ const logger = {
warn: mockWarn,
} as unknown as Logger;
// mock to get the headers fixed value
jest.mock('crypto', () => {
return {
randomBytes: (): { toString: () => string } => {
return {
toString: (): string => 'foo-random-bytes',
};
},
pseudoRandomBytes: (): { toString: () => string } => {
return {
toString: (): string => 'foo-phseudo-bytes',
};
},
};
});
const domain = 'https://registry.npmjs.org';
describe('proxy', () => {
beforeEach(() => {
nock.cleanAll();
});
const defaultRequestOptions = {
url: 'https://registry.npmjs.org',
url: domain,
};
const proxyPath = getConf('proxy1.yaml');
const conf = new Config(parseConfigFile(proxyPath));
conf.server_id = 'foo-phseudo-bytes';
describe('getRemoteMetadata', () => {
beforeEach(() => {
nock.cleanAll();
nock.abortPendingRequests();
jest.clearAllMocks();
vi.clearAllMocks();
});
describe('basic requests', () => {
test('success call to remote', async () => {

View file

@ -4,6 +4,7 @@
import getStream from 'get-stream';
import nock from 'nock';
import path from 'path';
import { describe, expect, test, vi } from 'vitest';
import { Config, parseConfigFile } from '@verdaccio/config';
import { streamUtils } from '@verdaccio/core';
@ -13,11 +14,11 @@ import { ProxyStorage } from '../src';
const getConf = (name) => path.join(__dirname, '/conf', name);
const mockDebug = jest.fn();
const mockInfo = jest.fn();
const mockHttp = jest.fn();
const mockError = jest.fn();
const mockWarn = jest.fn();
const mockDebug = vi.fn();
const mockInfo = vi.fn();
const mockHttp = vi.fn();
const mockError = vi.fn();
const mockWarn = vi.fn();
const logger = {
debug: mockDebug,

View file

@ -1,17 +1,18 @@
import nock from 'nock';
import path from 'path';
import { beforeEach, describe, test, vi } from 'vitest';
import { Config, parseConfigFile } from '@verdaccio/config';
import { logger, setup } from '@verdaccio/logger';
import { ProxyStorage } from '../src';
setup();
setup({});
const getConf = (name) => path.join(__dirname, '/conf', name);
// // mock to get the headers fixed value
jest.mock('crypto', () => {
vi.mock('crypto', () => {
return {
randomBytes: (): { toString: () => string } => {
return {
@ -30,7 +31,7 @@ describe('tarball proxy', () => {
beforeEach(() => {
nock.cleanAll();
nock.abortPendingRequests();
jest.clearAllMocks();
vi.clearAllMocks();
});
const defaultRequestOptions = {
url: 'https://registry.verdaccio.org',
@ -40,41 +41,44 @@ describe('tarball proxy', () => {
const conf = new Config(parseConfigFile(proxyPath));
describe('fetchTarball', () => {
test('get file tarball fetch', (done) => {
nock('https://registry.verdaccio.org')
.get('/jquery/-/jquery-0.0.1.tgz')
.replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'));
const prox1 = new ProxyStorage(defaultRequestOptions, conf, logger);
const stream = prox1.fetchTarball(
'https://registry.verdaccio.org/jquery/-/jquery-0.0.1.tgz',
{}
);
stream.on('response', () => {
done();
});
stream.on('error', (err) => {
done(err);
});
});
test('get file tarball fetch', () =>
new Promise((done) => {
nock('https://registry.verdaccio.org')
.get('/jquery/-/jquery-0.0.1.tgz')
.replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'));
const prox1 = new ProxyStorage(defaultRequestOptions, conf, logger);
const stream = prox1.fetchTarball(
'https://registry.verdaccio.org/jquery/-/jquery-0.0.1.tgz',
// @ts-expect-error
{}
);
stream.on('response', () => {
done(true);
});
stream.on('error', (err) => {
done(err);
});
}));
test.skip('get file tarball handle retries', (done) => {
nock('https://registry.verdaccio.org')
.get('/jquery/-/jquery-0.0.1.tgz')
.twice()
.reply(500, 'some-text')
.get('/jquery/-/jquery-0.0.1.tgz')
.once()
.replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'));
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
const stream = prox1.fetchTarball(
'https://registry.verdaccio.org/jquery/-/jquery-0.0.1.tgz',
{ retry: { limit: 2 } }
);
stream.on('error', () => {
// FIXME: stream should have handle 2 retry
done();
});
});
test.skip('get file tarball handle retries', () =>
new Promise((done) => {
nock('https://registry.verdaccio.org')
.get('/jquery/-/jquery-0.0.1.tgz')
.twice()
.reply(500, 'some-text')
.get('/jquery/-/jquery-0.0.1.tgz')
.once()
.replyWithFile(201, path.join(__dirname, 'partials/jquery-0.0.1.tgz'));
const prox1 = new ProxyStorage(defaultRequestOptions, conf);
const stream = prox1.fetchTarball(
'https://registry.verdaccio.org/jquery/-/jquery-0.0.1.tgz',
{ retry: { limit: 2 } }
);
stream.on('error', () => {
// FIXME: stream should have handle 2 retry
done();
});
}));
});
});
// test('get file tarball correct content-length', (done) => {