| 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 are placed in
__tests__ folders and have .test.ts extension.
- Test folder is placed in the same directory as an actual implementation.
- When testing types, suffix should be
.type-test.ts, to prevent from being executed by jest. (For example: packages/utils/tests/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/
├── __tests__/
│ └── utils.test.ts
└── utils.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).
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