Overview
Tests in Ship applications are optional by default. The template excludes testing dependencies to keep it lightweight and focused on rapid development. However, as your project grows in complexity or requires higher reliability, testing becomes essential. Testing is implemented using the Jest framework and MongoDB memory server. The setup supports execution in CI/CD pipelines. MongoDB memory server allows connecting to an in-memory MongoDB instance and running integration tests in isolation, ensuring reliable and fast test execution without affecting your production database.When to Add Testing
- Complex and confusing business rules
- Critical calculations/algorithms
- Core flows that affect other features
- Logic reused across the applications
- Areas with recurring hard bugs
When to Skip Testing
- Simple CRUD operations
- Prototype/MVP development
- Short-term projects
- Basic UI components
- Static content pages
Installation and Setup
Installing Dependencies
Add the necessary testing packages to your project:Jest Configuration
Navigate to the root ofapps/api
.
Create configuration file for jest:
jest.config.js
jest-mongodb-config.js
.env.test
file in the root of apps/api
and set the following variables:
.env.test
package.json scripts
Add test scripts to yourapps/api/package.json
:
apps/api/package.json
Test Structure
Tests should be placed next to the code they are testing inside atests/
folder. Use *.spec.ts
suffixes and standardize by unit type:
.action.spec.ts
— for action handler + validator (HTTP via supertest).service.spec.ts
— for data/service layer.validator.spec.ts
— for standalone schema/validators
API resource example
Utilities
Colocate tests for utility modules. Keep them small and pure (no DB). Createtests/
folder inside utils
Testing Examples
Service Integration Test Example
user.service.spec.ts
API Action Test Example
Test your API endpoints:sign-up.action.spec.ts
Testing Utilities
security.util.spec.ts
Best Practices
Test Isolation
Each test should be isolated from the others. UsebeforeEach
to clean the database before each test.
Test Naming
Use descriptive test names that explain the expected behavior:Mock External Services
Mock external API calls and services:title: “Testing”
Overview
In Ship testing settled through Jest framework and MongoDB memory server with possibility running them in CI/CD pipeline. MongoDB’s memory server allows connecting to the MongoDB server and running integration tests isolated. Tests should be placed in thetests
directory specified for each resource from the resources
folder and have next naming format user.service.spec.ts
.
apps/api/src/resources/user/tests/user.service.spec.ts
Example
GitHub Actions
By default, tests run for each pull request to themain
branch through the run-tests.yml
workflow.
.github/workflows/run-tests.yml
To set up pull request rejection if tests failed visit
Settings > Branches
tab in your repository. Then add the branch protection rule “Require status checks to pass before merging”.