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
630
ローコードSaaSのUXを向上させるためのTypeScript
taro28
2
1.4k
GraphQLをServer Componentsで使いたい
taro28
8
3.3k
Sequenceを理解する
taro28
1
370
propsのバケツリレー対策でGlobal_Stateを使うその前に
taro28
12
5.1k
状態ってなに?🙃
taro28
2
650
ReactのSuspenseを使った非同期処理のエラーハンドリング
taro28
9
7.6k
一口目から美味しいReactのスルメ本🦑
taro28
3
1.6k
T-falってすごい【社内LT】
taro28
1
410
Other Decks in Programming
See All in Programming
Groovy as a human and AI friendly programming language
paulk
0
140
続・コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions-again
handlename
0
150
Kotlinの15年とServer-Side Kotlin
n_takehata
1
170
PumaからFalconに移行したRailsアプリケーションでの実践
falcon8823
0
300
dotNET_vulnclicker_intro.pdf
deka_morita
0
160
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
220
気持ちぃ〜角丸 Squircle
ryu1013
0
370
そのデザイントークン、Flutterでも同じ見た目になりますか?
nakamizu
0
140
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
190
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
12
7.7k
最近”作らせてる” ローカルAIアプリの話
akit37
0
520
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
3
1.3k
Featured
See All Featured
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
330
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
450
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
490
Marketing to machines
jonoalderson
1
5.9k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
560
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
How STYLIGHT went responsive
nonsquared
100
6.3k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
500
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Mind Mapping
helmedeiros
1
420
We Have a Design System, Now What?
morganepeng
55
8.4k
Building Adaptive Systems
keathley
44
3.3k
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使って みてください!