Skip to content

Commit

Permalink
chore: add test for lang keys (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImLunaHey authored Dec 20, 2024
1 parent c75537f commit ab62cd2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ jobs:
cache: 'npm'
- run: npm ci
- run: npm run lint
- run: CHECK_LANGUAGES=true npm run test lang
continue-on-error: true
- run: npm run test
- run: npm run build --if-present
53 changes: 53 additions & 0 deletions src/i18n/lang.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { test } from 'vitest';
import { languages } from './index';

const typedObjectKeys = <T>(obj: T) => Object.keys(obj as object) as (keyof T)[];

for (const language of typedObjectKeys(languages).filter((lang) => lang !== 'en')) {
test.skipIf(process.env.CHECK_LANGUAGES !== 'true')(`${language} has the correct structure`, () => {
const enShape = JSON.parse(
JSON.stringify(languages.en, (_, value) => (typeof value === 'object' ? value : null)),
) as Partial<typeof languages.en>;

// Store all errors
const errors: string[] = [];

// Check each language
try {
const langShape = JSON.parse(
JSON.stringify(languages[language], (_, value) => (typeof value === 'object' ? value : null)),
) as Partial<typeof languages.en>;

// Find missing keys
const missingKeys = findMissingKeys(enShape, langShape, []);
if (missingKeys.length > 0) {
errors.push(`${language} is missing keys: ${missingKeys.join(', ')}`);
}
} catch (error: unknown) {
if (error instanceof Error) errors.push(`Error processing ${language}: ${error.message}`);
}

// If there are any errors, fail the test with all error messages
if (errors.length > 0) {
throw new Error('\n' + errors.join('\n'));

Check failure on line 32 in src/i18n/lang.test.ts

View workflow job for this annotation

GitHub Actions / build (22.x)

src/i18n/lang.test.ts > nl has the correct structure

Error: nl is missing keys: handleSearch, debug ❯ src/i18n/lang.test.ts:32:13
}
});
}

// Helper function to recursively find missing keys
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function findMissingKeys(reference: Record<string, any>, target: Record<string, any>, path: string[] = []): string[] {
const missingKeys: string[] = [];

for (const [key, value] of Object.entries(reference)) {
const currentPath = [...path, key];

if (!(key in target)) {
missingKeys.push(currentPath.join('.'));
} else if (typeof value === 'object' && value !== null) {
missingKeys.push(...findMissingKeys(value, target[key], currentPath));
}
}

return missingKeys;
}
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export default defineConfig({
test: {
environment: 'happy-dom',
setupFiles: ['./src/test-setup.ts'],
reporters: process.env.GITHUB_ACTIONS ? ['verbose', 'github-actions'] : ['verbose', 'html'],
},
});

0 comments on commit ab62cd2

Please sign in to comment.