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
スマート反転とウェブアクセシビリティ
camiha
0
210
From 6 People Classroom Meetup to 100 People Regional Conference / FOSS4G Hiroshima 2026
furukawayasuto
0
210
Heart of Swift Concurrency
koher
0
890
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
290
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
190
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
320
SONY CISC-NEWS NWS-1750 + NWB-225 フレームバッファの NetBSD/news68k ドライバ実装 / OSC2026Hiroshima
tsutsui
0
120
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
3k
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
280
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
340
Webの地図
yosuke_furukawa
PRO
6
4.6k
iOSDC2026登壇資料.pdf
riofujimon
0
170
Featured
See All Featured
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
320
Amusing Abliteration
ianozsvald
1
300
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
450
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
570
Heart Work Chapter 1 - Part 1
lfama
PRO
10
36k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Claude Code のすすめ
schroneko
67
230k
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使って みてください!