mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-28 22:42:11 +01:00
Merged PR 1967: Reward Shopping Cart Implementation
This commit is contained in:
committed by
Nino Righi
parent
d761704dc4
commit
f15848d5c0
@@ -13,6 +13,7 @@
|
||||
* [Vitest: Modern Testing Framework](#vitest-modern-testing-framework)
|
||||
* [Overview](#vitest-overview)
|
||||
* [Configuration](#vitest-configuration)
|
||||
* [CI/CD Integration: JUnit and Cobertura Reporting](#cicd-integration-junit-and-cobertura-reporting)
|
||||
* [Core Testing Features](#core-testing-features)
|
||||
* [Mocking in Vitest](#mocking-in-vitest)
|
||||
* [Example Test Structures with Vitest](#example-test-structures-with-vitest)
|
||||
@@ -144,6 +145,128 @@ export default defineConfig({
|
||||
});
|
||||
```
|
||||
|
||||
#### CI/CD Integration: JUnit and Cobertura Reporting
|
||||
|
||||
Both Jest and Vitest are configured to generate JUnit XML reports and Cobertura coverage reports for Azure Pipelines integration.
|
||||
|
||||
##### Jest Configuration (Existing Libraries)
|
||||
|
||||
Jest projects inherit JUnit and Cobertura configuration from `jest.preset.js`:
|
||||
|
||||
```javascript
|
||||
// jest.preset.js (workspace root)
|
||||
module.exports = {
|
||||
...nxPreset,
|
||||
coverageReporters: ['text', 'cobertura'],
|
||||
reporters: [
|
||||
'default',
|
||||
[
|
||||
'jest-junit',
|
||||
{
|
||||
outputDirectory: 'testresults',
|
||||
outputName: 'TESTS',
|
||||
uniqueOutputName: 'true',
|
||||
classNameTemplate: '{classname}',
|
||||
titleTemplate: '{title}',
|
||||
ancestorSeparator: ' › ',
|
||||
usePathForSuiteName: true,
|
||||
},
|
||||
],
|
||||
],
|
||||
};
|
||||
```
|
||||
|
||||
**Key Points:**
|
||||
- JUnit XML files are written to `testresults/TESTS-{uuid}.xml`
|
||||
- Cobertura coverage reports are written to `coverage/{projectPath}/cobertura-coverage.xml`
|
||||
- No additional configuration needed in individual Jest projects
|
||||
- Run with coverage: `npx nx test <project> --code-coverage`
|
||||
|
||||
##### Vitest Configuration (New Libraries)
|
||||
|
||||
Vitest projects require explicit JUnit and Cobertura configuration in their `vite.config.mts` files:
|
||||
|
||||
```typescript
|
||||
// libs/{domain}/{library}/vite.config.mts
|
||||
/// <reference types='vitest' />
|
||||
import { defineConfig } from 'vite';
|
||||
import angular from '@analogjs/vite-plugin-angular';
|
||||
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
||||
import { nxCopyAssetsPlugin } from '@nx/vite/plugins/nx-copy-assets.plugin';
|
||||
|
||||
export default
|
||||
// @ts-expect-error - Vitest reporter tuple types have complex inference issues, but config works correctly at runtime
|
||||
defineConfig(() => ({
|
||||
root: __dirname,
|
||||
cacheDir: '../../../node_modules/.vite/libs/{domain}/{library}',
|
||||
plugins: [angular(), nxViteTsPaths(), nxCopyAssetsPlugin(['*.md'])],
|
||||
test: {
|
||||
watch: false,
|
||||
globals: true,
|
||||
environment: 'jsdom',
|
||||
include: ['{src,tests}/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
|
||||
setupFiles: ['src/test-setup.ts'],
|
||||
reporters: [
|
||||
'default',
|
||||
['junit', { outputFile: '../../../testresults/junit-{project-name}.xml' }],
|
||||
],
|
||||
coverage: {
|
||||
reportsDirectory: '../../../coverage/libs/{domain}/{library}',
|
||||
provider: 'v8' as const,
|
||||
reporter: ['text', 'cobertura'],
|
||||
},
|
||||
},
|
||||
}));
|
||||
```
|
||||
|
||||
**Key Points:**
|
||||
- **JUnit Reporter**: Built into Vitest, no additional package needed
|
||||
- **Output Path**: Adjust relative path based on library depth:
|
||||
- 3 levels (`libs/domain/library`): Use `../../../testresults/`
|
||||
- 4 levels (`libs/domain/type/library`): Use `../../../../testresults/`
|
||||
- **Coverage Reporter**: Add `'cobertura'` to the reporter array
|
||||
- **TypeScript Suppression**: Add `// @ts-expect-error` comment before `defineConfig` to suppress type inference warnings
|
||||
- **Run with Coverage**: `npx nx test <project> --coverage.enabled=true`
|
||||
|
||||
##### Azure Pipelines Integration
|
||||
|
||||
Both Jest and Vitest reports are consumed by Azure Pipelines:
|
||||
|
||||
```yaml
|
||||
# azure-pipelines.yml
|
||||
- task: PublishTestResults@2
|
||||
displayName: Publish Test results
|
||||
inputs:
|
||||
testResultsFiles: '**/TESTS-*.xml'
|
||||
searchFolder: $(Build.StagingDirectory)/testresults
|
||||
testResultsFormat: JUnit
|
||||
mergeTestResults: false
|
||||
failTaskOnFailedTests: true
|
||||
|
||||
- task: PublishCodeCoverageResults@2
|
||||
displayName: Publish code Coverage
|
||||
inputs:
|
||||
codeCoverageTool: Cobertura
|
||||
summaryFileLocation: $(Build.StagingDirectory)/coverage/**/cobertura-coverage.xml
|
||||
```
|
||||
|
||||
**Verification:**
|
||||
- JUnit XML files: `testresults/junit-*.xml` or `testresults/TESTS-*.xml`
|
||||
- Cobertura XML files: `coverage/libs/{path}/cobertura-coverage.xml`
|
||||
|
||||
##### New Library Checklist
|
||||
|
||||
When creating a new Vitest-based library, ensure:
|
||||
|
||||
1. ✅ `reporters` array includes both `'default'` and JUnit configuration
|
||||
2. ✅ JUnit `outputFile` uses correct relative path depth
|
||||
3. ✅ Coverage `reporter` array includes `'cobertura'`
|
||||
4. ✅ Add `// @ts-expect-error` comment before `defineConfig()` if TypeScript errors appear
|
||||
5. ✅ Verify report generation: Run `npx nx test <project> --coverage.enabled=true --skip-cache`
|
||||
6. ✅ Check files exist:
|
||||
- `testresults/junit-{project-name}.xml`
|
||||
- `coverage/libs/{path}/cobertura-coverage.xml`
|
||||
|
||||
#### Core Testing Features
|
||||
|
||||
Vitest provides similar APIs to Jest with enhanced performance:
|
||||
|
||||
Reference in New Issue
Block a user