-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 [testing improvement] Add tests for handleRateLimit in apiUtils #365
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||||||||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||||||||||||||||||
| import { handleErrorResponse, getAuthenticatedUser } from '../apiUtils'; | ||||||||||||||||||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; | ||||||||||||||||||
| import { handleErrorResponse, getAuthenticatedUser, handleRateLimit } from '../apiUtils'; | ||||||||||||||||||
| import { RateLimitError } from '../types'; | ||||||||||||||||||
| import { NextResponse } from 'next/server'; | ||||||||||||||||||
| import { getServerSession } from "next-auth"; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -56,6 +57,45 @@ | |||||||||||||||||
| }); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| describe('handleRateLimit', () => { | ||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||
| vi.useFakeTimers(); | ||||||||||||||||||
| vi.setSystemTime(new Date(1000000)); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| afterEach(() => { | ||||||||||||||||||
| vi.useRealTimers(); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| it('should throw RateLimitError with timestamp from X-RateLimit-Reset header', () => { | ||||||||||||||||||
| const res = new Response(null, { | ||||||||||||||||||
| headers: { 'X-RateLimit-Reset': '2000' } | ||||||||||||||||||
| }); | ||||||||||||||||||
| expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({ | ||||||||||||||||||
| name: 'RateLimitError', | ||||||||||||||||||
| resetAt: new Date(2000000) | ||||||||||||||||||
| })); | ||||||||||||||||||
|
Comment on lines
+74
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
現在は
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/lib/__tests__/apiUtils.test.ts
Line: 74-77
Comment:
**`RateLimitError` インポートを活用した `instanceof` チェックへの改善**
現在は `name: 'RateLimitError'` という文字列で確認しているため、同じ `name` プロパティを持つ別クラスでもテストがパスしてしまいます。インポート済みの `RateLimitError` を使い `.toThrow(RateLimitError)` で型を明示的に検証することで、より堅牢なアサーションになります。
```suggestion
expect(() => handleRateLimit(res)).toThrow(RateLimitError);
expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({
resetAt: new Date(2000000)
}));
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+70
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using it('should throw RateLimitError with timestamp from X-RateLimit-Reset header', (): void => {
const res = new Response(null, {
headers: { 'X-RateLimit-Reset': '2000' }
});
expect((): void => { handleRateLimit(res); }).toThrow(RateLimitError);
try {
handleRateLimit(res);
} catch (error) {
expect((error as RateLimitError).resetAt).toEqual(new Date(2000000));
}
});References
|
||||||||||||||||||
|
|
||||||||||||||||||
| it('should throw RateLimitError with fallback timestamp if header is missing', () => { | ||||||||||||||||||
| const res = new Response(); | ||||||||||||||||||
| expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({ | ||||||||||||||||||
| name: 'RateLimitError', | ||||||||||||||||||
| resetAt: new Date(4600000) | ||||||||||||||||||
| })); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+80
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using it('should throw RateLimitError with fallback timestamp if header is missing', (): void => {
const res = new Response();
expect((): void => { handleRateLimit(res); }).toThrow(RateLimitError);
try {
handleRateLimit(res);
} catch (error) {
expect((error as RateLimitError).resetAt).toEqual(new Date(4600000));
}
});References
|
||||||||||||||||||
|
|
||||||||||||||||||
| it('should throw RateLimitError with fallback timestamp if header is invalid', () => { | ||||||||||||||||||
| const res = new Response(null, { | ||||||||||||||||||
| headers: { 'X-RateLimit-Reset': 'invalid' } | ||||||||||||||||||
| }); | ||||||||||||||||||
| expect(() => handleRateLimit(res)).toThrowError(expect.objectContaining({ | ||||||||||||||||||
| name: 'RateLimitError', | ||||||||||||||||||
| resetAt: new Date(4600000) | ||||||||||||||||||
| })); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+88
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using it('should throw RateLimitError with fallback timestamp if header is invalid', (): void => {
const res = new Response(null, {
headers: { 'X-RateLimit-Reset': 'invalid' }
});
expect((): void => { handleRateLimit(res); }).toThrow(RateLimitError);
try {
handleRateLimit(res);
} catch (error) {
expect((error as RateLimitError).resetAt).toEqual(new Date(4600000));
}
});References
|
||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| describe('getAuthenticatedUser', () => { | ||||||||||||||||||
| it('should return user object if session is valid', async () => { | ||||||||||||||||||
| vi.mocked(getServerSession).mockResolvedValueOnce({ | ||||||||||||||||||
|
|
||||||||||||||||||
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.
RateLimitErrorRateLimitErrorクラスがインポートされていますが、テスト内のアサーションでは文字列リテラル'RateLimitError'のみを使用しており、クラス自体は一切参照されていません。このインポートは削除するか、expect.any(RateLimitError)や.toThrow(RateLimitError)によるinstanceofチェックとして活用することを推奨します。文字列比較だとnameプロパティを手動で設定したあらゆるエラーが誤ってパスしてしまうため、型安全性が低くなります。Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!