test(vitest): vitest を導入して imUlid の UT を追加する#3
Merged
Conversation
imUlid はトラッキング用 visitorID の生成に使われ trust360 / im-cmp / IMUID-js 等の下流リポで利用されている。これまでテストが 無く、依存ライブラリ更新時の regression を検知できなかったため、 im-core と同じ vitest 2.1.9 + jsdom 25.0.1 構成で UT を導入する。 カバー範囲は imUlid 関数の以下 4 観点で計 13 tests: - 出力フォーマット: 26 文字、Crockford 風 base32 (I/L/O/U 除外) - 時刻部 (先頭 10 文字): Date.now を spy して 32 進エンコードの 正逆性を検算 + monotonicity - ランダム部 (末尾 16 文字): crypto.getRandomValues を mock して byte 値 & 31 が alphabet にマップされることを確認 - 多重呼び出し: 100 回連続でユニーク値を返すこと 未使用だった @types/jest は jest 本体への依存も無く dead weight だったため、本コミットで合わせて除去する。 webpack の ts-loader が entry 以外の test ファイルも型チェックして src/*.test.ts のテスト構文でエラーを出すため、build 専用の tsconfig.build.json を新設して test ファイルを除外する。eslint 側は test ファイルも lint 対象に含めたいので、tsconfig.json の include は据え置きで vitest.config.ts のみ .eslintignore に追加し、 gts 3 の node/no-unpublished-import は test ファイル限定で off に する override を .eslintrc.json に追加する。 package manager は packageManager フィールドで yarn 1.22.22 を 明示する。corepack 経由で newer yarn が解決された場合の lockfile 書き換えを防ぐ目的なのだ。 Co-Authored-By: Claude <noreply@anthropic.com>
これまで im-ulid には CI workflow が存在せず、push / PR で lint・vitest・build を自動実行する仕組みが無かった。 他リポと同じ pinact 済み action SHA / node 18.x の構成で test workflow を追加する。 package manager は master 段階の yarn 1.22.x をそのまま使う。 webpack 5.44 が node 17+ で ERR_OSSL_EVP_UNSUPPORTED で落ちるため、 NODE_OPTIONS=--openssl-legacy-provider を env で設定する。 これは migration PR で webpack 5.105+ に上がれば不要になる。 Co-Authored-By: Claude <noreply@anthropic.com>
初回コミット時点で stmt 100% / branch 75% (= 4 分岐中 3) だった
カバレッジを、唯一の未到達ブランチ msCrypto フォールバックパスに
対するテストを追加して branch coverage 100% に引き上げる。
実装側の `('crypto' in window ? crypto : msCrypto).getRandomValues(...)`
における msCrypto 経路は legacy IE 用フォールバックで、jsdom 環境では
通常通過しない。テスト内で window.crypto を一時的に削除し、
window.msCrypto に getRandomValues の mock を仕込んでから imUlid を
呼び出すことで該当パスを通す。finally で元の crypto を defineProperty
で復元するため後続テストへの副作用は無い。
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
im-ulid にはこれまで自動テストが一切無く、依存ライブラリ更新時の
regression を検知する手段が無かったのだ。imUlid はトラッキング用
visitorID の生成に使われ trust360 / im-cmp / IMUID-js 等の下流リポ
で利用されているため、Phase 0.5 の cmp-consent-pod と同じ
vitest + jsdom 構成で UT を導入するのだ。
本 PR は migration PR (#1) より前にマージしてベースラインを取る
目的の test PR なのだ。マージ後に #1 を rebase することで、
pnpm 移行・TypeScript 6 / gts 7 への bump による振る舞い変化が
あれば test が即落ちる構成になるのだ。
Changes
test(vitest): vitest 2.1.9 + jsdom 25.0.1 を devDependencies に追加、vitest.config.ts (jsdom 環境) を新規追加。
src/im-ulid.test.ts(13 tests) を追加:正逆性 + 既知値 (0/31/32) のエンコード結果 + monotonicity
byte 値 & 31 が alphabet にマップされることを検証
ci(test): `.github/workflows/test.yml` を新規追加。push / PR で lint → test → build を自動実行 (yarn 1.22 ベース、node 18.x)。
webpack 5.44 が node 17+ で落ちる対策で NODE_OPTIONS=--openssl-legacy-provider
を env で設定。
yarn 4 berry を解決して lockfile を書き換えるのを防ぐ。
`tsconfig.build.json` を新設して webpack の ts-loader を
そちら参照に切り替える。
Test plan
Co-Authored-By: Claude noreply@anthropic.com