Skip to content

Commit

Permalink
Merge pull request #468 from zenn-dev/feat-vitest
Browse files Browse the repository at this point in the history
feat: 各パッケージのテストを jest から vitest への移行した
  • Loading branch information
cm-wada-yusuke authored Oct 2, 2023
2 parents c714387 + 3d176ef commit 16d3626
Show file tree
Hide file tree
Showing 55 changed files with 559 additions and 1,870 deletions.
12 changes: 0 additions & 12 deletions packages/zenn-cli/jest.config.client.js

This file was deleted.

31 changes: 0 additions & 31 deletions packages/zenn-cli/jest.config.server.js

This file was deleted.

10 changes: 3 additions & 7 deletions packages/zenn-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,16 @@
"fix:eslint": "eslint --fix 'src/**/*.{ts,tsx}'",
"strict:lint": "eslint 'src/**/*.{ts,tsx}' --max-warnings 0",
"test": "run-s test:client test:server",
"test:client": "jest --config=jest.config.client.js",
"test:server": "jest --config=jest.config.server.js",
"test:client": "vitest run --config vitest.client.config.ts",
"test:server": "vitest run --config vitest.server.config.ts",
"exec:zenn": "node ./dist/server/zenn.js"
},
"devDependencies": {
"@swc/core": "1.2.205",
"@swc/jest": "^0.2.24",
"@types/configstore": "^6.0.0",
"@types/connect-history-api-fallback": "^1.3.5",
"@types/emoji-regex": "^9.2.0",
"@types/express": "^4.17.17",
"@types/fs-extra": "^11.0.1",
"@types/jest": "^29.4.0",
"@types/js-yaml": "^4.0.5",
"@types/node": "^18.13.0",
"@types/node-fetch": "^2.6.2",
Expand Down Expand Up @@ -74,8 +71,6 @@
"gray-matter": "^4.0.3",
"history": "^5.3.0",
"image-size": "^1.0.2",
"jest": "^29.4.2",
"jest-environment-jsdom": "^29.4.2",
"js-yaml": "^4.1.0",
"node-fetch": "^2.6.9",
"node-loader": "^2.0.0",
Expand All @@ -95,6 +90,7 @@
"ts-loader": "^9.4.2",
"typescript": "^4.9.5",
"vite": "^4.2.1",
"vitest": "^0.34.4",
"wait-on": "^7.0.1",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1",
Expand Down
1 change: 1 addition & 0 deletions packages/zenn-cli/src/client/__tests__/lib/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, test, expect } from 'vitest';
import { encodeUrlPeriod, decodeUrlPeriod } from '../../lib/helper';

describe('encodeUrlPeriod と decodeUrlPeriod を使った処理のテスト', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/zenn-cli/src/common/__tests__/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, test, expect } from 'vitest';
import * as helper from '../helper';

