Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 各パッケージのテストを jest から vitest への移行した #468

Merged
merged 9 commits into from
Oct 2, 2023

Conversation

uttk-dev
Copy link
Contributor

📑 Summary

packages 内のパッケージのテストを jest から vitest へ移行しました。

Resolves #419

📋 Tasks

プルリクエストを作成いただく際、お手数ですが以下の内容についてご確認をお願いします。

  • 📖 Contribution Guide を読んだ
  • 👩‍💻 canary ブランチに対するプルリクエストである
  • zenn-cli で実行して正しく動作しているか確認する
  • 不要なコードが含まれていないか( コメントやログの消し忘れに注意 )
  • XSS になるようなコードが含まれていないか
  • モバイル端末での表示が考慮されているか
  • Pull Reuqest の内容は妥当か( 膨らみすぎてないか )

より詳しい内容は Pull Request Policy を参照してください。

@uttk-dev uttk-dev added the feature 機能追加・改善 label Sep 21, 2023
@uttk-dev uttk-dev self-assigned this Sep 21, 2023
@@ -1,3 +1,4 @@
import { describe, test, expect } from 'vitest';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import しないグローバルAPI方式もありますが、TypeScript の型エラーが発生して別途設定が必要になってくるので、あえて import する形式で実装しています。

Comment on lines +13 to +14
console.error = vi.fn();
vi.spyOn(Log, 'error');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vitest では .mockImplementation() の引数が必須なので、console.error() をモックする形で実装しました。そのほうが、Log.error() の内部処理をなるべく実行しつつテストできるので。

jest.spyOn(helper, 'generateFileIfNotExist').mockImplementation();
console.log = jest.fn();
console.error = jest.fn();
vi.spyOn(helper, 'generateFileIfNotExist').mockReturnValue(undefined);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vitest だと .mockImplementation() の引数が必須になるので、.mockReturnValue(undefined) で対応しています。( 他のファイルも同様です )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

副作用系は undefined になるイメージですね。

jest.spyOn(process, 'exit').mockImplementation();
jest.spyOn(Log, 'error').mockImplementation();
vi.spyOn(process, 'cwd').mockReturnValue('foo');
vi.spyOn(process, 'exit').mockReturnValue(undefined as never);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.exit() の返り値が never なので as never を使って対応する必要がありました。

@@ -1 +1 @@
export type CliExecFn = (argv?: string[]) => void;
export type CliExecFn = (argv?: string[]) => void | Promise<void>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

より正確な型付けを行いました。動作に影響はありません。

Comment on lines +6 to +7
// markdownToHtml で diff を使っているので、あらかじめ読み込んでおく
loadLanguages('diff');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本来は Babel を使って自動で行っている処理ですが、vitest だと Babel がうまく動作しなかったので、手動で loadLanguages() を実行しています。

@uttk-dev uttk-dev changed the title feat: jest から vitest への移行途中 feat: 各パッケージのテストを jest から vitest への移行した Sep 21, 2023
@cm-wada-yusuke
Copy link
Member

ローカルでテストが成功しないため確認中です。私の環境の問題の可能性高。

zenn-cli:test:  FAIL  src/server/__tests__/commands/index.test.ts [ src/server/__tests__/commands/index.test.ts ]
zenn-cli:test:  FAIL  src/server/__tests__/commands/preview.test.ts [ src/server/__tests__/commands/preview.test.ts ]
zenn-cli:test:  FAIL  src/server/__tests__/preview/app.test.ts [ src/server/__tests__/preview/app.test.ts ]
zenn-cli:test: Error: Failed to load url zenn-markdown-html/lib/utils (resolved id: zenn-markdown-html/lib/utils) in /Users/wada.yusuke/ghq/github.com/zenn-dev/zenn-editor/packages/zenn-cli/src/server/lib/articles.ts. Does the file exist?

@cm-wada-yusuke
Copy link
Member

リポジトリを作り直して解決しました

Copy link
Member

@cm-wada-yusuke cm-wada-yusuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@cm-wada-yusuke cm-wada-yusuke merged commit 16d3626 into canary Oct 2, 2023
3 checks passed
@cm-wada-yusuke cm-wada-yusuke deleted the feat-vitest branch October 2, 2023 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 機能追加・改善
Projects
None yet
Development

Successfully merging this pull request may close these issues.

jest から vitest に移行する
2 participants