|
1 | | -import { createRequire } from 'node:module' |
2 | | -import path from 'node:path' |
3 | | - |
4 | 1 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
5 | 2 |
|
6 | | -// Mock node:module. |
7 | | -vi.mock('node:module', () => ({ |
8 | | - createRequire: vi.fn(() => vi.fn()), |
9 | | -})) |
10 | | - |
11 | | -// Mock constants. |
12 | | -vi.mock('../constants.mts', () => ({ |
| 3 | +// Mock the JSON import. |
| 4 | +vi.mock('../../../data/alert-translations.json', () => ({ |
13 | 5 | default: { |
14 | | - rootPath: '/mock/root/path', |
| 6 | + alerts: { |
| 7 | + badEncoding: { |
| 8 | + description: |
| 9 | + 'Source files are encoded using a non-standard text encoding.', |
| 10 | + emoji: '⚠️', |
| 11 | + suggestion: |
| 12 | + 'Ensure all published files are encoded using a standard encoding such as UTF8, UTF16, UTF32, SHIFT-JIS, etc.', |
| 13 | + title: 'Bad text encoding', |
| 14 | + }, |
| 15 | + badSemver: { |
| 16 | + description: |
| 17 | + 'Package version is not a valid semantic version (semver).', |
| 18 | + emoji: '⚠️', |
| 19 | + suggestion: |
| 20 | + 'All versions of all packages on npm should use use a valid semantic version. Publish a new version of the package with a valid semantic version. Semantic version ranges do not work with invalid semantic versions.', |
| 21 | + title: 'Bad semver', |
| 22 | + }, |
| 23 | + }, |
15 | 24 | }, |
16 | 25 | })) |
17 | 26 |
|
18 | 27 | describe('translations utilities', () => { |
19 | | - let mockRequire: ReturnType<typeof vi.fn> |
20 | | - let mockTranslations: Record<string, any> |
21 | | - |
22 | 28 | beforeEach(() => { |
23 | 29 | vi.clearAllMocks() |
24 | | - // Reset the module-level cache by clearing the module from cache. |
25 | | - vi.resetModules() |
26 | | - |
27 | | - mockTranslations = { |
28 | | - messages: { |
29 | | - hello: 'Hello', |
30 | | - goodbye: 'Goodbye', |
31 | | - }, |
32 | | - errors: { |
33 | | - notFound: 'Not found', |
34 | | - unauthorized: 'Unauthorized', |
35 | | - }, |
36 | | - } |
37 | | - |
38 | | - mockRequire = vi.fn(() => mockTranslations) |
39 | | - vi.mocked(createRequire).mockReturnValue(mockRequire) |
40 | 30 | }) |
41 | 31 |
|
42 | 32 | describe('getTranslations', () => { |
43 | | - it('loads translations from the correct path', async () => { |
44 | | - // Re-import to get fresh module with reset cache. |
45 | | - const { getTranslations: getTranslationsFresh } = await import( |
46 | | - './translations.mts' |
47 | | - ) |
48 | | - |
49 | | - const result = getTranslationsFresh() |
50 | | - |
51 | | - expect(mockRequire).toHaveBeenCalledWith( |
52 | | - path.join('/mock/root/path', 'translations.json'), |
53 | | - ) |
54 | | - expect(result).toBe(mockTranslations) |
| 33 | + it('returns the translations object', async () => { |
| 34 | + const { getTranslations } = await import('./translations.mts') |
| 35 | + |
| 36 | + const result = getTranslations() |
| 37 | + |
| 38 | + expect(result).toHaveProperty('alerts') |
| 39 | + expect(result.alerts).toHaveProperty('badEncoding') |
| 40 | + expect(result.alerts).toHaveProperty('badSemver') |
55 | 41 | }) |
56 | 42 |
|
57 | | - it('caches translations after first load', async () => { |
58 | | - // Re-import to get fresh module with reset cache. |
59 | | - const { getTranslations: getTranslationsFresh } = await import( |
60 | | - './translations.mts' |
61 | | - ) |
| 43 | + it('returns consistent results on multiple calls', async () => { |
| 44 | + const { getTranslations } = await import('./translations.mts') |
62 | 45 |
|
63 | | - const result1 = getTranslationsFresh() |
64 | | - const result2 = getTranslationsFresh() |
65 | | - const result3 = getTranslationsFresh() |
| 46 | + const result1 = getTranslations() |
| 47 | + const result2 = getTranslations() |
| 48 | + const result3 = getTranslations() |
66 | 49 |
|
67 | | - // Should only require once. |
68 | | - expect(mockRequire).toHaveBeenCalledTimes(1) |
69 | | - // Should return the same object. |
| 50 | + // Should return the same object reference. |
70 | 51 | expect(result1).toBe(result2) |
71 | 52 | expect(result2).toBe(result3) |
72 | | - expect(result1).toBe(mockTranslations) |
73 | 53 | }) |
74 | 54 |
|
75 | | - it('returns the translations object', async () => { |
76 | | - // Re-import to get fresh module with reset cache. |
77 | | - const { getTranslations: getTranslationsFresh } = await import( |
78 | | - './translations.mts' |
79 | | - ) |
| 55 | + it('includes alert properties', async () => { |
| 56 | + const { getTranslations } = await import('./translations.mts') |
80 | 57 |
|
81 | | - const result = getTranslationsFresh() |
| 58 | + const result = getTranslations() |
82 | 59 |
|
83 | | - expect(result).toHaveProperty('messages') |
84 | | - expect(result).toHaveProperty('errors') |
85 | | - expect(result.messages.hello).toBe('Hello') |
86 | | - expect(result.errors.notFound).toBe('Not found') |
| 60 | + expect(result.alerts.badEncoding).toEqual({ |
| 61 | + description: |
| 62 | + 'Source files are encoded using a non-standard text encoding.', |
| 63 | + emoji: '⚠️', |
| 64 | + suggestion: |
| 65 | + 'Ensure all published files are encoded using a standard encoding such as UTF8, UTF16, UTF32, SHIFT-JIS, etc.', |
| 66 | + title: 'Bad text encoding', |
| 67 | + }) |
87 | 68 | }) |
88 | 69 |
|
89 | | - it('uses createRequire with import.meta.url', async () => { |
90 | | - // Re-import to get fresh module with reset cache. |
91 | | - const { getTranslations: getTranslationsFresh } = await import( |
92 | | - './translations.mts' |
93 | | - ) |
| 70 | + it('has correct structure for alert entries', async () => { |
| 71 | + const { getTranslations } = await import('./translations.mts') |
94 | 72 |
|
95 | | - getTranslationsFresh() |
| 73 | + const result = getTranslations() |
| 74 | + const { badSemver } = result.alerts |
96 | 75 |
|
97 | | - expect(createRequire).toHaveBeenCalledWith( |
98 | | - expect.stringContaining('.mts'), |
99 | | - ) |
| 76 | + expect(badSemver).toHaveProperty('description') |
| 77 | + expect(badSemver).toHaveProperty('emoji') |
| 78 | + expect(badSemver).toHaveProperty('suggestion') |
| 79 | + expect(badSemver).toHaveProperty('title') |
| 80 | + expect(typeof badSemver.description).toBe('string') |
| 81 | + expect(typeof badSemver.title).toBe('string') |
100 | 82 | }) |
101 | 83 |
|
102 | | - it('handles empty translations object', async () => { |
103 | | - mockTranslations = {} |
104 | | - mockRequire = vi.fn(() => mockTranslations) |
105 | | - vi.mocked(createRequire).mockReturnValue(mockRequire) |
106 | | - |
107 | | - // Re-import to get fresh module with reset cache. |
108 | | - const { getTranslations: getTranslationsFresh } = await import( |
109 | | - './translations.mts' |
110 | | - ) |
| 84 | + it('returns alerts object with multiple entries', async () => { |
| 85 | + const { getTranslations } = await import('./translations.mts') |
111 | 86 |
|
112 | | - const result = getTranslationsFresh() |
| 87 | + const result = getTranslations() |
113 | 88 |
|
114 | | - expect(result).toEqual({}) |
| 89 | + expect(Object.keys(result.alerts).length).toBeGreaterThanOrEqual(2) |
| 90 | + expect(result.alerts.badEncoding).toBeDefined() |
| 91 | + expect(result.alerts.badSemver).toBeDefined() |
115 | 92 | }) |
116 | 93 |
|
117 | | - it('handles complex nested translations', async () => { |
118 | | - mockTranslations = { |
119 | | - level1: { |
120 | | - level2: { |
121 | | - level3: { |
122 | | - message: 'Deeply nested message', |
123 | | - }, |
124 | | - }, |
125 | | - }, |
126 | | - arrays: ['item1', 'item2'], |
127 | | - } |
128 | | - mockRequire = vi.fn(() => mockTranslations) |
129 | | - vi.mocked(createRequire).mockReturnValue(mockRequire) |
130 | | - |
131 | | - // Re-import to get fresh module with reset cache. |
132 | | - const { getTranslations: getTranslationsFresh } = await import( |
133 | | - './translations.mts' |
134 | | - ) |
135 | | - |
136 | | - const result = getTranslationsFresh() |
137 | | - |
138 | | - expect(result.level1.level2.level3.message).toBe('Deeply nested message') |
139 | | - expect(result.arrays).toEqual(['item1', 'item2']) |
| 94 | + it('handles nested alert structure', async () => { |
| 95 | + const { getTranslations } = await import('./translations.mts') |
| 96 | + |
| 97 | + const result = getTranslations() |
| 98 | + |
| 99 | + // Verify we can access nested properties. |
| 100 | + expect(result.alerts.badEncoding.title).toBe('Bad text encoding') |
| 101 | + expect(result.alerts.badSemver.title).toBe('Bad semver') |
140 | 102 | }) |
141 | 103 | }) |
142 | 104 | }) |
0 commit comments