|
| 1 | +# Package.json Configuration for Angular Unit Tests |
| 2 | + |
| 3 | +## Overview |
| 4 | +This document explains the package.json configuration needed in the main `eform-angular-frontend` repository to support running unit tests for the time-planning-pn plugin. |
| 5 | + |
| 6 | +## Required Test Scripts |
| 7 | + |
| 8 | +The GitHub Actions workflows will try to run tests using one of these approaches (in order): |
| 9 | + |
| 10 | +### Option 1: test:ci script (Recommended) |
| 11 | +If your package.json has a `test:ci` script, it will use that: |
| 12 | + |
| 13 | +```json |
| 14 | +{ |
| 15 | + "scripts": { |
| 16 | + "test:ci": "ng test --watch=false --code-coverage --browsers=ChromeHeadless" |
| 17 | + } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +### Option 2: test script with parameters |
| 22 | +If `test:ci` is not available, it will try to use the `test` script with additional parameters: |
| 23 | + |
| 24 | +```json |
| 25 | +{ |
| 26 | + "scripts": { |
| 27 | + "test": "ng test" |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +The workflow will add `--watch=false --browsers=ChromeHeadless` automatically. |
| 33 | + |
| 34 | +## Common Issues and Solutions |
| 35 | + |
| 36 | +### Issue 1: "--include" parameter not recognized |
| 37 | + |
| 38 | +If you're using Karma with Jasmine, the `--include` parameter might not work. Instead, you can: |
| 39 | + |
| 40 | +**Solution A:** Use a karma.conf.js configuration that supports file filtering: |
| 41 | +```javascript |
| 42 | +// karma.conf.js |
| 43 | +module.exports = function(config) { |
| 44 | + config.set({ |
| 45 | + // ... other config |
| 46 | + files: [ |
| 47 | + { pattern: './src/**/*.spec.ts', included: true, watched: true } |
| 48 | + ], |
| 49 | + }); |
| 50 | +}; |
| 51 | +``` |
| 52 | + |
| 53 | +**Solution B:** Update package.json to support include patterns: |
| 54 | +```json |
| 55 | +{ |
| 56 | + "scripts": { |
| 57 | + "test:ci": "ng test --watch=false --code-coverage --browsers=ChromeHeadless", |
| 58 | + "test:plugin": "ng test --watch=false --browsers=ChromeHeadless --include='**/time-planning-pn/**/*.spec.ts'" |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### Issue 2: ChromeHeadless not available |
| 64 | + |
| 65 | +If ChromeHeadless browser is not configured: |
| 66 | + |
| 67 | +**Solution:** Install and configure Chrome headless in karma.conf.js: |
| 68 | +```javascript |
| 69 | +// karma.conf.js |
| 70 | +module.exports = function(config) { |
| 71 | + config.set({ |
| 72 | + browsers: ['ChromeHeadless'], |
| 73 | + customLaunchers: { |
| 74 | + ChromeHeadlessCI: { |
| 75 | + base: 'ChromeHeadless', |
| 76 | + flags: ['--no-sandbox', '--disable-gpu'] |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | +}; |
| 81 | +``` |
| 82 | + |
| 83 | +Then update package.json: |
| 84 | +```json |
| 85 | +{ |
| 86 | + "scripts": { |
| 87 | + "test:ci": "ng test --watch=false --browsers=ChromeHeadlessCI" |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Issue 3: Angular version compatibility |
| 93 | + |
| 94 | +For Angular 15+, you might need to use the new test configuration: |
| 95 | + |
| 96 | +```json |
| 97 | +{ |
| 98 | + "scripts": { |
| 99 | + "test": "ng test", |
| 100 | + "test:ci": "ng test --no-watch --no-progress --browsers=ChromeHeadless --code-coverage" |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Issue 4: Jest instead of Karma |
| 106 | + |
| 107 | +If your project uses Jest instead of Karma: |
| 108 | + |
| 109 | +```json |
| 110 | +{ |
| 111 | + "scripts": { |
| 112 | + "test": "jest", |
| 113 | + "test:ci": "jest --ci --coverage --testPathPattern='time-planning-pn'" |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +## Recommended Configuration |
| 119 | + |
| 120 | +For the most compatibility with the time-planning-pn plugin tests, use this configuration: |
| 121 | + |
| 122 | +```json |
| 123 | +{ |
| 124 | + "scripts": { |
| 125 | + "test": "ng test", |
| 126 | + "test:ci": "ng test --no-watch --no-progress --browsers=ChromeHeadless --code-coverage", |
| 127 | + "test:headless": "ng test --no-watch --browsers=ChromeHeadless" |
| 128 | + }, |
| 129 | + "devDependencies": { |
| 130 | + "@angular-devkit/build-angular": "^15.0.0", |
| 131 | + "karma": "~6.4.0", |
| 132 | + "karma-chrome-launcher": "~3.1.0", |
| 133 | + "karma-coverage": "~2.2.0", |
| 134 | + "karma-jasmine": "~5.1.0", |
| 135 | + "karma-jasmine-html-reporter": "~2.0.0" |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## Workflow Behavior |
| 141 | + |
| 142 | +The GitHub Actions workflow (`.github/workflows/dotnet-core-master.yml` and `dotnet-core-pr.yml`) will: |
| 143 | + |
| 144 | +1. Check if `test:ci` script exists in package.json |
| 145 | +2. If yes, run: `npm run test:ci -- --include='**/time-planning-pn/**/*.spec.ts'` |
| 146 | +3. If no, check if `test` script exists |
| 147 | +4. If yes, run: `npm run test -- --watch=false --browsers=ChromeHeadless --include='**/time-planning-pn/**/*.spec.ts'` |
| 148 | +5. If neither exists, skip the tests with a message |
| 149 | +6. The step has `continue-on-error: true`, so it won't fail the entire workflow |
| 150 | + |
| 151 | +## Testing Locally |
| 152 | + |
| 153 | +To test if your configuration works: |
| 154 | + |
| 155 | +```bash |
| 156 | +# Clone both repositories |
| 157 | +git clone https://github.com/microting/eform-angular-frontend.git |
| 158 | +git clone https://github.com/microting/eform-angular-timeplanning-plugin.git |
| 159 | + |
| 160 | +# Copy plugin files |
| 161 | +cp -r eform-angular-timeplanning-plugin/eform-client/src/app/plugins/modules/time-planning-pn \ |
| 162 | + eform-angular-frontend/eform-client/src/app/plugins/modules/ |
| 163 | + |
| 164 | +# Install dependencies |
| 165 | +cd eform-angular-frontend/eform-client |
| 166 | +npm install |
| 167 | + |
| 168 | +# Try running tests |
| 169 | +npm run test:ci -- --include='**/time-planning-pn/**/*.spec.ts' |
| 170 | +# or |
| 171 | +npm run test -- --watch=false --browsers=ChromeHeadless |
| 172 | +``` |
| 173 | + |
| 174 | +## Contact |
| 175 | + |
| 176 | +If you need help configuring the tests, check: |
| 177 | +- Angular CLI testing documentation: https://angular.io/guide/testing |
| 178 | +- Karma configuration: https://karma-runner.github.io/latest/config/configuration-file.html |
| 179 | +- The test files in this repository for examples |
0 commit comments