Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
- packages/core/package.json
- packages/shop-web-angular/config/karma.conf.js
- packages/shop-web-angular/package.json
- packages/core/src/test/integration/GeoLocations.spec.ts
-- packages/admin-web-angular/angular.json - packages/shop-web-angular/config/spec-bundle.js
- packages/admin-web-angular/src/test.ts
- packages/shop-mobile-expo/README.md
- package.json
The Ever Demand platform employs a multi-layered testing strategy tailored to its monorepo architecture, which includes a Node.js/NestJS backend, several Angular-based web applications, and mobile applications. The strategy leverages different frameworks and tools appropriate for each part of the stack, including Jest for backend unit and integration tests, and a combination of Karma, Jasmine, and Protractor for frontend unit and end-to-end (E2E) testing.
This approach ensures that individual packages and the platform as a whole are robust and reliable. Testing is integrated into the development workflow with scripts for running tests locally and in Continuous Integration (CI) environments. The configuration is managed within each package, allowing for specialized setups while maintaining a cohesive testing process across the entire project.
Testing Frameworks and Tools
The project utilizes a variety of testing frameworks and tools, each chosen for its suitability to a specific part of the application stack.
| Package/Component Type | Testing Type | Framework/Tool | Description |
|---|---|---|---|
@ever-platform/core (Backend) | Unit & Integration | Jest | A JavaScript testing framework used for testing the NestJS backend logic. |
Angular Apps (e.g., admin-web-angular, shop-web-angular) | Unit | Karma, Jasmine | Karma is the test runner, and Jasmine is the testing framework for Angular components and services. |
Angular Apps (e.g., admin-web-angular, shop-web-angular) | End-to-End (E2E) | Protractor | An E2E test framework for Angular applications. |
Mobile Apps (e.g., shop-mobile-expo) | Unit | Jest | Used for testing the React Native (Expo) mobile application. |
Sources: packages/core/package.json, packages/shop-web-angular/config/karma.conf.js, packages/admin-web-angular/angular.json, packages/shop-mobile-expo/README.md
Backend Testing (@ever-platform/core)
The backend, built with NestJS, is tested using the Jest framework. The configuration is defined within the jest property of packages/core/package.json.
Jest Configuration
The Jest setup is configured to find and run test files ending in .spec.ts within the src directory. It uses ts-jest to transform TypeScript files and generates coverage reports in the coverage/packages/core directory.
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage/packages/core",
"testEnvironment": "node"
}
Sources: packages/core/package.json:203-222
Integration Testing
Integration tests are written to verify the interaction between different services and the database. The GeoLocations.spec.ts file provides an example of this approach.
The test setup involves:
- Connecting to a test-specific MongoDB database before all tests run.
- Using
fakerto generate mock data for entities likeGeoLocationandWarehouse. - Dropping all database collections after each test to ensure isolation.
- Disconnecting from the database after all tests are complete.
This diagram illustrates the flow of an integration test case.
Frontend Testing (Angular)
Angular applications within the monorepo, such as admin-web-angular and shop-web-angular, use Karma for unit testing and Protractor for E2E testing.
Unit Testing with Karma and Jasmine
Unit tests for Angular components and services are run using the Karma test runner, with Jasmine as the assertion framework.
Karma Configuration
The karma.conf.js file defines the testing environment. Key configurations include:
- Frameworks:
jasmineis set as the testing framework. - Test Entry Point:
config/spec-bundle.jsis the main file that loads all test files. - Preprocessors: Webpack is used to bundle the test files, and
sourcemapandcoveragepreprocessors are enabled. - Reporters: Test results are reported to the console using
mocha, and coverage reports are generated bycoverageandremap-coverage. - Browsers: Tests are configured to run in a headless Chrome environment, especially for CI, using a custom launcher
ChromeTravisCi.
// packages/shop-web-angular/config/karma.conf.js
const configuration = {
frameworks: ['jasmine'],
files: [
{ pattern: './config/spec-bundle.js', watched: false },
// ...
],
preprocessors: {
'./config/spec-bundle.js': ['coverage', 'webpack', 'sourcemap'],
},
webpack: testWebpackConfig,
reporters: ['mocha', 'coverage', 'remap-coverage'],
browsers: ['ChromeTravisCi'],
customLaunchers: {
ChromeTravisCi: {
base: 'ChromeHeadless',
flags: ['--no-sandbox', '--disable-gpu'],
},
},
singleRun: false,
// ...
};
Sources: packages/shop-web-angular/config/karma.conf.js:15-104
Test Environment Setup
The Angular test environment is initialized in two key files:
packages/admin-web-angular/src/test.ts: This file imports necessary Zone.js patches for asynchronous testing and initializes the AngularTestBedusingplatformBrowserDynamicTesting.packages/shop-web-angular/config/spec-bundle.js: This file uses Webpack'srequire.contextto dynamically discover and load all files ending with.spec.tsfrom thesrcdirectory, ensuring they are included in the test bundle.
The following diagram shows the process of running Angular unit tests.
End-to-End (E2E) Testing with Protractor
E2E tests are configured to run using Protractor, which simulates user interactions in a real browser. The configuration is specified in angular.json for the admin-web-angular project.
The e2e architect target points to a protractor.conf.js file and specifies the development server target (ever-admin:serve) that the tests will run against.
// packages/admin-web-angular/angular.json
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "./protractor.conf.js",
"devServerTarget": "ever-admin:serve"
}
}
Sources: packages/admin-web-angular/angular.json:162-169
Scripts to run these tests are available in package.json files, such as e2e and protractor in packages/shop-web-angular/package.json.
Sources: packages/shop-web-angular/package.json:48-58
Mobile App Testing
The testing strategy extends to mobile applications as well. For the React Native application (shop-mobile-expo), tests are run using Jest.
# packages/shop-mobile-expo/README.md
## 🧪 Testing
We're using Jest to test our application.
To run all tests, run `yarn run test`
Sources: packages/shop-mobile-expo/README.md:58-62
CI/CD Integration
The testing strategy is designed for integration with CI/CD pipelines.
- The root
package.jsonand individual packagepackage.jsonfiles contain scripts liketest:cithat execute tests in a non-interactive, single-run mode. - The Karma configuration includes a
ChromeTravisCilauncher, which runs tests in a headless, sandboxed Chrome browser suitable for CI environments like Travis CI. - Coverage reports are generated, which can be uploaded to services like Coveralls, as indicated by the
coveragescript inpackages/core/package.json.
Sources: packages/shop-web-angular/config/karma.conf.js:95-101, packages/shop-web-angular/package.json:71, packages/core/package.json:20