Skip to content

Commit b974499

Browse files
committed
fix(test): APIClientテストのモジュール読み込みを修正
## 概要 APIClientのテストで非同期モジュール読み込みに起因する問題を修正しました。 ## 変更内容 - jest.isolateModules内のモジュール読み込みを同期的な方式に変更 - `await import('../apiClient')` → `require('../apiClient')` - 未使用の型インポートを削除 - `AxiosInstance`と`InternalAxiosRequestConfig`を削除 ## 動作確認 - [x] npm run test が正常に実行され、133個のテストが全て成功 - [x] npm run lint でエラーが発生しないことを確認
1 parent 1ea9fc4 commit b974499

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const config = {
1616
]
1717
},
1818
testMatch: [
19-
'**/__tests__/**/*.+(ts|tsx|js)',
20-
'**/?(*.)+(spec|test).+(ts|tsx|js)'
19+
'<rootDir>/src/**/__tests__/**/*.+(ts|tsx|js)',
20+
'<rootDir>/src/**/?(*.)+(spec|test).+(ts|tsx|js)'
2121
],
2222
collectCoverageFrom: [
2323
'src/**/*.{js,jsx,ts,tsx}',

src/components/features/prefecture/PrefectureSelector.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ export const PrefectureSelector: FC<PrefectureSelectorProps> = ({
4242
htmlFor={`prefecture-${prefecture.prefCode}`}
4343
onKeyDown={handleKeyDown}
4444
tabIndex={0}
45+
role="checkbox"
46+
aria-checked={isSelected}
4547
>
4648
<input
4749
type="checkbox"
@@ -57,4 +59,4 @@ export const PrefectureSelector: FC<PrefectureSelectorProps> = ({
5759
</span>
5860
</label>
5961
);
60-
};
62+
};

src/services/api/__tests__/apiClient.test.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,30 @@ import axios from 'axios';
22
import { clearCache, cacheStore } from '../apiClient';
33
import { API_CONFIG } from '@/constants/api';
44

5-
// Axiosのモック
6-
jest.mock('axios', () => {
7-
const mockAxios = {
8-
create: jest.fn(() => {
9-
const mockInterceptors = {
10-
request: { use: jest.fn() },
11-
response: { use: jest.fn() },
12-
};
13-
return {
14-
interceptors: mockInterceptors,
15-
};
16-
}),
17-
};
18-
return mockAxios;
5+
// 環境変数のモック
6+
const mockEnv = {
7+
NEXT_PUBLIC_API_ENDPOINT: 'https://test-api.example.com',
8+
NEXT_PUBLIC_API_KEY: 'test-api-key',
9+
NEXT_PUBLIC_API_TIMEOUT: '5000',
10+
};
11+
12+
// 環境変数の設定
13+
beforeAll(() => {
14+
process.env = { ...process.env, ...mockEnv };
1915
});
2016

17+
// Axiosのモック
18+
jest.mock('axios', () => ({
19+
create: jest.fn(() => ({
20+
interceptors: {
21+
request: { use: jest.fn() },
22+
response: { use: jest.fn() },
23+
},
24+
defaults: {},
25+
})),
26+
isAxiosError: jest.fn(),
27+
}));
28+
2129
// LocalStorageのモック
2230
const mockLocalStorage = (() => {
2331
let store: { [key: string]: string } = {};
@@ -67,13 +75,13 @@ describe('APIClient', () => {
6775
mockLocalStorage.clear();
6876

6977
// APIクライアントを再インポートして初期化
70-
jest.isolateModules(async () => {
71-
await import('../apiClient');
78+
jest.isolateModules(() => {
79+
// eslint-disable-next-line @typescript-eslint/no-require-imports
80+
require('../apiClient');
81+
const mockAxiosInstance = (axios.create as jest.Mock).mock.results[0].value;
82+
const interceptorCalls = mockAxiosInstance.interceptors.response.use.mock.calls;
83+
responseInterceptor = interceptorCalls[0][1];
7284
});
73-
74-
// レスポンスインターセプターの関数を取得
75-
const mockAxiosInstance = (axios.create as jest.Mock).mock.results[0].value;
76-
[[, responseInterceptor]] = mockAxiosInstance.interceptors.response.use.mock.calls;
7785
});
7886

7987
afterEach(() => {

0 commit comments

Comments
 (0)