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": { "scripts": {
"clean": "rimraf ./build", "clean": "rimraf ./build",
"test": "cross-env TZ=utc jest", "test": "cross-env TZ=utc vitest run",
"type-check": "tsc --noEmit -p tsconfig.build.json", "type-check": "tsc --noEmit -p tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly -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", "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 { printMessage } from '../src/formatter';
import { LevelCode } from '../src/levels'; import { LevelCode } from '../src/levels';
jest.mock('dayjs', () => ({ vi.mock('dayjs', () => ({
__esModule: true, __esModule: true,
default: () => ({ default: () => ({
format: () => 'formatted-date', format: () => 'formatted-date',

View File

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

View File

@ -1,3 +1,5 @@
import { describe, expect, test } from 'vitest';
import { formatLoggingDate, padRight } from '../src/utils'; import { formatLoggingDate, padRight } from '../src/utils';
describe('formatLoggingDate', () => { 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" "verdaccio"
], ],
"engines": { "engines": {
"node": ">=18" "node": ">=14"
}, },
"scripts": { "scripts": {
"clean": "rimraf ./build", "clean": "rimraf ./build",
"test": "jest", "test": "vitest run",
"type-check": "tsc --noEmit -p tsconfig.build.json", "type-check": "tsc --noEmit -p tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly -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", "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 { DEFAULT_REGISTRY } from '@verdaccio/config';
import { HEADERS, TOKEN_BASIC, TOKEN_BEARER, constants } from '@verdaccio/core'; import { HEADERS, TOKEN_BASIC, TOKEN_BEARER, constants } from '@verdaccio/core';
import { Logger } from '@verdaccio/types'; import { Logger } from '@verdaccio/types';
@ -5,11 +7,11 @@ import { buildToken } from '@verdaccio/utils';
import { ProxyStorage } from '../src'; import { ProxyStorage } from '../src';
const mockDebug = jest.fn(); const mockDebug = vi.fn();
const mockInfo = jest.fn(); const mockInfo = vi.fn();
const mockHttp = jest.fn(); const mockHttp = vi.fn();
const mockError = jest.fn(); const mockError = vi.fn();
const mockWarn = jest.fn(); const mockWarn = vi.fn();
const logger = { const logger = {
debug: mockDebug, debug: mockDebug,

View File

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

View File

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

View File

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

View File

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

View File

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