From a2686197ebda35b3408e9b24187fd907d2641df2 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sat, 24 Feb 2024 23:12:37 +0800 Subject: [PATCH] test: got --- lib/app.test.ts | 2 +- lib/config.ts | 2 +- lib/routes.test.ts | 4 +-- lib/utils/common-utils.test.ts | 2 +- test/utils/got.js => lib/utils/got.test.ts | 29 +++++++++++++++------- 5 files changed, 25 insertions(+), 14 deletions(-) rename test/utils/got.js => lib/utils/got.test.ts (69%) diff --git a/lib/app.test.ts b/lib/app.test.ts index 095e69de4..3faefd09c 100644 --- a/lib/app.test.ts +++ b/lib/app.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from '@jest/globals'; -import app from './app'; +import app from '@/app'; describe('index', () => { it('serve index', async () => { diff --git a/lib/config.ts b/lib/config.ts index 648779ff1..195fb5805 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -649,7 +649,7 @@ if (envs.REMOTE_CONFIG) { // @ts-expect-error value is set export const config: Config = value; -export const setConfig = (env: Partial) => { +export const setConfig = (env: Record) => { envs = Object.assign(process.env, env); calculateValue(); }; diff --git a/lib/routes.test.ts b/lib/routes.test.ts index 8fd2a4955..6edc3cdc7 100644 --- a/lib/routes.test.ts +++ b/lib/routes.test.ts @@ -2,10 +2,10 @@ import { describe, expect, it, afterAll, jest } from '@jest/globals'; process.env.SOCKET = 'socket'; jest.mock('request-promise-native'); -import app from './app'; +import app from '@/app'; import Parser from 'rss-parser'; const parser = new Parser(); -import { config } from './config'; +import { config } from '@/config'; afterAll(() => { delete process.env.SOCKET; diff --git a/lib/utils/common-utils.test.ts b/lib/utils/common-utils.test.ts index 2dd667d68..2ec31a419 100644 --- a/lib/utils/common-utils.test.ts +++ b/lib/utils/common-utils.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from '@jest/globals'; -import utils from './common-utils'; +import utils from '@/utils/common-utils'; describe('common-utils', () => { it('toTitleCase', () => { diff --git a/test/utils/got.js b/lib/utils/got.test.ts similarity index 69% rename from test/utils/got.js rename to lib/utils/got.test.ts index 89c144e23..e1531fa92 100644 --- a/test/utils/got.js +++ b/lib/utils/got.test.ts @@ -1,15 +1,14 @@ -process.env.REQUEST_TIMEOUT = '500'; -const got = require('../../lib/utils/got'); -const logger = require('../../lib/utils/logger'); -const config = require('../../lib/config').value; -const nock = require('nock'); +import { describe, expect, it, afterEach, jest } from '@jest/globals'; +import nock from 'nock'; -afterAll(() => { - delete process.env.REQUEST_TIMEOUT; +afterEach(() => { + jest.resetModules(); }); describe('got', () => { it('headers', async () => { + const { default: got } = await import('@/utils/got'); + const { config } = await import('@/config'); nock('http://rsshub.test') .get('/test') .reply(function () { @@ -21,6 +20,8 @@ describe('got', () => { }); it('retry', async () => { + const { default: got } = await import('@/utils/got'); + const { config } = await import('@/config'); const requestRun = jest.fn(); nock('http://rsshub.test') .get('/testRerty') @@ -32,7 +33,7 @@ describe('got', () => { try { await got.get('http://rsshub.test/testRerty'); - } catch (error) { + } catch (error: any) { expect(error.name).toBe('HTTPError'); } @@ -41,6 +42,7 @@ describe('got', () => { }); it('axios', async () => { + const { default: got } = await import('@/utils/got'); nock('http://rsshub.test') .post('/post') .reply(() => [200, '{"code": 0}']); @@ -51,27 +53,36 @@ describe('got', () => { }, }); expect(response1.statusCode).toBe(200); + // @ts-expect-error custom property expect(response1.status).toBe(200); expect(response1.body).toBe('{"code": 0}'); + // @ts-expect-error custom property expect(response1.data.code).toBe(0); }); it('timeout', async () => { + process.env.REQUEST_TIMEOUT = '500'; + + const { default: got } = await import('@/utils/got'); nock('http://rsshub.test') .get('/timeout') .delay(600) .reply(() => [200, '{"code": 0}']); + const logger = (await import('@/utils/logger')).default; + // @ts-expect-error unused const loggerSpy = jest.spyOn(logger, 'error').mockReturnValue({}); try { await got.get('http://rsshub.test/timeout'); throw new Error('Timeout Invalid'); - } catch (error) { + } catch (error: any) { expect(error.name).toBe('RequestError'); } expect(loggerSpy).toHaveBeenCalledWith(expect.stringContaining('http://rsshub.test/timeout')); loggerSpy.mockRestore(); + + delete process.env.REQUEST_TIMEOUT; }); });