refactor: migrate testing framework from Karma/Jasmine to Vitest with Angular support
This commit is contained in:
1 parent
237d995e16
commit
11924df19e
14 files changed
+1351
-273
No files matched your search
+2
-10
@@ -45,18 +45,10 @@ type-check:
|
|||||||
# -- Test Stage --
|
# -- Test Stage --
|
||||||
|
|
||||||
test:
|
test:
|
||||||
|
extends: .node_setup
|
||||||
stage: test
|
stage: test
|
||||||
before_script:
|
|
||||||
- apt-get update && apt-get install -y chromium
|
|
||||||
- bun install --frozen-lockfile
|
|
||||||
variables:
|
|
||||||
CHROME_BIN: /usr/bin/chromium
|
|
||||||
script:
|
script:
|
||||||
- bun run test
|
- bun run test --watch=false --coverage
|
||||||
--watch=false
|
|
||||||
--browsers=ChromeHeadlessCI
|
|
||||||
--code-coverage
|
|
||||||
--no-progress
|
|
||||||
artifacts:
|
artifacts:
|
||||||
when: always
|
when: always
|
||||||
paths:
|
paths:
|
||||||
|
|||||||
+1
-12
@@ -81,18 +81,7 @@
|
|||||||
"builder": "@angular/build:extract-i18n"
|
"builder": "@angular/build:extract-i18n"
|
||||||
},
|
},
|
||||||
"test": {
|
"test": {
|
||||||
"builder": "@angular/build:karma",
|
"builder": "@analogjs/vitest-angular:test"
|
||||||
"options": {
|
|
||||||
"tsConfig": "tsconfig.spec.json",
|
|
||||||
"inlineStyleLanguage": "scss",
|
|
||||||
"assets": [
|
|
||||||
{
|
|
||||||
"glob": "**/*",
|
|
||||||
"input": "public"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"styles": ["src/styles.scss"]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
module.exports = function (config) {
|
|
||||||
config.set({
|
|
||||||
basePath: '',
|
|
||||||
frameworks: ['jasmine', '@angular-devkit/build-angular'],
|
|
||||||
plugins: [
|
|
||||||
require('karma-jasmine'),
|
|
||||||
require('karma-chrome-launcher'),
|
|
||||||
require('karma-jasmine-html-reporter'),
|
|
||||||
require('karma-coverage'),
|
|
||||||
require('@angular-devkit/build-angular/plugins/karma'),
|
|
||||||
],
|
|
||||||
|
|
||||||
proxies: {},
|
|
||||||
|
|
||||||
client: {
|
|
||||||
jasmine: {},
|
|
||||||
clearContext: false,
|
|
||||||
},
|
|
||||||
coverageReporter: {
|
|
||||||
dir: require('path').join(__dirname, './coverage'),
|
|
||||||
subdir: '.',
|
|
||||||
reporters: [{ type: 'html' }, { type: 'text-summary' }, { type: 'lcovonly' }],
|
|
||||||
},
|
|
||||||
reporters: ['progress', 'kjhtml'],
|
|
||||||
browsers: ['Chrome'],
|
|
||||||
customLaunchers: {
|
|
||||||
ChromeHeadlessCI: {
|
|
||||||
base: 'ChromeHeadless',
|
|
||||||
flags: [
|
|
||||||
'--no-sandbox',
|
|
||||||
'--disable-gpu',
|
|
||||||
'--disable-dev-shm-usage',
|
|
||||||
'--disable-extensions',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
singleRun: false,
|
|
||||||
restartOnFileChange: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
+12
-8
@@ -46,27 +46,31 @@
|
|||||||
"tslib": "^2.8.1"
|
"tslib": "^2.8.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@analogjs/platform": "^2.5.2",
|
||||||
|
"@analogjs/vite-plugin-angular": "^2.5.2",
|
||||||
|
"@analogjs/vitest-angular": "^2.5.2",
|
||||||
"@angular/build": "21.2.13",
|
"@angular/build": "21.2.13",
|
||||||
"@angular/cli": "21.2.13",
|
"@angular/cli": "21.2.13",
|
||||||
"@angular/compiler-cli": "21.2.15",
|
"@angular/compiler-cli": "21.2.15",
|
||||||
"@eslint/js": "^10.0.1",
|
"@eslint/js": "^10.0.1",
|
||||||
|
"@nx/devkit": "^22.7.5",
|
||||||
|
"@nx/vite": "^22.0.0",
|
||||||
"@types/express": "^5.0.6",
|
"@types/express": "^5.0.6",
|
||||||
"@types/jasmine": "~6.0.0",
|
|
||||||
"@types/jest": "^30.0.0",
|
"@types/jest": "^30.0.0",
|
||||||
"@types/node": "^25.9.1",
|
"@types/node": "^25.9.1",
|
||||||
|
"@vitest/coverage-v8": "^4.0.0",
|
||||||
|
"@vitest/ui": "^4.0.0",
|
||||||
"eslint": "^10.4.1",
|
"eslint": "^10.4.1",
|
||||||
"eslint-config-prettier": "^10.1.8",
|
"eslint-config-prettier": "^10.1.8",
|
||||||
"eslint-plugin-prettier": "^5.5.6",
|
"eslint-plugin-prettier": "^5.5.6",
|
||||||
"jasmine-core": "~6.2.0",
|
|
||||||
"jest": "^30.4.2",
|
"jest": "^30.4.2",
|
||||||
"jest-preset-angular": "^16.1.5",
|
"jest-preset-angular": "^16.1.5",
|
||||||
"karma": "~6.4.4",
|
"jsdom": "^22.0.0",
|
||||||
"karma-chrome-launcher": "~3.2.0",
|
|
||||||
"karma-coverage": "~2.2.1",
|
|
||||||
"karma-jasmine": "~5.1.0",
|
|
||||||
"karma-jasmine-html-reporter": "~2.2.0",
|
|
||||||
"prettier": "^3.8.3",
|
"prettier": "^3.8.3",
|
||||||
"typescript": "~6.0.3",
|
"typescript": "~6.0.3",
|
||||||
"typescript-eslint": "^8.60.0"
|
"typescript-eslint": "^8.60.0",
|
||||||
|
"vite": "^7.0.0",
|
||||||
|
"vite-tsconfig-paths": "^4.2.0",
|
||||||
|
"vitest": "^4.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
import { provideZonelessChangeDetection } from '@angular/core';
|
import { provideZonelessChangeDetection } from '@angular/core';
|
||||||
import { MatDialogRef } from '@angular/material/dialog';
|
import { MatDialogRef } from '@angular/material/dialog';
|
||||||
|
import { vi } from 'vitest';
|
||||||
import { CalendarSettingsDialog } from './calendar-settings-dialog';
|
import { CalendarSettingsDialog } from './calendar-settings-dialog';
|
||||||
|
|
||||||
describe('CalendarSettingsDialog', () => {
|
describe('CalendarSettingsDialog', () => {
|
||||||
@@ -15,7 +16,7 @@ describe('CalendarSettingsDialog', () => {
|
|||||||
{
|
{
|
||||||
provide: MatDialogRef,
|
provide: MatDialogRef,
|
||||||
useValue: {
|
useValue: {
|
||||||
close: jasmine.createSpy('close'),
|
close: vi.fn(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ describe('RulesPanel', () => {
|
|||||||
it('should start adding new If-Then rule', () => {
|
it('should start adding new If-Then rule', () => {
|
||||||
component.startAddNew(RuleType.IF_THEN);
|
component.startAddNew(RuleType.IF_THEN);
|
||||||
|
|
||||||
expect(component['isAddingNew']()).toBeTrue();
|
expect(component['isAddingNew']()).toBe(true);
|
||||||
expect(component['newRuleType']()).toBe(RuleType.IF_THEN);
|
expect(component['newRuleType']()).toBe(RuleType.IF_THEN);
|
||||||
expect(component['editBuffer'].type).toBe(RuleType.IF_THEN);
|
expect(component['editBuffer'].type).toBe(RuleType.IF_THEN);
|
||||||
expect(component['editBuffer'].name).toBe('Nouvelle Règle');
|
expect(component['editBuffer'].name).toBe('Nouvelle Règle');
|
||||||
@@ -42,7 +42,7 @@ describe('RulesPanel', () => {
|
|||||||
it('should start adding new Time Window rule', () => {
|
it('should start adding new Time Window rule', () => {
|
||||||
component.startAddNew(RuleType.TIME_WINDOW);
|
component.startAddNew(RuleType.TIME_WINDOW);
|
||||||
|
|
||||||
expect(component['isAddingNew']()).toBeTrue();
|
expect(component['isAddingNew']()).toBe(true);
|
||||||
expect(component['newRuleType']()).toBe(RuleType.TIME_WINDOW);
|
expect(component['newRuleType']()).toBe(RuleType.TIME_WINDOW);
|
||||||
expect(component['editBuffer'].type).toBe(RuleType.TIME_WINDOW);
|
expect(component['editBuffer'].type).toBe(RuleType.TIME_WINDOW);
|
||||||
expect(component['editBuffer'].name).toBe('Nouvelle Période');
|
expect(component['editBuffer'].name).toBe('Nouvelle Période');
|
||||||
@@ -52,7 +52,7 @@ describe('RulesPanel', () => {
|
|||||||
component.startAddNew(RuleType.IF_THEN);
|
component.startAddNew(RuleType.IF_THEN);
|
||||||
component.cancelEdit();
|
component.cancelEdit();
|
||||||
|
|
||||||
expect(component['isAddingNew']()).toBeFalse();
|
expect(component['isAddingNew']()).toBe(false);
|
||||||
expect(component['editingRuleId']()).toBeNull();
|
expect(component['editingRuleId']()).toBeNull();
|
||||||
expect(Object.keys(component['editBuffer']).length).toBe(0);
|
expect(Object.keys(component['editBuffer']).length).toBe(0);
|
||||||
});
|
});
|
||||||
@@ -65,7 +65,7 @@ describe('RulesPanel', () => {
|
|||||||
component.saveEdit();
|
component.saveEdit();
|
||||||
|
|
||||||
expect(component.rules.length).toBe(initialCount + 1);
|
expect(component.rules.length).toBe(initialCount + 1);
|
||||||
expect(component.rules.some((r) => r.name === 'Custom Test Rule')).toBeTrue();
|
expect(component.rules.some((r) => r.name === 'Custom Test Rule')).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should update an existing rule', () => {
|
it('should update an existing rule', () => {
|
||||||
@@ -89,7 +89,7 @@ describe('RulesPanel', () => {
|
|||||||
component.deleteRule(targetId, mockEvent);
|
component.deleteRule(targetId, mockEvent);
|
||||||
|
|
||||||
expect(component.rules.length).toBe(initialCount - 1);
|
expect(component.rules.length).toBe(initialCount - 1);
|
||||||
expect(component.rules.some((r) => r.id === targetId)).toBeFalse();
|
expect(component.rules.some((r) => r.id === targetId)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should toggle a rule', () => {
|
it('should toggle a rule', () => {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Component, ChangeDetectionStrategy, inject, signal } from '@angular/core';
|
import { Component, ChangeDetectionStrategy, inject, signal, Injectable } from '@angular/core';
|
||||||
import {
|
import {
|
||||||
RuleService,
|
RuleService,
|
||||||
RuleType,
|
RuleType,
|
||||||
@@ -26,6 +26,7 @@ import {
|
|||||||
} from '@angular/material/core';
|
} from '@angular/material/core';
|
||||||
import { CdkDragDrop, CdkDropList, CdkDrag } from '@angular/cdk/drag-drop';
|
import { CdkDragDrop, CdkDropList, CdkDrag } from '@angular/cdk/drag-drop';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
class CustomDateAdapter extends NativeDateAdapter {
|
class CustomDateAdapter extends NativeDateAdapter {
|
||||||
override format(date: Date, displayFormat: unknown): string {
|
override format(date: Date, displayFormat: unknown): string {
|
||||||
if (displayFormat === 'input') {
|
if (displayFormat === 'input') {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Component, input, output, ChangeDetectionStrategy } from '@angular/core';
|
import { Component, input, output, ChangeDetectionStrategy, Injectable } from '@angular/core';
|
||||||
import { SelectableDayType, tooltipTypeMapping, VacDay } from '../../../services/calendar-service';
|
import { SelectableDayType, tooltipTypeMapping, VacDay } from '../../../services/calendar-service';
|
||||||
import { MatCard } from '@angular/material/card';
|
import { MatCard } from '@angular/material/card';
|
||||||
import { MatOption, MatSelect } from '@angular/material/select';
|
import { MatOption, MatSelect } from '@angular/material/select';
|
||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
import { MatIconModule } from '@angular/material/icon';
|
import { MatIconModule } from '@angular/material/icon';
|
||||||
import { MatButtonModule } from '@angular/material/button';
|
import { MatButtonModule } from '@angular/material/button';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
class CustomDateAdapter extends NativeDateAdapter {
|
class CustomDateAdapter extends NativeDateAdapter {
|
||||||
override format(date: Date, displayFormat: unknown): string {
|
override format(date: Date, displayFormat: unknown): string {
|
||||||
if (displayFormat === 'input') {
|
if (displayFormat === 'input') {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { TestBed } from '@angular/core/testing';
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
import { vi } from 'vitest';
|
||||||
import { provideZonelessChangeDetection } from '@angular/core';
|
import { provideZonelessChangeDetection } from '@angular/core';
|
||||||
import {
|
import {
|
||||||
CalendarService,
|
CalendarService,
|
||||||
@@ -228,20 +229,20 @@ describe('SelectedDates', () => {
|
|||||||
{ id: '3', type: RuleType.IF_THEN, enabled: true } as IfThenRule,
|
{ id: '3', type: RuleType.IF_THEN, enabled: true } as IfThenRule,
|
||||||
];
|
];
|
||||||
|
|
||||||
const applyTimeWindowRulesSpy = spyOn(selectedDates, 'applyTimeWindowRules');
|
const applyTimeWindowRulesSpy = vi.spyOn(selectedDates, 'applyTimeWindowRules');
|
||||||
const applyIfThenRulesSpy = spyOn(selectedDates, 'applyIfThenRules');
|
const applyIfThenRulesSpy = vi.spyOn(selectedDates, 'applyIfThenRules');
|
||||||
|
|
||||||
await selectedDates.optimizeVacations(vacationDays, rules, calendarService);
|
await selectedDates.optimizeVacations(vacationDays, rules, calendarService);
|
||||||
|
|
||||||
expect(applyIfThenRulesSpy.calls.count()).toBe(2);
|
expect(applyIfThenRulesSpy.mock.calls.length).toBe(2);
|
||||||
expect(applyTimeWindowRulesSpy.calls.count()).toBe(1);
|
expect(applyTimeWindowRulesSpy.mock.calls.length).toBe(1);
|
||||||
|
|
||||||
// First rule is IF_THEN
|
// First rule is IF_THEN
|
||||||
expect(applyIfThenRulesSpy.calls.argsFor(0)[1][0].id).toBe('1');
|
expect(applyIfThenRulesSpy.mock.calls[0][1][0].id).toBe('1');
|
||||||
// Second rule is TIME_WINDOW
|
// Second rule is TIME_WINDOW
|
||||||
expect(applyTimeWindowRulesSpy.calls.argsFor(0)[1][0].id).toBe('2');
|
expect(applyTimeWindowRulesSpy.mock.calls[0][1][0].id).toBe('2');
|
||||||
// Third rule is IF_THEN
|
// Third rule is IF_THEN
|
||||||
expect(applyIfThenRulesSpy.calls.argsFor(1)[1][0].id).toBe('3');
|
expect(applyIfThenRulesSpy.mock.calls[1][1][0].id).toBe('3');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should apply time window rules', () => {
|
it('should apply time window rules', () => {
|
||||||
@@ -268,7 +269,7 @@ describe('SelectedDates', () => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
// Mock monthsInAYearFromNow to return a controlled range of days
|
// Mock monthsInAYearFromNow to return a controlled range of days
|
||||||
spyOn(calendarService, 'monthsInAYearFromNow').and.returnValue([
|
vi.spyOn(calendarService, 'monthsInAYearFromNow').mockReturnValue([
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
new Date(futureYear, 11, 20),
|
new Date(futureYear, 11, 20),
|
||||||
@@ -376,7 +377,7 @@ describe('SelectedDates', () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
spyOn(calendarService, 'monthsInAYearFromNow').and.returnValue([
|
vi.spyOn(calendarService, 'monthsInAYearFromNow').mockReturnValue([
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
new Date(futureYear, 6, 1),
|
new Date(futureYear, 6, 1),
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ describe('RuleService', () => {
|
|||||||
const rules = service.rules;
|
const rules = service.rules;
|
||||||
const christmasRule = rules.find((r) => r.type === RuleType.TIME_WINDOW) as TimeWindowRule;
|
const christmasRule = rules.find((r) => r.type === RuleType.TIME_WINDOW) as TimeWindowRule;
|
||||||
expect(christmasRule).toBeDefined();
|
expect(christmasRule).toBeDefined();
|
||||||
expect(christmasRule.startDate instanceof Date).toBeTrue();
|
expect(christmasRule.startDate instanceof Date).toBe(true);
|
||||||
expect(christmasRule.endDate instanceof Date).toBeTrue();
|
expect(christmasRule.endDate instanceof Date).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add a new rule', () => {
|
it('should add a new rule', () => {
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import '@angular/compiler';
|
||||||
|
import { vi } from 'vitest';
|
||||||
|
import '@analogjs/vitest-angular/setup-snapshots';
|
||||||
|
import '@analogjs/vitest-angular/setup-serializers';
|
||||||
|
import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed';
|
||||||
|
|
||||||
|
setupTestBed();
|
||||||
|
|
||||||
|
Object.defineProperty(window, 'matchMedia', {
|
||||||
|
writable: true,
|
||||||
|
value: vi.fn().mockImplementation((query: string) => ({
|
||||||
|
matches: false,
|
||||||
|
media: query,
|
||||||
|
onchange: null,
|
||||||
|
addListener: vi.fn(),
|
||||||
|
removeListener: vi.fn(),
|
||||||
|
addEventListener: vi.fn(),
|
||||||
|
removeEventListener: vi.fn(),
|
||||||
|
dispatchEvent: vi.fn(),
|
||||||
|
})),
|
||||||
|
});
|
||||||
+4
-4
@@ -1,10 +1,10 @@
|
|||||||
/* To learn more about Typescript configuration file: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html. */
|
|
||||||
/* To learn more about Angular compiler options: https://angular.dev/reference/configs/angular-compiler-options. */
|
|
||||||
{
|
{
|
||||||
"extends": "./tsconfig.json",
|
"extends": "./tsconfig.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"outDir": "./out-tsc/spec",
|
"outDir": "./out-tsc/spec",
|
||||||
"types": ["jasmine"]
|
"types": ["jasmine", "vitest/globals"],
|
||||||
|
"target": "es2022"
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts"]
|
"include": ["src/**/*.ts"],
|
||||||
|
"files": ["src/test-setup.ts"]
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/// <reference types="vitest" />
|
||||||
|
|
||||||
|
import angular from '@analogjs/vite-plugin-angular';
|
||||||
|
|
||||||
|
import { defineConfig } from 'vite';
|
||||||
|
|
||||||
|
// https://vitejs.dev/config/
|
||||||
|
export default defineConfig(({ mode }) => {
|
||||||
|
return {
|
||||||
|
plugins: [angular()],
|
||||||
|
test: {
|
||||||
|
globals: true,
|
||||||
|
environment: 'jsdom',
|
||||||
|
setupFiles: ['src/test-setup.ts'],
|
||||||
|
include: ['**/*.spec.ts'],
|
||||||
|
reporters: ['default'],
|
||||||
|
coverage: {
|
||||||
|
provider: 'v8',
|
||||||
|
reporter: ['text', 'html', 'cobertura'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
define: {
|
||||||
|
'import.meta.vitest': mode !== 'production',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
Reference in new issue
Block a user