Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: translations (DEV-4071) #1767

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
657170e
test: implement test of translations
domsteinbach Sep 3, 2024
9f66fa1
test: include ts files
domsteinbach Sep 9, 2024
b6b648a
refactor: remove unused translations and implement translations
domsteinbach Sep 9, 2024
39a7061
Merge branch 'main' into feature/dev-4071-write-test-for-translations
domsteinbach Sep 9, 2024
ec8652b
chore: rename
domsteinbach Sep 10, 2024
a75a0cf
refactor: implement translation
domsteinbach Sep 10, 2024
d5c2d0d
refactor: remove unused translations
domsteinbach Sep 10, 2024
a19f35a
Merge branch 'main' into feature/dev-4071-write-test-for-translations
domsteinbach Sep 10, 2024
70f73dd
Merge branch 'main' into feature/dev-4071-write-test-for-translations
domsteinbach Oct 1, 2024
e0a2efa
Merge branch 'main' into feature/dev-4071-write-test-for-translations
domsteinbach Oct 21, 2024
4d17d1a
Merge branch 'main' into feature/dev-4071-write-test-for-translations
domsteinbach Oct 22, 2024
649eaae
Merge branch 'main' into feature/dev-4071-write-test-for-translations
domsteinbach Oct 22, 2024
77d6fa0
test: refactor test to use results from ngx-translate-extract
domsteinbach Oct 23, 2024
d52b4e8
feat: implement ngx-translate-extract
domsteinbach Oct 23, 2024
e044373
test: fails to parse
domsteinbach Oct 23, 2024
e77d233
fix: removed trailing commas in case to prevent ngx-translate-extract…
irmastnt Oct 23, 2024
df531b4
fix: add missing translation keys, remove unused translation keys
domsteinbach Oct 28, 2024
a9b5bf5
test: add test for empty strings in en
domsteinbach Oct 28, 2024
c501289
fix: reintroduce missing translations
domsteinbach Oct 28, 2024
1ebd46c
test: get all translations
domsteinbach Oct 28, 2024
193f570
chore: rm
domsteinbach Oct 28, 2024
93d69c9
chore: ignore temp translations file
domsteinbach Oct 28, 2024
017673c
chore: comments
domsteinbach Oct 28, 2024
2e2f39b
refactor: only run translation tests locally
domsteinbach Oct 28, 2024
563e9a3
refactor: remove extract from cloud
domsteinbach Oct 28, 2024
7dbe1cd
Merge branch 'main' into feature/dev-4071-write-test-for-translations
domsteinbach Nov 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ apps/dsp-app/cypress/screenshots

# Sentry Config File
.sentryclirc
/apps/dsp-app/cypress/fixtures/temp/translations-in-use.json
43 changes: 26 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,24 @@ The most common commands are defined in `package.json`.

> **_NOTE:_** You can install `nx` globally with `npm install -g nx`. If not, then all `nx` commands below need to be prefixed with `npx`.

| nx | npm |
| ---------------------------------------------------- | ------------------------- |
| `nx run dsp-app:test` | `npm run test-local` |
| `nx run dsp-app:test:ci` | `npm run test-ci` |
| `nx run-many --all --target=test --configuration=ci` | `npm run test-ci-all` |
| `nx run dsp-app:serve` | `npm run start-local` |
| `nx run dsp-app:serve:test-server` | `npm run start-test` |
| `nx run dsp-app:serve:dev-server` | `npm run start-dev` |
| `nx run dsp-app:serve:ls-test-server` | `npm run start-ls-test` |
| `nx run dsp-app:serve:stage-server` | `npm run start-stage` |
| `nx run dsp-app:serve:0845-test-server` | `npm run start-0845-test` |
| `nx run dsp-app:lint` | `npm run lint-ci` |
| `nx run dsp-app:lint --fix` | `npm run lint-local` |
| `nx run dsp-app-e2e:e2e:development` | `npm run e2e-ci-dev` |
| `nx run dsp-app-e2e:e2e:production` | `npm run e2e-ci` |
| `nx run dsp-app:build` | `build` |
| `nx run dsp-app:build:production` | `build-prod` |
| nx | npm |
|------------------------------------------------------|-------------------------------|
| `nx run dsp-app:test` | `npm run test-local` |
| `nx run dsp-app:test:ci` | `npm run test-ci` |
| `nx run-many --all --target=test --configuration=ci` | `npm run test-ci-all` |
| `nx run dsp-app:serve` | `npm run start-local` |
| `nx run dsp-app:serve:test-server` | `npm run start-test` |
| `nx run dsp-app:serve:dev-server` | `npm run start-dev` |
| `nx run dsp-app:serve:ls-test-server` | `npm run start-ls-test` |
| `nx run dsp-app:serve:stage-server` | `npm run start-stage` |
| `nx run dsp-app:serve:0845-test-server` | `npm run start-0845-test` |
| `nx run dsp-app:lint` | `npm run lint-ci` |
| `nx run dsp-app:lint --fix` | `npm run lint-local` |
| `nx run dsp-app-e2e:e2e:development` | `npm run e2e-ci-dev` |
| `nx run dsp-app-e2e:e2e:production` | `npm run e2e-ci` |
| `nx run dsp-app:build` | `build` |
| `nx run dsp-app:build:production` | `build-prod` |


