refactor: replace require with import for JSON module in vitest.setup.ts

This commit is contained in:
雨霖铃
2026-05-29 22:08:04 +08:00
parent f9fbc79612
commit 093bf278da
+6 -7
View File
@@ -1,11 +1,10 @@
import '@testing-library/jest-dom';
import { afterEach, beforeEach, vi } from 'vitest';
import React from 'react';
import zhMessages from './public/_locales/zh/messages.json';
// Load actual zh translations for getMessage mock
const zhMessages: Record<string, { message: string }> =
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('./public/_locales/zh/messages.json');
// Type assertion to allow string indexing
const zhMessagesMap = zhMessages as Record<string, { message: string }>;
vi.mock('@/utils/chromeI18n', () => ({
useI18n: (ns?: string | string[]) => ({
@@ -16,13 +15,13 @@ vi.mock('@/utils/chromeI18n', () => ({
msgId = key.replace(':', '_').replace(/\./g, '_');
}
// Try direct key first
if (zhMessages[msgId]) return zhMessages[msgId].message;
if (zhMessagesMap[msgId]) return zhMessagesMap[msgId].message;
// Try namespace prefix (using converted msgId)
if (ns) {
const namespaces = Array.isArray(ns) ? ns : [ns];
for (const n of namespaces) {
const candidate = `${n}_${msgId}`;
if (zhMessages[candidate]) return zhMessages[candidate].message;
if (zhMessagesMap[candidate]) return zhMessagesMap[candidate].message;
}
}
return msgId;
@@ -33,7 +32,7 @@ vi.mock('@/utils/chromeI18n', () => ({
},
isLoaded: true,
}),
getMessage: (msgId: string) => zhMessages[msgId]?.message ?? msgId,
getMessage: (msgId: string) => zhMessagesMap[msgId]?.message ?? msgId,
getLanguage: () => 'zh',
preloadNamespaces: vi.fn().mockResolvedValue(undefined),
}));