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
CSC509 Lecture 07
javiergs
PRO
0
250
実践Claude Code:20の失敗から学ぶAIペアプログラミング
takedatakashi
18
9.1k
Domain-centric? Why Hexagonal, Onion, and Clean Architecture Are Answers to the Wrong Question
olivergierke
3
990
品質ワークショップをやってみた
nealle
0
650
「ちょっと古いから」って避けてた技術書、今だからこそ読もう
mottyzzz
12
7.2k
Vue 3.6 時代のリアクティビティ最前線 〜Vapor/alien-signals の実践とパフォーマンス最適化〜
hiranuma
2
230
AI時代に必須!状況言語化スキル / ai-context-verbalization
minodriven
2
200
社会人になっても趣味開発を続けたい! / traPavilion
mazrean
1
110
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
570
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
570
pnpm に provenance のダウングレード を検出する PR を出してみた
ryo_manba
1
160
Blazing Fast UI Development with Compose Hot Reload (droidcon London 2025)
zsmb
0
390
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.7k
How GitHub (no longer) Works
holman
315
140k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
658
61k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.1k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
Building a Scalable Design System with Sketch
lauravandoore
463
33k
jQuery: Nuts, Bolts and Bling
dougneiner
65
7.9k
Visualization
eitanlees
150
16k
Documentation Writing (for coders)
carmenintech
75
5.1k
The Cost Of JavaScript in 2023
addyosmani
55
9.1k
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_