Skip to content

Commit

Permalink
feat: replace jest with vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Mar 3, 2024
1 parent 3bfdf94 commit 38e4215
Show file tree
Hide file tree
Showing 38 changed files with 698 additions and 1,646 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Expand Up @@ -18,7 +18,7 @@ permissions:
contents: read

jobs:
jest:
vitest:
runs-on: ubuntu-latest
timeout-minutes: 10
services:
Expand All @@ -31,7 +31,7 @@ jobs:
fail-fast: false
matrix:
node-version: [ 18, 20, 21 ]
name: Jest on Node ${{ matrix.node-version }}
name: Vitest on Node ${{ matrix.node-version }}
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
Expand All @@ -46,7 +46,7 @@ jobs:
- name: Run postinstall script for dependencies
run: pnpm rb
- name: Test all and generate coverage
run: pnpm run jest:coverage
run: pnpm run vitest:coverage
env:
REDIS_URL: redis://localhost:${{ job.services.redis.ports[6379] }}/
- name: Upload coverage to Codecov
Expand All @@ -72,7 +72,7 @@ jobs:
- name: Chrome from Google
dependency: google-chrome-stable
environment: '{ "CHROMIUM_EXECUTABLE_PATH": "google-chrome-stable" }'
name: Jest puppeteer on Node ${{ matrix.node-version }} with ${{ matrix.chromium.name }}
name: Vitest puppeteer on Node ${{ matrix.node-version }} with ${{ matrix.chromium.name }}
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
Expand Down Expand Up @@ -101,7 +101,7 @@ jobs:
sudo apt-get update
sudo apt-get install -yq --no-install-recommends ${{ matrix.chromium.dependency }}
- name: Test puppeteer
run: pnpm run jest puppeteer
run: pnpm run vitest puppeteer
env: ${{ fromJSON(matrix.chromium.environment) }}

docs:
Expand Down Expand Up @@ -153,7 +153,7 @@ jobs:

automerge:
if: github.triggering_actor == 'dependabot[bot]' && github.event_name == 'pull_request'
needs: [ jest, puppeteer, docs, all ]
needs: [ vitest, puppeteer, docs, all ]
runs-on: ubuntu-latest
permissions:
pull-requests: write
Expand Down
2 changes: 1 addition & 1 deletion lib/app.test.ts
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';

import app from '@/app';

Expand Down
4 changes: 2 additions & 2 deletions lib/config.test.ts
@@ -1,8 +1,8 @@
import { describe, expect, it, afterEach, jest } from '@jest/globals';
import { describe, expect, it, afterEach, vi } from 'vitest';
import nock from 'nock';

afterEach(() => {
jest.resetModules();
vi.resetModules();
});

