Ionic Cordova指定配置构建时,如何通过ng test验证环境变量是否正确调用?
Absolutely, you can validate that the correct environment variables are being used when running your ionic cordova build android --configuration a command with ng test—you just need to adjust your test setup and write targeted test cases. Here's how to do it step by step:
1. Mirror Build Configurations in Your Test Setup
First, you need to extend your angular.json test configuration to recognize your environment-specific setups, just like you did for the build process.
Open angular.json, find the projects.app.architect.test section, and add a configurations object (if it doesn’t exist) with entries for a, b, and c:
"projects": { "app": { ... "architect": { ... "test": { ... "configurations": { "a": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.a.ts" } ] }, "b": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.b.ts" } ] }, "c": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.c.ts" } ] } } } } } }
2. Write a Test Suite to Verify Environment Variables
Create a test file (e.g., src/environments/environment.spec.ts) to check that the merged variables from environment.common.ts and your target environment file are loaded correctly. This works because your environment files should already import and extend the common config.
import { environment } from './environment'; describe('Environment Configuration', () => { it('should load merged variables from common and environment.a.ts', () => { // Replace these with your actual expected values expect(environment.production).toBe(false); // Example common variable expect(environment.apiBaseUrl).toBe('https://api.a.example.com'); // Example 'a'-specific variable expect(environment.featureFlags.enableAdvancedTools).toBe(true); // Another 'a'-specific flag }); // Duplicate this test block for 'b' and 'c' if needed, // or adjust expectations when running tests for different configurations });
3. Run Tests with the Target Configuration
Now you can run the test suite using the same configuration flag you use for builds to confirm the correct environment is loaded:
ng test --configuration a
This command uses the fileReplacements you set up for configuration a, loads environment.a.ts (and its merged environment.common.ts values), and runs your tests to verify all variables match your expectations.
Key Notes
- Ensure each environment file (like
environment.a.ts) correctly imports and extends the common config (e.g.,export const environment = { ...environmentCommon, /* your overrides */ };). This guarantees the merged variables match what your app uses during builds. - For testing multiple configurations, you can either run separate commands (like
ng test --configuration b) or create conditional test logic, but running explicit per-configuration commands is simpler and more reliable.
内容的提问来源于stack exchange,提问作者Iraklis Bekiaris

