1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Next.js】JestのテストでReferenceError: Response is not definedのエラーの解決の仕方について

Posted at

はじめに

jestのテストでResponseを使おうとした際に下記のエラーが表示されました。

ReferenceError: Response is not defined

調べるとNode.jsにはResponseクラスが存在しないと認識しているため、表示されるエラーのようでした。

global.Response = Response;

解決方法

node-fetchをインストールする

npm install node-fetch@2
const { Response } = require("node-fetch");

global.Response = Response;

参考

おわりに

私の環境ではv3ではうまくいかなかったため、v2で対応しました。
既にNode.jsでもfetchをはじめ、Responseクラスも使えるようになっているようですが、Jestのテストではエラーが出ることがあるみたいです。


JISOUのメンバー募集中!

プログラミングコーチングJISOUでは、新たなメンバーを募集しています。
日本一のアウトプットコミュニティでキャリアアップしませんか?
興味のある方は、ぜひホームページをのぞいてみてください!
▼▼▼

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?