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でDIしてみた
Search
Ryohei Hisamitsu
January 16, 2021
Technology
0
520
TypeScriptでDIしてみた
Ryohei Hisamitsu
January 16, 2021
Tweet
Share
More Decks by Ryohei Hisamitsu
See All by Ryohei Hisamitsu
re:Invent参加フィードバック会
hisami
0
93
GoのアプリをEKS(Fargate)にデプロイしてみた話
hisami
0
250
Greengrassを使ったIoTのアーキテクチャ
hisami
0
170
ECS Execを使ってFargateにログインしてみた
hisami
1
390
Other Decks in Technology
See All in Technology
めちゃくちゃ開発するQAエンジニアになって感じたメリットとこれからの課題感
ryuhei0000yamamoto
0
120
決済サービスを支えるElastic Cloud - Elastic Cloudの導入と推進、決済サービスのObservability
suzukij
2
660
Scrumは歪む — 組織設計の原理原則
dashi
0
200
Everything Claude Code を眺める
oikon48
11
7.2k
わからなくて良いなら、わからなきゃだめなの?
kotaoue
1
370
猫でもわかるKiro CLI(AI 駆動開発への道編)
kentapapa
0
260
会社紹介資料 / Sansan Company Profile
sansan33
PRO
16
410k
【Oracle Cloud ウェビナー】【入門編】はじめてのOracle AI Data Platform - AIのためのデータ準備&自社用AIエージェントをワンストップで実現
oracle4engineer
PRO
1
150
It’s “Time” to use Temporal
sajikix
3
210
ReactのdangerouslySetInnerHTMLは“dangerously”だから危険 / Security.any #09 卒業したいセキュリティLT
flatt_security
0
310
(Test) ai-meetup slide creation
oikon48
3
440
NewSQL_ ストレージ分離と分散合意を用いたスケーラブルアーキテクチャ
hacomono
PRO
4
390
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
120
Exploring anti-patterns in Rails
aemeredith
2
290
Typedesign – Prime Four
hannesfritz
42
3k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
130
Building Flexible Design Systems
yeseniaperezcruz
330
40k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
440
Designing for humans not robots
tammielis
254
26k
HDC tutorial
michielstock
1
550
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
250
Deep Space Network (abreviated)
tonyrice
0
92
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
85
Transcript
で してみた NCDC株式会社 久光遼平
DI(Dependency Injection)とは? • 依存性(Dependency)の注入(Injection) • もう少しわかりやすく あるクラスが依存している要素をクラスの外側から注入すること ClassA { private
classB constructor( ClassBInterface classB ) { this.classB = classB; } } ClassA { private classB constructor() { this.classB = new ClassB(); } }
何が嬉しいの? • 単体テストがしやすくなる! class Service { private repository; constructor() {
this.repository = new Repository(); } find() { // ... this.repository.find(); return; } } よくあるテストしにくい例 テストしたいのはService層のロジック Repository(DB)のテストも含まれてしまう ・テスト失敗した時の切り分けが面倒 ・実行時間がかかる DI→テスト側からモックを注入できる!
実際のアプリではどうやって注入するの? • DIコンテナという便利な仕組みがある ◦ インスタンスの生成、注入を一括で行ってくれる
TypeScriptのDIライブラリ
②注入する側 ①注入される側 ③DIコンテナ 実装例 ④express
まとめ • DIすればテストしやすい作りにできる • DIの仕組みを作るにはInversifyJSを使えば楽 • あとはテストの時にモックを注入するだけ! • 追記:Jestのモック機能を使えば、DIしなくてもテストは書けそう ◦
DIのメリット→より簡単に自由にテスト書ける