Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
VitestのIn-Source Testingが便利
Search
taro
April 23, 2025
Programming
3.5k
11
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
VitestのIn-Source Testingが便利
Mita.ts #5
https://mitats.connpass.com/event/340678/
での登壇資料です。
taro
April 23, 2025
More Decks by taro
See All by taro
ローコードサービスの進化のためのモノレポ移行
taro28
2
620
ローコードSaaSのUXを向上させるためのTypeScript
taro28
2
1.4k
GraphQLをServer Componentsで使いたい
taro28
8
3.3k
Sequenceを理解する
taro28
1
360
propsのバケツリレー対策でGlobal_Stateを使うその前に
taro28
12
5.1k
状態ってなに?🙃
taro28
2
640
ReactのSuspenseを使った非同期処理のエラーハンドリング
taro28
9
7.5k
一口目から美味しいReactのスルメ本🦑
taro28
3
1.6k
T-falってすごい【社内LT】
taro28
1
410
Other Decks in Programming
See All in Programming
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
4
1.1k
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
270
『寄り添うラジオ』をAIで作る 体験価値から逆算した、会話しないUXと品質設計
theoriatec2024
3
180
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
320
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
230
Webの地図
yosuke_furukawa
PRO
6
4.5k
Jetpack Compose メカニズム
skydoves
0
420
新卒PdEのリアル
ryu1013
1
500
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
190
海上で動くGoサーバー: goroutineとchannelでさばく航行データストリーム
atsuki_seo
0
690
Family mrubyの進捗
kishima
1
120
自分的「カンファレンスの楽しみ方」
syumai
0
200
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1033
470k
Unsuck your backbone
ammeep
672
58k
Code Review Best Practice
trishagee
74
20k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
240
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
690
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
320
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
65k
Everyday Curiosity
cassininazir
0
320
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Transcript
VitestのIn-Source Testingが便利 2025-04-23 Mita.ts #5 taro(@taroro_tarotaro)
自己紹介 taro(@taroro_tarotaro) ベースマキナのソフトウェアエンジニア TypeScript, Go, GraphQL, etc.
目次 VitestのIn-Source Testingとは 嬉しいポイント テストのためのexportが不要になる プライベート関数にテストが書ける AIとの相性が良い まとめ
VitestのIn-Source Testingとは その名の通り、実装と同じファイルにテストが書けるやつ export function add(...args: number[]) { return args.reduce((a,
b) => a + b, 0); } // ソースコード内にテストが書ける if (import.meta.vitest) { it("add", () => { expect(add()).toBe(0); expect(add(1)).toBe(1); expect(add(1, 2, 3)).toBe(6); }); } https://vitest.dev/guide/in-source
VitestのIn-Source Testingとは わりと普通に動く 各種vitestの関数 Snapshotテスト モック
嬉しいポイント
テストのためのexportが不要になる テストのために関数をexportしてコメントを書く… // export for test export const swap =
<T>(data: T[], i: number, j: number) => { // data のi 番目とj 番目を入れ替える処理 // ... }; import { swap } from "./swap"; describe("swap", () => { it("basic", () => { expect(swap(["a", "b", "c"], 0, 1)).toEqual(["b", "a", "c"]); }); it("out of range", () => { // ...
テストのためのexportが不要になる テストのためのexportが不要! const swap = <T>(data: T[], i: number, j:
number) => { // data のi 番目とj 番目を入れ替える処理 // ... }; if (import.meta.vitest) { it("swap: basic", () => { expect(swap(["a", "b", "c"], 0, 1)).toEqual(["b", "a", "c"]); }); it("swap: out of range", () => { // ... } const useSwap = (initialData: Props) => {
テストのためのexportが不要になる テストのために定数をexport… // export for test export const userStatus =
{ // ... } as const; export const checkUserStatus = (user: User): UserStatus => { if (user.lastLoggedIn === null) { return userStatus.NEW; } if (user.lastLoggedIn > 30) { return userStatus.ACTIVE; } return userStatus.INACTIVE; };
テストのためのexportが不要になる テストのための定数や型などのexportが不要になる! const userStatus = { // ... } as
const; export const checkUserStatus = (user: User): UserStatus => { // ... }; if (import.meta.vitest) { describe("checkUserStatus", () => { test.each([ // テスト内で定数が使える! { lastLoggedIn: null, expected: userStatus.NEW }, // ...
プライベートな関数にテストが書ける const swap = <T>(data: T[], i: number, j: number)
=> { // data のi 番目とj 番目を入れ替える処理 }; const useSwap = (initialData: Props) => { // swap 関数を使って、データを入れ替える }; export const Swapper: FC<Props> = (initialData) => { const { data, swapData } = useSwap(initialData); // ... }; exportしている関数やコンポーネントに、まとめて全てのケースのテストを書く… describe("Swapper", () => { // ...
プライベートな関数にテストが書ける プライベートな関数に細かくテストを書ける! const swap = <T>(data: T[], i: number, j:
number) => { // data のi 番目とj 番目を入れ替える処理 // ... }; if (import.meta.vitest) { it("swap: basic", () => { expect(swap(["a", "b", "c"], 0, 1)).toEqual(["b", "a", "c"]); }); it("swap: out of range", () => { // ... } const useSwap = (initialData: Props) => {
プライベートな関数にテストが書ける 条件分岐で色んな関数を呼ぶ関数に全てのケースのテストを書く… export const dataConverter = (data: Data) => {
switch (data.type) { case "type1": return convertType1(data); case "type2": return convertType2(data); // ... } }; describe("dataConverter", () => { // 全てのtype のテストをdataConverter に対して書く… // ... });
プライベートな関数にテストが書ける プライベートな関数に細かくテストを書ける! const convertType1 = (data: Data) => { //
... if (import.meta.vitest) { it("convertType1", () => { // ... }); } // ... // この関数のテストは薄くできる! export const dataConverter = (data: Data) => { switch (data.type) { case "type1": return convertType1(data); // ...
AIとの相性が良い(気がする) AI Agentを使っていると、特に指示しなくてもテストに気づくことが多い(気がする) テストの内容も情報源として、実装をしてくれる(気がする) テストの修正を指示しなくていい(気がする)
AIとの相性が良い(気がする) AI Agentを使っていると、特に指示しなくてもテストに気づくことが多い(気がする) テストの内容も情報源として、実装をしてくれる(気がする) テストの修正を指示しなくていい(気がする) AIが理解しやすいってことは人間も理解しやすいはず…!
まとめ テストのためのexportが不要になる プライベートな関数にテストが書ける AIとの相性が良い(気がする)
ぜひIn-Source Testing使って みてください!