Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TypeScriptとDocumentaion tests / Documentation tests with TypeScript

TypeScriptとDocumentaion tests / Documentation tests with TypeScript

TOMIKAWA Sotaro

January 12, 2024
Tweet

More Decks by TOMIKAWA Sotaro

Other Decks in Programming

Transcript

  1. だれ • ssssota • 冨川宗太郎 • {"x": "ssssotaro", "github": "ssssota"}

    • JSのビルド周りのツールチェーンに関心がある
  2. 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が必修と聞いているのでみなさんご存知。 コード内(外)のドキュメントに記述されたコードを実行することで ドキュメントの信頼性を担保する。
  3. 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したい!
  4. 仕組み(変換) /** * @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); }); }