Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
TypeScriptとDocumentaion tests / Documentation t...
Search
TOMIKAWA Sotaro
January 12, 2024
Programming
4.2k
8
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
TypeScriptとDocumentaion tests / Documentation tests with TypeScript
TOMIKAWA Sotaro
January 12, 2024
More Decks by TOMIKAWA Sotaro
See All by TOMIKAWA Sotaro
ReactとSvelteのその先、Ripple-TS / Beyond React and Svelte: Ripple-TS
ssssota
3
2.5k
Atomics APIを知る / Understanding Atomics API
ssssota
2
1.4k
なんでRustの環境構築してないのにRust製のツールが動くの? / Why Do Rust-Based Tools Run Without a Rust Environment?
ssssota
15
56k
Web技術を最大限活用してRAW画像を現像する / Developing RAW Images on the Web
ssssota
2
3.3k
漸進。
ssssota
0
3.7k
Preact、HooksとSignalsの両立 / Preact: Harmonizing Hooks and Signals
ssssota
1
3.8k
useSyncExternalStoreを使いまくる
ssssota
6
7k
React CompilerとFine Grained Reactivityと宣言的UIのこれから / The next chapter of declarative UI
ssssota
8
6.2k
新しいAPI createRawSnippet触ってみた / What is the createRawSnippet?
ssssota
2
340
Other Decks in Programming
See All in Programming
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
12
19k
Android CLI
fornewid
0
220
バグを直したら useEffect が消えた
colorful12
2
570
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
710
運用ダッシュボードの設計を誰も教えてくれないのだけどみなさんどうしてるんですか? - チームに監視するという文化を根付かせるための第一歩を踏みたい -
satoshi256kbyte
1
130
How I Won Prize Money at a Hackathon Using Codex and Symphony Alpha
yasei_no_otoko
0
100
メールのエイリアス機能を履き違えない
isshinfunada
0
240
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
510
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
150
170k Jobs a Day on GKE: Scaling Mercari's CI Platform - and What's Next for AI-Native Development
junyaokabe
0
110
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
150
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
560
Featured
See All Featured
Design in an AI World
tapps
1
280
It's Worth the Effort
3n
188
29k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
270
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
440
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
470
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
280
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
710
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Between Models and Reality
mayunak
4
390
Transcript
TypeScriptとDocumentation tests ssssota
だれ • ssssota • 冨川宗太郎 • {"x": "ssssotaro", "github": "ssssota"}
• JSのビルド周りのツールチェーンに関心がある
Documentation testsとは /// ``` /// let result = doccomments::add(2, 3);
/// assert_eq!(result, 5); /// ``` pub fn add(a: i32, b: i32) -> i32 { a + b } ↑はRustのドキュメントに書いてあるもの 最近のJSエンジニアはRustが必修と聞いているのでみなさんご存知。 コード内(外)のドキュメントに記述されたコードを実行することで ドキュメントの信頼性を担保する。
JSのDocumentation tests • Supabase CEO作 https://github.com/kiwicopple/doctest-js • azu氏 作 https://github.com/azu/power-doctest
• https://github.com/davidchambers/doctest いずれもJavaScriptにサポートが限定される。 (3つ目はなぜかCoffeeScriptサポートがある) TypeScriptのファイルでもDocumentation testsしたい!
作った https://github.com/ssssota/doc-vitest Vitest向けのプラグインとして作ることでTypeScriptのトランスパイルは Vite(esbuild)に、テスト周りのCLI,assertionはVitestに全乗っかり。 /** * @import.meta.vitest * ```ts *
expect(add(1, 2)).toBe(3); * assert(add(3, 4) === 7); * ``` */ export function add(a: number, b: number) { return a + b; }
仕組み(変換) /** * @import.meta.vitest * ```ts:1+2=3 * expect(add(1, 2)).toBe(3); *
``` */ export function add(a: number, b: number) { return a + b; } /** * @import.meta.vitest * ```ts:1+2=3 * expect(add(1, 2)).toBe(3); * ``` */ export function add(a: number, b: number) { return a + b; } if (import.meta.vitest) { const {assert,...} = import.meta.vitest; import.meta.vitest.test("1+2=3", async()=>{ expect(add(1, 2)).toBe(3); }); }
仕組み(Vitest) Vitest用プラグインと書いたが正確にはVite用プラグイン。Vitestからの実行時に のみ変換が有効化される VitestのIn-Source Testingと呼ばれる機能で追記されたテストが実行される In-source Testingでは変換前の時点で import.meta.vitest の記述があるファイル だけを実行対象にするので
@import.meta.vitest という記述が必要だった テスト対象を抜き出せばOKなので、Markdown内のコードブロックもサポートし ている
おわりに JavaScriptの開発では至る所でコード変換されている。 (トランスパイル、バンドル、ミニファイ、Svelte/Vueコンポーネント、、、) webpackやViteのプラグインはUnpluginというライブラリで結構簡単に作れる。 みんなもコード変換しまくろう!