|
1 | 1 | import { Hono } from 'hono' |
2 | 2 | import { afterEach, describe, expect, test, vi } from 'vitest' |
| 3 | +import { delay as sleep } from '../../shared/helper.ts' |
3 | 4 | import { |
4 | 5 | defineI18nMiddleware, |
5 | 6 | detectLocaleFromAcceptLanguageHeader, |
@@ -79,15 +80,15 @@ describe('custom locale detection', () => { |
79 | 80 | expect(await res.json()).toEqual({ message: 'こんにちは, hono' }) |
80 | 81 | }) |
81 | 82 |
|
82 | | - test('detect with async loading', async () => { |
83 | | - const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) |
84 | | - |
85 | | - const loader = (path: string) => import(path).then(m => m.default || m) |
86 | | - const messages: Record<string, () => ReturnType<typeof loader>> = { |
87 | | - en: () => loader('./fixtures/en.json'), |
88 | | - ja: () => loader('./fixtures/ja.json') |
89 | | - } |
| 83 | + const messages: Record< |
| 84 | + string, |
| 85 | + () => Promise<typeof import('./fixtures/en.json') | typeof import('./fixtures/ja.json')> |
| 86 | + > = { |
| 87 | + en: () => import('./fixtures/en.json', { with: { type: 'json' } }).then(m => m.default), |
| 88 | + ja: () => import('./fixtures/ja.json', { with: { type: 'json' } }).then(m => m.default) |
| 89 | + } |
90 | 90 |
|
| 91 | + test('detect with async loading', async () => { |
91 | 92 | // async locale detector |
92 | 93 | const localeDetector = async (ctx: Context, i18n: CoreContext<string, DefineLocaleMessage>) => { |
93 | 94 | const locale = getQueryLocale(ctx.req.raw).toString() |
@@ -135,14 +136,6 @@ describe('custom locale detection', () => { |
135 | 136 | }) |
136 | 137 |
|
137 | 138 | test('detect with async parallel loading', async () => { |
138 | | - const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) |
139 | | - |
140 | | - const loader = (path: string) => import(path).then(m => m.default || m) |
141 | | - const messages: Record<string, () => ReturnType<typeof loader>> = { |
142 | | - en: () => loader('./fixtures/en.json'), |
143 | | - ja: () => loader('./fixtures/ja.json') |
144 | | - } |
145 | | - |
146 | 139 | // async locale detector |
147 | 140 | const localeDetector = async (ctx: Context, i18n: CoreContext<string, DefineLocaleMessage>) => { |
148 | 141 | const locale = getQueryLocale(ctx.req.raw).toString() |
@@ -181,14 +174,13 @@ describe('custom locale detection', () => { |
181 | 174 | message: 'こんにちは, hono' |
182 | 175 | } |
183 | 176 | } |
| 177 | + |
184 | 178 | // request in parallel |
185 | | - const resList = await Promise.all( |
186 | | - ['en', 'ja'].map(locale => |
187 | | - app |
188 | | - .request(`/?locale=${locale}`) |
189 | | - // @ts-ignore |
190 | | - .then(res => res.json()) |
191 | | - ) |
| 179 | + const resList: { message: string }[] = await Promise.all( |
| 180 | + ['en', 'ja'].map(async (locale): Promise<{ message: string }> => { |
| 181 | + const res: Response = await app.request(`/?locale=${locale}`) |
| 182 | + return (await res.json()) as { message: string } |
| 183 | + }) |
192 | 184 | ) |
193 | 185 | expect(resList).toEqual([translated['en'], translated['ja']]) |
194 | 186 | }) |
|
0 commit comments