From 477f9bcba45b5a8738afbe259a3b4a92d9cb4316 Mon Sep 17 00:00:00 2001 From: Juan Picado Date: Sat, 3 Aug 2024 22:52:48 +0200 Subject: [PATCH] chore: migrate vitest more package (#4778) * chore: migrate proxy package * chore: migrate logger-prettier --- .../logger/logger-prettify/jest.config.js | 10 --- packages/logger/logger-prettify/package.json | 2 +- .../test/__snapshots__/formatter.spec.ts.snap | 22 +++--- .../logger-prettify/test/formatter.spec.ts | 4 +- .../logger/logger-prettify/test/index.spec.ts | 32 ++++---- .../logger/logger-prettify/test/utils.test.ts | 2 + packages/logger/logger/jest.config.js | 10 --- packages/proxy/jest.config.js | 12 --- packages/proxy/package.json | 4 +- packages/proxy/test/headers.auth.spec.ts | 12 +-- packages/proxy/test/noProxy.spec.ts | 2 + packages/proxy/test/proxy-utils.spec.ts | 1 + packages/proxy/test/proxy.metadata.spec.ts | 33 +++----- packages/proxy/test/proxy.search.spec.ts | 11 +-- packages/proxy/test/proxy.tarball.spec.ts | 78 ++++++++++--------- 15 files changed, 103 insertions(+), 132 deletions(-) delete mode 100644 packages/logger/logger-prettify/jest.config.js delete mode 100644 packages/logger/logger/jest.config.js delete mode 100644 packages/proxy/jest.config.js diff --git a/packages/logger/logger-prettify/jest.config.js b/packages/logger/logger-prettify/jest.config.js deleted file mode 100644 index 405cddf2f..000000000 --- a/packages/logger/logger-prettify/jest.config.js +++ /dev/null @@ -1,10 +0,0 @@ -const config = require('../../../jest/config'); - -module.exports = Object.assign({}, config, { - coverageThreshold: { - global: { - // FIXME: increase to 90 - lines: 60, - }, - }, -}); diff --git a/packages/logger/logger-prettify/package.json b/packages/logger/logger-prettify/package.json index eda5bff1f..36c0bd251 100644 --- a/packages/logger/logger-prettify/package.json +++ b/packages/logger/logger-prettify/package.json @@ -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", diff --git a/packages/logger/logger-prettify/test/__snapshots__/formatter.spec.ts.snap b/packages/logger/logger-prettify/test/__snapshots__/formatter.spec.ts.snap index cc8dc4cf0..ec9abf993 100644 --- a/packages/logger/logger-prettify/test/__snapshots__/formatter.spec.ts.snap +++ b/packages/logger/logger-prettify/test/__snapshots__/formatter.spec.ts.snap @@ -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"`; diff --git a/packages/logger/logger-prettify/test/formatter.spec.ts b/packages/logger/logger-prettify/test/formatter.spec.ts index 1f4d011d7..c9f6b870f 100644 --- a/packages/logger/logger-prettify/test/formatter.spec.ts +++ b/packages/logger/logger-prettify/test/formatter.spec.ts @@ -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', diff --git a/packages/logger/logger-prettify/test/index.spec.ts b/packages/logger/logger-prettify/test/index.spec.ts index 85d8bbe02..7e1acfe50 100644 --- a/packages/logger/logger-prettify/test/index.spec.ts +++ b/packages/logger/logger-prettify/test/index.spec.ts @@ -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'); + })); }); diff --git a/packages/logger/logger-prettify/test/utils.test.ts b/packages/logger/logger-prettify/test/utils.test.ts index 0189927c3..cf6cc25b6 100644 --- a/packages/logger/logger-prettify/test/utils.test.ts +++ b/packages/logger/logger-prettify/test/utils.test.ts @@ -1,3 +1,5 @@ +import { describe, expect, test } from 'vitest'; + import { formatLoggingDate, padRight } from '../src/utils'; describe('formatLoggingDate', () => { diff --git a/packages/logger/logger/jest.config.js b/packages/logger/logger/jest.config.js deleted file mode 100644 index 059a06266..000000000 --- a/packages/logger/logger/jest.config.js +++ /dev/null @@ -1,10 +0,0 @@ -const config = require('../../../jest/config'); - -module.exports = Object.assign({}, config, { - coverageThreshold: { - global: { - // FIXME: increase to 90 - lines: 39, - }, - }, -}); diff --git a/packages/proxy/jest.config.js b/packages/proxy/jest.config.js deleted file mode 100644 index 9b301f64f..000000000 --- a/packages/proxy/jest.config.js +++ /dev/null @@ -1,12 +0,0 @@ -const config = require('../../jest/config'); - -module.exports = Object.assign({}, config, { - coverageThreshold: { - global: { - branches: 79, - functions: 90, - lines: 86, - statements: 86, - }, - }, -}); diff --git a/packages/proxy/package.json b/packages/proxy/package.json index 4af0d8912..3d691216b 100644 --- a/packages/proxy/package.json +++ b/packages/proxy/package.json @@ -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", diff --git a/packages/proxy/test/headers.auth.spec.ts b/packages/proxy/test/headers.auth.spec.ts index 7b12c51d1..3187a5e8e 100644 --- a/packages/proxy/test/headers.auth.spec.ts +++ b/packages/proxy/test/headers.auth.spec.ts @@ -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, diff --git a/packages/proxy/test/noProxy.spec.ts b/packages/proxy/test/noProxy.spec.ts index ae4427f37..98c19afaf 100644 --- a/packages/proxy/test/noProxy.spec.ts +++ b/packages/proxy/test/noProxy.spec.ts @@ -1,3 +1,5 @@ +import { describe, expect, test } from 'vitest'; + import { logger, setup } from '@verdaccio/logger'; import { ProxyStorage } from '../src'; diff --git a/packages/proxy/test/proxy-utils.spec.ts b/packages/proxy/test/proxy-utils.spec.ts index e91038434..89d62eccc 100644 --- a/packages/proxy/test/proxy-utils.spec.ts +++ b/packages/proxy/test/proxy-utils.spec.ts @@ -1,4 +1,5 @@ import assert from 'assert'; +import { describe, test } from 'vitest'; import { parseInterval } from '../src/proxy-utils'; diff --git a/packages/proxy/test/proxy.metadata.spec.ts b/packages/proxy/test/proxy.metadata.spec.ts index 073c5f308..9c495b836 100644 --- a/packages/proxy/test/proxy.metadata.spec.ts +++ b/packages/proxy/test/proxy.metadata.spec.ts @@ -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 () => { diff --git a/packages/proxy/test/proxy.search.spec.ts b/packages/proxy/test/proxy.search.spec.ts index 041f06d25..0978c64c2 100644 --- a/packages/proxy/test/proxy.search.spec.ts +++ b/packages/proxy/test/proxy.search.spec.ts @@ -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, diff --git a/packages/proxy/test/proxy.tarball.spec.ts b/packages/proxy/test/proxy.tarball.spec.ts index 8f73fe240..3276df281 100644 --- a/packages/proxy/test/proxy.tarball.spec.ts +++ b/packages/proxy/test/proxy.tarball.spec.ts @@ -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) => {