Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:The Ever Traduora project employs a comprehensive testing strategy that covers both the backend API and the frontend web application. This strategy relies on distinct testing frameworks and methodologies for each part of the stack to ensure functionality, reliability, and correctness. The API is tested using Jest for both unit and end-to-end (E2E) tests, while the web application uses Protractor for its E2E tests.
This document outlines the tools, configurations, and processes involved in the project's testing strategy, focusing on how tests are structured, executed, and maintained.
API Testing
The API testing strategy is centered around the Jest testing framework. It includes both isolated unit tests for individual components and integrated end-to-end tests that validate complete API flows.
Sources: api/package.json
Test Execution Scripts
The api/package.json file defines several scripts for running different types of tests.
| Script | Description |
|---|---|
test | Runs unit tests using Jest. It is configured to run in a test environment with a 3GB memory limit. |
test:watch | Runs unit tests in watch mode, which is useful during development for immediate feedback. |
test:e2e | Executes end-to-end tests. It sets specific environment variables (TR_DEFAULT_PROJECT_PLAN, TR_DB_DATABASE) and uses a dedicated Jest configuration (./test/jest-e2e.json). Tests are run sequentially (--runInBand). |
test:ci | A script intended for Continuous Integration, which runs both unit tests (yarn test) and E2E tests (yarn test:e2e). |
Sources: api/package.json:23-27
Unit Testing
Unit tests focus on verifying the functionality of individual modules or functions in isolation. These tests are typically located alongside the source code in .spec.ts files. For example, formatters like the PHP parser have dedicated unit tests to ensure they correctly parse and export data.
The following is an example of a unit test for the PHP formatter.
// api/src/formatters/php.spec.ts:5
test('should parse php files', async () => {
const input = `[
"term two" => "hello there, all good?",
"term.three.is..very.nested" => "Export format...",
"term" => [
"one" => "Current Plan: {{ project.plan.name }}",
"three" => [
"is" => [
"" => [
"very" => [
"nested too" => "Another export format..."
]
]
]
]
]
]`;
const result = await phpParser(input);
expect(result).toEqual({
translations: [
// ... assertions
],
});
});
Sources: api/src/formatters/php.spec.ts:5-29
End-to-End (E2E) Testing
E2E tests are crucial for verifying the integration of different API components and validating user-facing workflows. The project uses supertest to make HTTP requests to the API endpoints and assert the responses.
Test Environment Setup
Each E2E test suite runs in a clean, isolated environment. A utility function, createAndMigrateApp, is responsible for this setup.
The diagram below illustrates the test environment setup flow.
This diagram shows the steps taken by the createAndMigrateApp helper to prepare a fresh application instance and database for each test run.
Sources: api/test/util.ts:94-117
The beforeEach and afterEach hooks are used to manage the lifecycle of the application and database connection for each test file.
beforeEach: CallscreateAndMigrateApp()to get a fresh application instance. It also signs up test users and creates test projects needed for the tests.afterEach: Closes the database connection and the application instance to clean up resources.
Sources: api/test/export.e2e-spec.ts:15-18, api/test/export.e2e-spec.ts:275-278
Test Helpers
The api/test/util.ts file provides several helper functions to streamline the creation of test data and entities.
signupTestUser: Signs up a new user via the/api/v1/auth/signupendpoint and returns the user's details, including the access token.createTestProject: Creates a new project for a given user via the/api/v1/projectsendpoint.createAndMigrateApp: As described above, prepares the test application and database.
These helpers reduce boilerplate code and make tests more readable and maintainable.
Sources: api/test/util.ts
E2E Test Example: Exporting Translations
The following sequence diagram illustrates the flow of an E2E test that verifies the translation export functionality.
This diagram shows an E2E test making a request to the export endpoint and validating the response.
Sources: api/test/export.e2e-spec.ts:122-131
The tests cover various scenarios, including:
- Correct data formatting for different export types (
jsonflat,properties). - Correct lexical ordering of terms.
- Handling of UTF-8 characters.
- Validation of request parameters (e.g., missing
localeorformat). - Authentication and authorization checks.
Sources: api/test/export.e2e-spec.ts, api/test/import.e2e-spec.ts, api/test/project-user.e2e-spec.ts
Webapp Testing
The frontend web application uses Protractor, an end-to-end test framework for Angular applications.
Sources: webapp/package.json
Test Execution Scripts
The webapp/package.json file contains the script to run E2E tests.
| Script | Description |
|---|---|
e2e | Executes E2E tests for the Angular application using ng e2e. |
Sources: webapp/package.json:25
E2E Testing
The webapp E2E tests are designed to simulate user interactions in a real browser and verify that the application behaves as expected.
A simple test case is defined in webapp/e2e/src/app.e2e-spec.ts, which navigates to the application's root page and checks for a welcome message. This test utilizes the Page Object Model (POM) pattern, where AppPage encapsulates the logic for interacting with the page.
// webapp/e2e/src/app.e2e-spec.ts
import { AppPage } from './app.po';
describe('workspace-project App', () => {
let page: AppPage;
beforeEach(() => {
page = new AppPage();
});
it('should display welcome message', () => {
page.navigateTo();
expect(page.getParagraphText()).toEqual('Welcome to auth-web!');
});
});
Sources: webapp/e2e/src/app.e2e-spec.ts
Conclusion
The testing strategy for Ever Traduora is robust, with clear separation between backend and frontend testing. The API benefits from a combination of Jest unit tests and comprehensive E2E tests that ensure endpoint integrity and business logic correctness against a real database. The webapp relies on Protractor for its E2E tests to validate user flows in a browser environment. This dual approach provides strong quality assurance across the entire application stack.