| npx | npm |
| ----------------------------------------- | ------------------- |
Expand All @@ -92,6 +93,14 @@ There are three NPM scripts to run the E2E tests:
- `npm run e2e-ci` will run the E2E tests in the console in a production environment. This is the command run also on GitHub CI.
- `npm run e2e-local` will open the Cypress UI which will enable to run individual tests easily and see every step as it runs.

If there are tests for translations failing, you can merge the translations with the following command:

| npx | npm |
| ----------------------------------------- | ------------------- |
| `nx run dsp-app:i18n:merge-translations` | `i18n:merge-translations` |

This will extract all translation keys from the source code and add missing translation keys to all translation files. The values of added keys will be empty strings.

## Further Documentation

### User guide
Expand Down
8 changes: 8 additions & 0 deletions apps/dsp-app/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { defineConfig } from 'cypress';
import { checkUnusedENTranslations, compareTranslationFileWithEn } from './cypress/plugins/translations';

export default defineConfig({
projectId: 'n5b5id',
Expand All @@ -14,5 +15,12 @@ export default defineConfig({
env: {
apiUrl: 'http://0.0.0.0:3333',
},
setupNodeEvents(on, config) {
on('task', {
checkUnusedENTranslations,
compareTranslationFileWithEn,
});
return config;
},
},
});
84 changes: 84 additions & 0 deletions apps/dsp-app/cypress/e2e/translation/translation.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { TranslationKeyComp, TranslationUsage } from '../../plugins/translations';

(Cypress.env('INCLUDE_TRANSLATIONS') ? describe : describe.skip)('Translations', () => {
before(() => {
Cypress.env('skipDatabaseCleanup', true);
});

it('should have some translations, but no unused translations', () => {
cy.task<TranslationUsage>('checkUnusedENTranslations').then(translationKeys => {
expect(translationKeys.usedKeys.length).to.be.greaterThan(
0,
`There should be translation keys in use (${translationKeys.usedKeys.length} satisfies this condition)`
);
expect(translationKeys.unusedKeys.length).to.eq(
0,
`There should be no unused translation keys. Found ${translationKeys.unusedKeys.length} unused.\n\nUnused translation keys:\n\n${translationKeys.unusedKeys.join(',\n')}`
);
});
});

it('should have all used translation keys defined in en.json', () => {
cy.task<TranslationUsage>('checkUnusedENTranslations').then(translationKeys => {
// Load the en.json file
cy.readFile('src/assets/i18n/en.json').then(enTranslations => {
const isKeyInObject = (obj: any, key: string): boolean => {
return key.split('.').reduce((o, k) => (o && o.hasOwnProperty(k) ? o[k] : undefined), obj) !== undefined;
};

const missingInEn = translationKeys.usedKeys.filter(key => !isKeyInObject(enTranslations, key));

expect(missingInEn.length).to.eq(
0,
`There are missing translations in en.json for the following keys:\n\n${missingInEn.join(',\n')}`
);
});
});
});

it('should have the same translation keys in all translation files', () => {
const translationFiles = ['de.json', 'fr.json', 'it.json', 'rm.json'];
translationFiles.forEach(file => {
cy.task('compareTranslationFileWithEn', file).then((keys: TranslationKeyComp) => {
expect(keys.missingKeys.length).to.eq(
0,
`Translation file ${file} is missing keys:\n ${keys.missingKeys.join(',\n')}`
);
expect(keys.additionalKeys.length).to.eq(
0,
`Translation file ${file} has additional keys, which are not part of the default lang @en:\n ${keys.additionalKeys.join(',\n')}`
);
});
});
});

it('should have no empty translation strings in en.json', () => {
cy.readFile('src/assets/i18n/en.json').then(enTranslations => {
const findEmptyStrings = (obj: any, prefix = ''): string[] => {
const emptyKeys: string[] = [];
Object.keys(obj).forEach(key => {
const value = obj[key];
const fullKey = prefix ? `${prefix}.${key}` : key;

if (typeof value === 'object' && value !== null) {
emptyKeys.push(...findEmptyStrings(value, fullKey));
} else if (typeof value === 'string' && value.trim() === '') {
emptyKeys.push(fullKey);
}
});
return emptyKeys;
};

const emptyKeys = findEmptyStrings(enTranslations);

expect(emptyKeys.length).to.eq(
0,
`There are empty translation strings in en.json for the following keys:\n\n${emptyKeys.join(',\n')}`
);
});
});

after(() => {
Cypress.env('skipDatabaseCleanup', false);
});
});
10 changes: 10 additions & 0 deletions apps/dsp-app/cypress/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { checkUnusedENTranslations, compareTranslationFileWithEn } from './translations';

const pluginConfig = (on: Cypress.PluginEvents) => {
on('task', {
checkUnusedENTranslations,
compareTranslationFileWithEn,
});
};

export default pluginConfig;
58 changes: 58 additions & 0 deletions apps/dsp-app/cypress/plugins/translations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import fs = require('fs');
import path = require('path');

export interface TranslationUsage {
usedKeys: string[];
unusedKeys: string[];
}

export interface TranslationKeyComp {
missingKeys: string[];
additionalKeys: string[];
}

const extractKeys = (obj: any, prefix = ''): Record<string, boolean> =>
Object.keys(obj).reduce((res, key) => {
const pre = prefix ? `${prefix}.` : '';
if (typeof obj[key] === 'object') {
Object.assign(res, extractKeys(obj[key], pre + key));
} else {
res[pre + key] = true;
}
return res;
}, {});

export const checkUnusedENTranslations = (): TranslationUsage => {
const translationsInUsePath = path.join(__dirname, '../../cypress/fixtures/temp/translations-in-use.json');
const translationKeysInUse = JSON.parse(fs.readFileSync(translationsInUsePath, 'utf8'));

const fPath = path.join(__dirname, '../../src/assets/i18n/en.json');
const translationKeys = JSON.parse(fs.readFileSync(fPath, 'utf8'));
const translationKeysSet = extractKeys(translationKeys);

const usedKeys = new Set(Object.keys(translationKeysInUse));

const allKeys = Object.keys(translationKeysSet);
const unusedKeys = allKeys.filter(key => !usedKeys.has(key));

return {
usedKeys: Array.from(usedKeys),
unusedKeys,
};
};

export const compareTranslationFileWithEn = (file: string): TranslationKeyComp => {
const translationsInUsePath = path.join(__dirname, '../../cypress/fixtures/temp/translations-in-use.json');
const translationKeysInUse = JSON.parse(fs.readFileSync(translationsInUsePath, 'utf8'));

const fPath = path.join(__dirname, '../../src/assets/i18n/', file);
const translationKeys = JSON.parse(fs.readFileSync(fPath, 'utf8'));

const flatTranslationKeys = extractKeys(translationKeys);
const usedKeys = Object.keys(translationKeysInUse);

const missingKeys = usedKeys.filter(key => !Object.keys(flatTranslationKeys).includes(key));
const additionalKeys = Object.keys(flatTranslationKeys).filter(key => !usedKeys.includes(key));

return { missingKeys, additionalKeys };
};
4 changes: 4 additions & 0 deletions apps/dsp-app/cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Cypress.on('uncaught:exception', err => {
// do things here before each test if needed
// All active session data (cookies, localStorage and sessionStorage) across all domains are cleared.
beforeEach(() => {
if (Cypress.env('skipDatabaseCleanup')) {
return; // Skip cleanup
}

let users: UserProfiles;

// clear database
Expand Down
Loading
Loading