describe('config', () => {
Expand Down
10 changes: 6 additions & 4 deletions lib/config.ts
Expand Up @@ -633,16 +633,18 @@ calculateValue();

if (envs.REMOTE_CONFIG) {
got.get(envs.REMOTE_CONFIG)
.then((response) => {
.then(async (response) => {
const data = JSON.parse(response.body);
if (data) {
envs = Object.assign(envs, data);
calculateValue();
require('@/utils/logger').default.info('Remote config loaded.');
const { default: logger } = await import('@/utils/logger');
logger.info('Remote config loaded.');
}
})
.catch((error) => {
require('@/utils/logger').default.error('Remote config load failed.', error);
.catch(async (error) => {
const { default: logger } = await import('@/utils/logger');
logger.error('Remote config load failed.', error);
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/errors/index.test.ts
@@ -1,4 +1,4 @@
import { describe, expect, it, afterAll } from '@jest/globals';
import { describe, expect, it, afterAll } from 'vitest';
import supertest from 'supertest';
import server from '@/index';
import { load } from 'cheerio';
Expand Down
4 changes: 2 additions & 2 deletions lib/middleware/access-control.test.ts
@@ -1,4 +1,4 @@
import { describe, expect, it, jest, afterEach } from '@jest/globals';
import { describe, expect, it, vi, afterEach } from 'vitest';
import md5 from '@/utils/md5';

process.env.NODE_NAME = 'mock';
Expand All @@ -10,7 +10,7 @@ async function checkBlock(response) {

afterEach(() => {
delete process.env.ACCESS_KEY;
jest.resetModules();
vi.resetModules();
});

describe('access-control', () => {
Expand Down
5 changes: 2 additions & 3 deletions lib/middleware/anti-hotlink.test.ts
@@ -1,8 +1,7 @@
import { describe, expect, it, jest, afterEach, afterAll } from '@jest/globals';
import { describe, expect, it, vi, afterEach, afterAll } from 'vitest';
import Parser from 'rss-parser';

const parser = new Parser();
jest.setTimeout(50000);

afterAll(() => {
delete process.env.HOTLINK_TEMPLATE;
Expand All @@ -16,7 +15,7 @@ afterEach(() => {
delete process.env.HOTLINK_INCLUDE_PATHS;
delete process.env.HOTLINK_EXCLUDE_PATHS;
delete process.env.ALLOW_USER_HOTLINK_TEMPLATE;
jest.resetModules();
vi.resetModules();
});

const expects = {
Expand Down
4 changes: 2 additions & 2 deletions lib/middleware/cache.test.ts
@@ -1,4 +1,4 @@
import { describe, expect, it, jest, afterEach, afterAll, beforeAll } from '@jest/globals';
import { describe, expect, it, vi, afterEach, afterAll, beforeAll } from 'vitest';
import Parser from 'rss-parser';
import wait from '@/utils/wait';

Expand All @@ -11,7 +11,7 @@ beforeAll(() => {

afterEach(() => {
delete process.env.CACHE_TYPE;
jest.resetModules();
vi.resetModules();
});

afterAll(() => {
Expand Down
12 changes: 1 addition & 11 deletions lib/middleware/debug.test.ts
@@ -1,16 +1,9 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';
import app from '@/app';
import { load } from 'cheerio';

process.env.NODE_NAME = 'mock';

let gitHash;
try {
gitHash = require('git-rev-sync').short();
} catch {
gitHash = (process.env.HEROKU_SLUG_COMMIT && process.env.HEROKU_SLUG_COMMIT.slice(0, 7)) || 'unknown';
}

describe('debug', () => {
it('debug', async () => {
const response1 = await app.request('/test/1');
Expand All @@ -34,9 +27,6 @@ describe('debug', () => {
case 'Node Name:':
expect(value).toBe('mock');
break;
case 'Git Hash:':
expect(value).toBe(gitHash);
break;
case 'Request Amount:':
expect(value).toBe('6');
break;
Expand Down
6 changes: 2 additions & 4 deletions lib/middleware/filter-engine.test.ts
@@ -1,14 +1,12 @@
import { describe, expect, it, afterAll, jest, afterEach } from '@jest/globals';

jest.setTimeout(50000);
import { describe, expect, it, afterAll, vi, afterEach } from 'vitest';

afterAll(() => {
delete process.env.FILTER_REGEX_ENGINE;
});

afterEach(() => {
delete process.env.FILTER_REGEX_ENGINE;
jest.resetModules();
vi.resetModules();
});

describe('filter-engine', () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/header.test.ts
@@ -1,4 +1,4 @@
import { describe, expect, it, afterAll } from '@jest/globals';
import { describe, expect, it, afterAll } from 'vitest';

process.env.NODE_NAME = 'mock';
process.env.ALLOW_ORIGIN = 'rsshub.mock';
Expand Down
4 changes: 2 additions & 2 deletions lib/middleware/parameter.test.ts
@@ -1,4 +1,4 @@
import { describe, expect, it, jest } from '@jest/globals';
import { describe, expect, it, vi } from 'vitest';
import app from '@/app';
import Parser from 'rss-parser';
import { config } from '@/config';
Expand Down Expand Up @@ -423,7 +423,7 @@ describe('multi parameter', () => {

describe('openai', () => {
it(`chatgpt`, async () => {
jest.resetModules();
vi.resetModules();

process.env.OPENAI_API_KEY = 'sk-1234567890';
const app = (await import('@/app')).default;
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/template.test.ts
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';
import app from '@/app';
import Parser from 'rss-parser';

Expand Down
109 changes: 52 additions & 57 deletions lib/pkg.test.ts
@@ -1,4 +1,4 @@
import { describe, expect, it } from '@jest/globals';
import { describe, expect, it } from 'vitest';
import { init, request } from './pkg';

describe('pkg', () => {
Expand All @@ -10,64 +10,59 @@ describe('pkg', () => {
expect(config.ua).toBe('mock');
});

it('request', (done) => {
request('/test/1').then((data) => {
expect(data).toMatchObject({
atomlink: 'http://localhost/test/1',
title: 'Test 1',
itunes_author: null,
link: 'https://github.com/DIYgod/RSSHub',
item: [
{
title: 'Title1',
description: 'Description1',
pubDate: 'Mon, 31 Dec 2018 15:59:50 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/1',
author: 'DIYgod1',
},
{
title: 'Title2',
description: 'Description2',
pubDate: 'Mon, 31 Dec 2018 15:59:40 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/2',
author: 'DIYgod2',
},
{
title: 'Title3',
description: 'Description3',
pubDate: 'Mon, 31 Dec 2018 15:59:30 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/3',
author: 'DIYgod3',
},
{
title: 'Title4',
description: 'Description4',
pubDate: 'Mon, 31 Dec 2018 15:59:20 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/4',
author: 'DIYgod4',
},
{
title: 'Title5',
description: 'Description5',
pubDate: 'Mon, 31 Dec 2018 15:59:10 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/5',
author: 'DIYgod5',
},
],
allowEmpty: false,
});
done();
it('request', async () => {
const data = await request('/test/1');
expect(data).toMatchObject({
atomlink: 'http://localhost/test/1',
title: 'Test 1',
itunes_author: null,
link: 'https://github.com/DIYgod/RSSHub',
item: [
{
title: 'Title1',
description: 'Description1',
pubDate: 'Mon, 31 Dec 2018 15:59:50 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/1',
author: 'DIYgod1',
},
{
title: 'Title2',
description: 'Description2',
pubDate: 'Mon, 31 Dec 2018 15:59:40 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/2',
author: 'DIYgod2',
},
{
title: 'Title3',
description: 'Description3',
pubDate: 'Mon, 31 Dec 2018 15:59:30 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/3',
author: 'DIYgod3',
},
{
title: 'Title4',
description: 'Description4',
pubDate: 'Mon, 31 Dec 2018 15:59:20 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/4',
author: 'DIYgod4',
},
{
title: 'Title5',
description: 'Description5',
pubDate: 'Mon, 31 Dec 2018 15:59:10 GMT',
link: 'https://github.com/DIYgod/RSSHub/issues/5',
author: 'DIYgod5',
},
],
allowEmpty: false,
});
});

it('error', (done) => {
request('/test/error')
.then(() => {
done();
})
.catch((error) => {
expect(error).toBe('Error test');
done();
});
it('error', async () => {
try {
await request('/test/error');
} catch (error) {
expect(error).toBe('Error test');
}
});
});
2 changes: 1 addition & 1 deletion lib/routes.test.ts
@@ -1,4 +1,4 @@
import { describe, expect, it, afterAll } from '@jest/globals';
import { describe, expect, it, afterAll } from 'vitest';
process.env.SOCKET = 'socket';

import app from '@/app';
Expand Down
30 changes: 17 additions & 13 deletions lib/routes.ts
Expand Up @@ -10,21 +10,25 @@ type Root = {
get: (routePath: string, filePath: string) => void;
};

const imports = directoryImport({
targetDirectoryPath: path.join(__dirname, './routes'),
importPattern: /router\.js$/,
});

const routes: Record<string, (root: Root) => void> = {};

for (const path in imports) {
const name = path.split('/').find(Boolean);
if (name) {
routes[name] = (
imports[path] as {
default: (root: Root) => void;
}
).default;
if (process.env.NODE_ENV === 'test') {
routes.test = (await import('./routes/test/router')).default;
} else {
const imports = directoryImport({
targetDirectoryPath: path.join(__dirname, './routes'),
importPattern: /router\.js$/,
});

for (const path in imports) {
const name = path.split('/').find(Boolean);
if (name) {
routes[name] = (
imports[path] as {
default: (root: Root) => void;
}
).default;
}
}
}

Expand Down

0 comments on commit 38e4215

Please sign in to comment.