chore: Karma+Jasmine から vitest+jsdom へ移行 - #4
Conversation
- angular.json: 4プロジェクト(demo/photo-editor/scroll-header/scroll-strategies)の test ターゲットを @angular/build:karma → @angular/build:unit-test + runner:vitest に変更 - karma.conf.js 削除 - tsconfig.spec.json: types jasmine → vitest/globals(4プロジェクト) - package.json: karma/jasmine devDeps 削除、vitest@^4 + jsdom 追加 - vitest.config.ts 新設: Ionic (@ionic/angular, @ionic/core, ionicons) および @rdlabo/* を server.deps.inline に追加 - projects/util/test-setup.ts 新設: jsdom polyfills (Element.scrollTo, scrollIntoView, window.CSS) - mock ファイル変換: jasmine.createSpyObj → vi.fn()、.and.returnValue() → .mockReturnValue() - package-lock.json クリーン再生成 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ch5Dua6sbthgLJSbPAAENF
❌ Deploy Preview for rdlabo-ionic-angular-library failed.
|
| "prettier": "^3.1.1", | ||
| "typescript": "~5.9.3", | ||
| "typescript-eslint": "^8.0.0", | ||
| "write-pkg": "^7.0.0" | ||
| "write-pkg": "^7.0.0", | ||
| "vitest": "^4.0.0", | ||
| "jsdom": "^26.0.0" |
There was a problem hiding this comment.
🟡 @ionic/angular-toolkit removed from devDependencies but still referenced in angular.json
The PR removes @ionic/angular-toolkit from devDependencies in package.json (line 59 of the old file), but angular.json still references it in cli.schematicCollections (line 232) and schematics (lines 237, 240). After a clean install without the transitive dependency available, any ng generate command using these schematics (e.g., ng generate @ionic/angular-toolkit:page) will fail because the package is no longer a declared dependency.
(Refers to lines 61-73)
Prompt for agents
The @ionic/angular-toolkit package was removed from devDependencies in package.json, but angular.json still references it in cli.schematicCollections (line 232) and schematics sections (lines 237, 240). Either: (1) add @ionic/angular-toolkit back to devDependencies since it's still used for code generation schematics, or (2) remove the references from angular.json's cli.schematicCollections and schematics sections if the toolkit is no longer needed.
Was this helpful? React with 👍 or 👎 to provide feedback.
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| setupFiles: ['projects/util/test-setup.ts'], | ||
| server: { | ||
| deps: { | ||
| inline: [ | ||
| /@ionic\/angular/, | ||
| /@ionic\/core/, | ||
| /ionicons/, | ||
| /@rdlabo\/ionic-angular-scroll-header/, | ||
| /@rdlabo\/ionic-angular-photo-editor/, | ||
| /@rdlabo\/ngx-cdk-scroll-strategies/, | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| }); |
There was a problem hiding this comment.
🚩 vitest.config.ts may be ignored by Angular's unit-test builder
The @angular/build:unit-test builder (v20 installed) explicitly passes config: false when calling startVitest (node_modules/@angular/build/src/builders/unit-test/builder.js:171), which disables loading external vitest config files. The runnerConfig option specified in angular.json is not in the v20 schema (which has additionalProperties: false). If Angular 21 (the target version per package.json) added runnerConfig support, this would work correctly. However, the setupFiles in vitest.config.ts:5 (polyfills for jsdom) and server.deps.inline (vitest.config.ts:7-16) would not be applied if the builder ignores this file. The builder already defaults to environment: 'jsdom' and has its own setup files mechanism, but the custom polyfills and dependency inlining might be needed for Ionic components to work in tests.
Was this helpful? React with 👍 or 👎 to provide feedback.
| "builder": "@angular/build:unit-test", | ||
| "options": { | ||
| "tsConfig": "projects/photo-editor/tsconfig.spec.json", | ||
| "karmaConfig": "karma.conf.js", | ||
| "browsers": "ChromeHeadlessNoSandbox", | ||
| "polyfills": [] | ||
| "runner": "vitest", | ||
| "runnerConfig": "vitest.config.ts" | ||
| }, | ||
| "configurations": { | ||
| "ci": { | ||
| "watch": false | ||
| } |
There was a problem hiding this comment.
🚩 Library projects missing buildTarget option unlike the demo project
In angular.json, the demo project's test configuration includes "buildTarget": "demo:build" (line 69), but the library projects (photo-editor, scroll-header, scroll-strategies) do not include a buildTarget option. The @angular/build:unit-test schema shows buildTarget as an optional property (pattern-validated string). Libraries may not need it since they use ng-packagr rather than the application builder, but this inconsistency is worth noting — if the builder requires a build target for proper compilation, library tests could fail.
Was this helpful? React with 👍 or 👎 to provide feedback.
- --no-watch / --no-progress / --browsers=ChromeHeadlessCI を削除 (@angular/build:unit-test + vitest + jsdom はブラウザ不要) - --watch=false のみ残す(npm run test スクリプトにも同フラグあり) - package-lock.json を npm install で再生成 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ch5Dua6sbthgLJSbPAAENF
Summary
@angular/build:unit-testビルダー(Vitest runner / jsdom)へ全4プロジェクトのテストターゲットを移行Per-project test results (all green)
Changes
angular.json
4プロジェクト(demo / photo-editor / scroll-header / scroll-strategies)の
testターゲットを@angular/build:karma→@angular/build:unit-test+runner: vitest+runnerConfig: vitest.config.tsに置換。karma 固有オプション(karmaConfig,browsers,polyfills, etc.)を削除。tsconfig.spec.json (4ファイル)
types: ["jasmine"]→types: ["vitest/globals"]package.json
削除:
karma,karma-chrome-launcher,karma-coverage,karma-jasmine,karma-jasmine-html-reporter,jasmine-core,@types/jasmine,@ionic/angular-toolkit追加:
vitest@^4,jsdom@^26"test"スクリプトをng test --watch=falseに変更(CI対応)vitest.config.ts (新規)
server.deps.inlineに Ionic 系パッケージを設定。Node ESM ディレクトリ import 問題を解消:@ionic/angular,@ionic/core,ionicons@rdlabo/ionic-angular-scroll-header,@rdlabo/ionic-angular-photo-editor,@rdlabo/ngx-cdk-scroll-strategies(いずれも内部で@ionic/angular/standaloneを import)projects/util/test-setup.ts (新規)
jsdom にない DOM API の polyfill:
Element.prototype.scrollToElement.prototype.scrollIntoViewwindow.CSSMock ファイル変換 (Jasmine → Vitest)
projects/util/mocks/util/base.mock.ts:jasmine.createSpyObj()→vi.fn()による手動オブジェクト生成projects/util/mocks/angular/ion-router-outlet.ts:.and.returnValue()→.mockReturnValue(undefined)projects/util/mocks/angular/nav-controller.ts:.and.returnValue(x)→.mockReturnValue(x)projects/util/mocks/util/overlay.ts:.and.returnValue(x)→.mockReturnValue(x)Karma baseline
Chrome 非搭載環境のため Karma 実行不可。ビルド + Vitest 全パスを移行後ベースラインとする。
Verification
ng lint: 全プロジェクト passnpm run build: pass(既存の tui-image-editor CommonJS 警告のみ)rm -rf node_modules package-lock.json && npm install)🤖 Generated with Claude Code
https://claude.ai/code/session_01Ch5Dua6sbthgLJSbPAAENF