-
Notifications
You must be signed in to change notification settings - Fork 3
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
Jestからvitestへの移行 #224
Conversation
package.json
Outdated
"react": "canary", | ||
"react-dom": "canary", | ||
"react": "19.0.0-rc.0", | ||
"react-dom": "19.0.0-rc.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
canaryにすると、ビルドのタイミングで使用されるコードが変化する可能性があるので、バージョンを固定
There was a problem hiding this comment.
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' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beforeEachしたときに初期化されず、beforeEachから出したら初期化されるようになりました
beforeEachから出しています
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vitestだとstubEnvという環境変数を制御するAPIが用意されているようなので、使えるといいかもと思いました
|
||
expectedExisted | ||
? expect(handler[method]).not.toBeUndefined() | ||
: expect(handler[method]).toBeUndefined() |
There was a problem hiding this comment.
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(), | ||
})) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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>, |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nodeのままだと動かなかったので修正しています
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' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vitestだとstubEnvという環境変数を制御するAPIが用意されているようなので、使えるといいかもと思いました
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご対応ありがとうございます!!
fix #219