blog.unresolved.xyzblog.unresolved.xyz

Jest実行時にReferenceError: fetch is not definedが出る問題

Sat Jul 04 2020
  • TIL
  • Jest
  • JavaScript

知りませんでした。

fetch is not available in Node, which is where Jest is running your tests. Is it an experimental browser technology.

You will need to polyfill the behaviour if you want to make actual http calls, or mock fetch to simulate network requests.

https://github.com/facebook/jest/issues/2071#issuecomment-259709487

yarn add -D whatwg-fetch して

jest.setup.js

1const fetchPolifill = require('whatwg-fetch') 2 3global.fetch = fetchPolifill.fetch 4global.Request = fetchPolifill.Request 5global.Headers = fetchPolifill.Headers 6global.Response = fetchPolifill.Response