Skip to content

Instantly share code, notes, and snippets.

@tonkotsuboy
Created January 17, 2019 12:24
Show Gist options
  • Save tonkotsuboy/9110451047bb78e51e0bcd541123da1d to your computer and use it in GitHub Desktop.
Save tonkotsuboy/9110451047bb78e51e0bcd541123da1d to your computer and use it in GitHub Desktop.
Hybrid Typesでモックを作る
// クライアント環境では次のように使っているライブラリのモックを作りたい
// const library = myLibrary();
// library.foo();
// Hybrid Typesでモックを作る
interface IMyLibrary {
(): void;
foo(): void;
bar(buz: number): string;
}
function getMockMyLibrary(): IMyLibrary {
const myLibrary = <IMyLibrary>() => {};
myLibrary.foo = () => {
console.warn("モック1");
};
myLibrary.bar = buz => {
return `${buz}円だよ`;
};
return myLibrary;
}
// 使ってみる
const serverSideLibrary = getMockMyLibrary();
// 結果:"モック1"
serverSideLibrary.foo();
// 結果"100000円だよ"
console.warn(serverSideLibrary.bar(100_000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment