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
jestのspyOnでmockする際に詰まった話
Search
riku0202
July 20, 2022
Programming
0
1.8k
jestのspyOnでmockする際に詰まった話
riku0202
July 20, 2022
Tweet
Share
More Decks by riku0202
See All by riku0202
ReactでWebsocket通信してみた.pdf
riku0202
0
120
Other Decks in Programming
See All in Programming
JETLS.jl ─ A New Language Server for Julia
abap34
2
460
生成AIを利用するだけでなく、投資できる組織へ
pospome
2
410
大規模Cloud Native環境におけるFalcoの運用
owlinux1000
0
200
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
460
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
160
Giselleで作るAI QAアシスタント 〜 Pull Requestレビューに継続的QAを
codenote
0
300
ZJIT: The Ruby 4 JIT Compiler / Ruby Release 30th Anniversary Party
k0kubun
1
280
AIエンジニアリングのご紹介 / Introduction to AI Engineering
rkaga
8
3.4k
Cap'n Webについて
yusukebe
0
150
脳の「省エネモード」をデバッグする ~System 1(直感)と System 2(論理)の切り替え~
panda728
PRO
0
120
PC-6001でPSG曲を鳴らすまでを全部NetBSD上の Makefile に押し込んでみた / osc2025hiroshima
tsutsui
0
190
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 2
philipschwarz
PRO
0
120
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Art, The Web, and Tiny UX
lynnandtonic
304
21k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
980
The World Runs on Bad Software
bkeepers
PRO
72
12k
Applied NLP in the Age of Generative AI
inesmontani
PRO
3
2k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
170
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
38
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
82
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
1
210
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
196
70k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
0
46
Transcript
jestのspyOnでmockする際に 詰まった話
自己紹介
Riku Shimojima 株式会社HRBrain フロントエンド(React,TS) バックエンド(Go) エンジニア歴1年半 twitter: @Riku_0202_
ある日、CustomHooksのテストを書いていると、、、
なぜかテストが通らない、、、
なぜかテストが通らない、、、 例
なぜかテストが通らない、、、 例
refを渡したElementの 高さを取得し保持する CustomHook 例
例 てすと
useRefのモックが上手くいってないっぽい??
None
調べてみた👀
jestのissueにめっちゃ書いてありました
問題はこのimport方法にありました import * as React from "react"; import React from
"react";
トランスパイル import { useRef } from "react" import * as
React from "react" useRef React.useRef import React from "react" React.useRef
_react.useRef _react.useRef トランスパイル import { useRef } from "react" import
* as React from "react" useRef React.useRef import React from "react" React.useRef _react["default"].useRef
↑は _react["default"] をmockしている jest.spyOn(React, "useRef");
↑は _react["default"] をmockしている jest.spyOn(React, "useRef"); 故に単一importしたuseRefは モックされない _react.useRef; _react["default"].useRef;
None
成功!
[ 参考リンク ] Mock/Spy exported functions within a single module
in Jest jest.spyOnで差し替わってなかった Jest spyOn() calls the actual function instead of the mocked
フロントエンドエンジニアを積極採用募集中です
フロントエンドエンジニアを積極採用募集中です @Riku_0202_
ありがとうございました!! @Riku_0202_