| name | tests |
| description | Test style guidelines including naming conventions, mock organization, translation handling, and file structure. Use when writing or organizing tests in the Trezor Suite codebase. |
Tests
Reading
Translations in tests
Text in the app may change as translators and copywriters update strings in Crowdin, independently of developers. To
avoid failing tests in Crowdin sync PRs, get the string by its translation ID instead of using the literal text.
expect(
screen.getByText('This can change with a Crowdin sync and someone will have to fix the test.'),
).toBeTruthy();
expect(screen.getByText(getTranslation('path.to.translation'))).toBeTruthy();
expect(screen.getByText(getTranslation('path.to.translation'))).toBe(
'I want a developer to check this important text if it is changed in Crowdin.',
);
Naming conventions
- Tests MUST be placed directly next to the file they are testing (co-located tests).
tests and __tests__ directories are NOT allowed for test files.
- The goal is fast navigation and clear coverage visibility: when you open a source file,
you should immediately see whether it has a nearby test.
- Use
.test.ts / .test.tsx suffix for test files.
- When testing types, suffix should be
.type-test.ts, to prevent from being executed by jest.
(For example: typedObjectFromEntries.type-test.ts)
- Fixtures are placed in
mocks folders and have mock prefix.
mocks folder is placed in the root of the package, not in src.
Example:
my-module/
├── mocks/
│ └── mockMyComponent.ts
└── src/
├── MyComponent.tsx
├── MyComponent.test.tsx
├── useMyData.ts
├── useMyData.test.ts
├── utils.ts
└── utils.test.ts
Reusability
To keep things simple, avoid creating complex mocks to be shared between multiple test suites. In case you do reuse a
mock, keep it generic and non-opinionated.
Simple test: change in shared mock SHALL NOT break existing tests (or make fixes trivial).
Type tests
Keep type-test assertions module-local. Do not export test-only values or types, because exports
pollute the generated declaration files. Use void statements to mark assertion values as used:
const valid: ExpectedType = value;
const invalid: ExpectedType = incompatibleValue;
void valid;
void invalid;
Mocks (& Fixtures)
Typing
All fixtures and mocks shall be typed and declaratively defined; using as to cast an incomplete object is only a last
resort. This may add boilerplate, but it ensures type changes surface as type errors instead of hard-to-fix failing
tests.
Organization & Naming Convention