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

Jestからvitestへの移行 #224

Merged
merged 32 commits into from
Jul 7, 2024

Conversation

mongolyy
Copy link
Contributor

@mongolyy mongolyy commented Jun 4, 2024

fix #219

@mongolyy mongolyy changed the title [WIP] Feature/219 migrate to vitest [WIP] Jestからvitestへの移行 Jun 4, 2024
package.json Outdated
"react": "canary",
"react-dom": "canary",
"react": "19.0.0-rc.0",
"react-dom": "19.0.0-rc.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

canaryにすると、ビルドのタイミングで使用されるコードが変化する可能性があるので、バージョンを固定

Copy link
Contributor Author

Choose a reason for hiding this comment

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

また、非同期コンポーネントのテストについて、canaryと19 rcで動くようだったので、19 rcを使用しています
testing-library/react-testing-library#1209 (comment)

process.env.AZURE_AD_B2C_TENANT_NAME = 'tenantNameAADB2C'
process.env.AZURE_AD_B2C_CLIENT_ID = 'clientIdAADB2C'
process.env.AZURE_AD_B2C_CLIENT_SECRET = 'clientSecretAADB2C'
process.env.AZURE_AD_B2C_PRIMARY_USER_FLOW = 'primaryUserFlowAADB2C'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

beforeEachしたときに初期化されず、beforeEachから出したら初期化されるようになりました
beforeEachから出しています

Copy link
Contributor

Choose a reason for hiding this comment

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

badge
vitestだとstubEnvという環境変数を制御するAPIが用意されているようなので、使えるといいかもと思いました


expectedExisted
? expect(handler[method]).not.toBeUndefined()
: expect(handler[method]).toBeUndefined()
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の仕様で、requireを使うのが難しく、importを使う必要がありました
importを使うと型情報が入ってきてしまい、この書き方をするときにts-ignoreの必要がありました

このテストについて、関数が定義されているかどうかのテストであり、実質buildすれば確認できる内容だったので削除しました

jest.mock('next/navigation', () => ({
__esModule: true,
redirect: () => redirectMock(),
}))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

他のテストコードも同様ですが、もともとmockはdescribe内で定義していたと思います
グローバルな場所で宣言しているよりはdescribe内にある方がテスト内で使われることがわかりやすいと感じたので、移動させました

@@ -37,12 +41,16 @@ describe('LendingList Component', () => {

it('貸出中のユーザーがいる場合、その一覧が返却予定日の昇順で表示される', async () => {
// @ts-ignore
prismaLendingHistoryMock.mockResolvedValueOnce(expectedLendingHistories)
prismaLendingHistoryMock.mockResolvedValue(expectedLendingHistories)
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に書き換えるときにxxxOnceにしても挙動は変えられず、xxxだと挙動が帰られたので変更しています
他の行も同様です

describe('BookDetail component', () => {
const BookDetailComponent = require('@/app/books/[id]/bookDetail').default
describe('BookDetail component', async () => {
const BookDetailComponent = (await import('@/app/books/[id]/bookDetail')).default
Copy link
Contributor Author

Choose a reason for hiding this comment

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

他も同様ですが、test対象のコンポーネントはawait importでimportしています
https://vitest.dev/api/vi#vi-hoisted

render(
<Suspense>
<BookDetailPage params={{ id: book.id.toString() }} />
</Suspense>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

async componentのテストの書き方はまだ公式に認められたものはなさそうです
ただ、今後でラップするタイプが標準になりそうだったので、それに追従しています
testing-library/react-testing-library#1209 (comment)

@@ -13,7 +13,7 @@
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"moduleResolution": "Bundler",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

nodeのままだと動かなかったので修正しています

@mongolyy mongolyy changed the title [WIP] Jestからvitestへの移行 Jestからvitestへの移行 Jun 10, 2024
@mongolyy mongolyy marked this pull request as ready for review June 10, 2024 12:36
@mongolyy mongolyy requested a review from a team as a code owner June 10, 2024 12:36
@mongolyy mongolyy requested a review from katakoto291 June 10, 2024 12:36
process.env.AZURE_AD_B2C_TENANT_NAME = 'tenantNameAADB2C'
process.env.AZURE_AD_B2C_CLIENT_ID = 'clientIdAADB2C'
process.env.AZURE_AD_B2C_CLIENT_SECRET = 'clientSecretAADB2C'
process.env.AZURE_AD_B2C_PRIMARY_USER_FLOW = 'primaryUserFlowAADB2C'
Copy link
Contributor

Choose a reason for hiding this comment

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

badge
vitestだとstubEnvという環境変数を制御するAPIが用意されているようなので、使えるといいかもと思いました

test/app/books/id/bookDetail.test.tsx Outdated Show resolved Hide resolved
test/app/books/register/addBookDiv.test.tsx Outdated Show resolved Hide resolved
test/app/books/register/bookForm.test.tsx Outdated Show resolved Hide resolved
test/app/books/register/searchedBook.test.tsx Outdated Show resolved Hide resolved
test/app/users/userCard.test.tsx Outdated Show resolved Hide resolved
test/components/userAvatar.test.tsx Show resolved Hide resolved
vitest.setup.ts Outdated Show resolved Hide resolved
vitest.setup.ts Outdated Show resolved Hide resolved
test/components/bookTile.test.tsx Outdated Show resolved Hide resolved
@mongolyy mongolyy self-assigned this Jun 30, 2024
@mongolyy
Copy link
Contributor Author

mongolyy commented Jul 7, 2024

vitestだとstubEnvという環境変数を制御するAPIが用意されているようなので、使えるといいかもと思いました

ありがとうございます!
対応しました 3612538

@mongolyy mongolyy requested a review from katakoto291 July 7, 2024 08:32
Copy link
Contributor

@katakoto291 katakoto291 left a comment

Choose a reason for hiding this comment

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

ご対応ありがとうございます!!

@mongolyy mongolyy merged commit 78677bc into company-library:main Jul 7, 2024
6 checks passed
@mongolyy mongolyy deleted the feature/219_migrate-to-vitest branch July 7, 2024 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vitest に切り替える
2 participants