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.7k
jestのspyOnでmockする際に詰まった話
riku0202
July 20, 2022
Tweet
Share
More Decks by riku0202
See All by riku0202
ReactでWebsocket通信してみた.pdf
riku0202
0
100
Other Decks in Programming
See All in Programming
Webの外へ飛び出せ NativePHPが切り拓くPHPの未来
takuyakatsusa
2
380
Cursor AI Agentと伴走する アプリケーションの高速リプレイス
daisuketakeda
1
130
NPOでのDevinの活用
codeforeveryone
0
360
Select API from Kotlin Coroutine
jmatsu
1
190
Enterprise Web App. Development (2): Version Control Tool Training Ver. 5.1
knakagawa
1
120
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
430
Composerが「依存解決」のためにどんな工夫をしているか #phpcon
o0h
PRO
1
230
CursorはMCPを使った方が良いぞ
taigakono
1
180
A2A プロトコルを試してみる
azukiazusa1
2
1.2k
Result型で“失敗”を型にするPHPコードの書き方
kajitack
4
420
なんとなくわかった気になるブロックテーマ入門/contents.nagoya 2025 6.28
chiilog
1
220
XP, Testing and ninja testing
m_seki
3
200
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
694
190k
Making Projects Easy
brettharned
116
6.3k
The Invisible Side of Design
smashingmag
299
51k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.8k
How to train your dragon (web standard)
notwaldorf
93
6.1k
It's Worth the Effort
3n
185
28k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
700
Building Flexible Design Systems
yeseniaperezcruz
328
39k
What's in a price? How to price your products and services
michaelherold
246
12k
Site-Speed That Sticks
csswizardry
10
660
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
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_