describe('validateSlug() のテスト', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/zenn-cli/src/server/__tests__/commands/help.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import { exec } from '../../commands/help';
import { commandListText } from '../../lib/messages';

describe('helpコマンドのテスト', () => {
beforeEach(() => {
console.log = jest.fn();
console.log = vi.fn();
});

test('ヘルプメッセージを表示する', () => {
Expand Down
10 changes: 6 additions & 4 deletions packages/zenn-cli/src/server/__tests__/commands/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { vi, describe, test, expect, beforeEach, SpyInstance } from 'vitest';
import { exec } from '../../commands/index';
import * as Log from '../../lib/log';
import { commandListText } from '../../lib/messages';
import * as notify from '../../lib/notify-update';

describe('CLIのデフォルトの挙動のテスト', () => {
let notifyNeedUpdateCLIMock: jest.SpyInstance<Promise<void>>;
let notifyNeedUpdateCLIMock: SpyInstance<any[], Promise<void>>;

beforeEach(() => {
// mock
console.log = jest.fn();
jest.spyOn(Log, 'error').mockImplementation();
notifyNeedUpdateCLIMock = jest
console.log = vi.fn();
console.error = vi.fn();
vi.spyOn(Log, 'error');
notifyNeedUpdateCLIMock = vi
.spyOn(notify, 'notifyNeedUpdateCLI')
.mockResolvedValue();
});
Expand Down
7 changes: 4 additions & 3 deletions packages/zenn-cli/src/server/__tests__/commands/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import path from 'path';
import { exec } from '../../commands/init';
import * as helper from '../../lib/helper';
Expand All @@ -6,9 +7,9 @@ import { initHelpText } from '../../lib/messages';
describe('initコマンドのテスト', () => {
beforeEach(() => {
// mock
jest.spyOn(helper, 'generateFileIfNotExist').mockImplementation();
console.log = jest.fn();
console.error = jest.fn();
vi.spyOn(helper, 'generateFileIfNotExist').mockReturnValue(undefined);
console.log = vi.fn();
console.error = vi.fn();
});

test('ディレクトリに対して generateFileIfNotExist を実行する', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import path from 'path';
import { exec } from '../../commands/new-article';
import * as helper from '../../lib/helper';
Expand All @@ -9,9 +10,9 @@ describe('new:articleコマンドのテスト', () => {

beforeEach(() => {
// mock
jest.spyOn(helper, 'generateFileIfNotExist').mockImplementation();
jest.spyOn(Log, 'error').mockImplementation();
console.log = jest.fn();
vi.spyOn(helper, 'generateFileIfNotExist').mockReturnValue(undefined);
vi.spyOn(Log, 'error').mockReturnValue(undefined);
console.log = vi.fn();
});

test('有効な引数に generateFileIfNotExist を実行する', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import path from 'path';
import { exec } from '../../commands/new-book';
import * as helper from '../../lib/helper';
Expand All @@ -15,9 +16,9 @@ describe('new:book コマンドのテスト', () => {

beforeEach(() => {
// mock
jest.spyOn(helper, 'generateFileIfNotExist').mockImplementation();
jest.spyOn(Log, 'error').mockImplementation();
console.log = jest.fn();
vi.spyOn(helper, 'generateFileIfNotExist').mockReturnValue(undefined);
vi.spyOn(Log, 'error').mockReturnValue(undefined);
console.log = vi.fn();
});

test('config.yaml のデフォルト値で generateFileIfNotExist を実行する', () => {
Expand Down
13 changes: 9 additions & 4 deletions packages/zenn-cli/src/server/__tests__/commands/preview.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import type { Server as HttpServer } from 'http';
import { exec } from '../../commands/preview';
import { previewHelpText } from '../../lib/messages';
import * as server from '../../lib/server';

describe('preview コマンドのテスト', () => {
let mockReturnServer: HttpServer;

beforeEach(() => {
console.log = jest.fn();
jest.spyOn(server, 'startServer').mockImplementation();
jest.spyOn(server, 'startLocalChangesWatcher').mockImplementation();
console.log = vi.fn();
mockReturnServer = {} as HttpServer;
vi.spyOn(server, 'startServer').mockResolvedValue(mockReturnServer);
vi.spyOn(server, 'startLocalChangesWatcher').mockResolvedValue(undefined);
});

test('--help オプションを渡すとヘルプメッセージを表示する', async () => {
Expand Down Expand Up @@ -63,7 +68,7 @@ describe('preview コマンドのテスト', () => {
test('デフォルトでは startLocalChangesWatcher を実行する', async () => {
await exec([]);
expect(server.startLocalChangesWatcher).toHaveBeenCalledWith(
undefined,
mockReturnServer,
`${process.cwd()}/{articles,books}/**/*`
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import { exec } from '../../commands/version';

describe('version コマンドのテスト', () => {
console.log = jest.fn();
console.log = vi.fn();
beforeEach(() => {
console.log = jest.fn();
console.log = vi.fn();
});

test('バージョン情報をコンソールに出力する', () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/zenn-cli/src/server/__tests__/lib/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi, describe, test, expect, beforeEach, afterEach } from 'vitest';
import path from 'path';
import fs from 'fs-extra';
import * as helper from '../../lib/helper';
Expand All @@ -24,9 +25,9 @@ describe('getCurrentCliVersion() のテスト', () => {
describe('getWorkingPath() のテスト', () => {
beforeEach(() => {
// mock
jest.spyOn(process, 'cwd').mockReturnValue('foo');
jest.spyOn(process, 'exit').mockImplementation();
jest.spyOn(Log, 'error').mockImplementation();
vi.spyOn(process, 'cwd').mockReturnValue('foo');
vi.spyOn(process, 'exit').mockReturnValue(undefined as never);
vi.spyOn(Log, 'error').mockReturnValue(undefined);
});

test('結合されたパスを返す', () => {
Expand Down Expand Up @@ -156,10 +157,10 @@ describe('generateFileIfNotExist() のテスト', () => {
describe('completeHtml() のテスト', () => {
beforeEach(() => {
// process.cwdがfixturesディレクトリを指すようにする
jest.spyOn(process, 'cwd').mockReturnValue(fixtureDirPath);
vi.spyOn(process, 'cwd').mockReturnValue(fixtureDirPath);
});

test.only('src の値が URL なら検証に成功する', () => {
test('src の値が URL なら検証に成功する', () => {
const html = helper.completeHtml(
'<img src="https://example.com/images/example.png">'
);
Expand Down
7 changes: 4 additions & 3 deletions packages/zenn-cli/src/server/__tests__/lib/log.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { vi, describe, test, expect, beforeEach } from 'vitest';
import * as Log from '../../lib/log';
import colors from 'colors/safe';

describe('Log のテスト', () => {
beforeEach(() => {
// mock
console.log = jest.fn();
console.warn = jest.fn();
console.error = jest.fn();
console.log = vi.fn();
console.warn = vi.fn();
console.error = vi.fn();
});

test('エラーメッセージを表示する', () => {
Expand Down
33 changes: 16 additions & 17 deletions packages/zenn-cli/src/server/__tests__/lib/notify-update.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { vi, describe, test, expect, beforeEach, SpyInstance } from 'vitest';
import * as helper from '../../lib/helper';
import {
CLI_UPDATE_CHECK_INTERVAL,
Expand All @@ -7,30 +8,28 @@ import {
const localVersion = 'v0.0.1';
const publishedVersion = 'v0.0.2';

const configGetMock = jest.fn();
const configGetMock = vi.fn();

jest.mock(
'configstore',
() =>
function () {
return {
set: () => void 0,
get: configGetMock.mockReturnValue(0),
};
}
);
vi.mock('configstore', () => ({
default: function () {
return {
set: () => void 0,
get: configGetMock.mockReturnValue(0),
};
},
}));

describe('CLIのアップデート通知のテスト', () => {
let consoleLogMock: jest.SpyInstance;
let getCurrentCliVersionMock: jest.SpyInstance;
let getPublishedCliVersionMock: jest.SpyInstance;
let consoleLogMock: SpyInstance;
let getCurrentCliVersionMock: SpyInstance;
let getPublishedCliVersionMock: SpyInstance;

beforeEach(() => {
consoleLogMock = jest.spyOn(console, 'log').mockImplementation();
getCurrentCliVersionMock = jest
consoleLogMock = vi.spyOn(console, 'log').mockReturnValue(undefined);
getCurrentCliVersionMock = vi
.spyOn(helper, 'getCurrentCliVersion')
.mockReturnValue(localVersion);
getPublishedCliVersionMock = jest
getPublishedCliVersionMock = vi
.spyOn(helper, 'getPublishedCliVersion')
.mockResolvedValue(publishedVersion);
});
Expand Down
Loading

0 comments on commit 16d3626

Please sign in to comment.