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
650
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
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
20
21k
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
7
1.8k
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
230
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
620
技術的負債の返済は、AI時代の複利で効く投資 — 経営としての意思決定とその遂行
curekoshimizu
1
2.2k
Embedded Swiftで作る自作USBデバイス によるiOSデバイスの自動テスト / iOSDC Japan 2026 glassfiber
glassfiber
0
290
phperだってパンチカードでコーディングしたい
o0h
PRO
0
190
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
180
モジュールの視点からSwiftを読み解く #iosdc
s_shimotori
0
340
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
300
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
170
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
340
Featured
See All Featured
4 Signs Your Business is Dying
shpigford
187
23k
Embracing the Ebb and Flow
colly
88
5.2k
A Modern Web Designer's Workflow
chriscoyier
699
190k
Side Projects
sachag
456
43k
Writing Fast Ruby
sferik
630
63k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
930
Believing is Seeing
oripsolob
1
240
What's in a price? How to price your products and services
michaelherold
247
13k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
490
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
510
Mobile First: as difficult as doing things right
swwweet
225
10k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
910
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使って みてください!