aneagoie/testing-exercise

Node-fetch 3.0.0 moved to from CommonJS to Module

Closed this issue · 1 comments

changes
I've been doing exercises and as usual there was a snag preventing me from progress. Jest didn't want to run with node-fetch import since as far as I understood it moved to ESM. It constantly threw an error on "import". Presumably it happened as a result of installing the latest node-fetch version which is 3.0.0

Any way. if you are wondering how I managed to dealt with the error, here is my package.json file. See the snapshot

Basically you need to switch type to "module" and
"test": "jest --watchAll"
change to
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watchAll"

Then go over all imports and export and change them to ECMA standard. For example, comment out the line
//module.exports = googleSearch;
and export the function like this
export const googleSearch = (searchInput, db) =>...

then open your script.test.js, and change the top line to:

//const googleSearch = require("./script");
import { googleSearch } from "./script";

Here's another thing you've got to install (apart from jest module)

npm install @jest/globals

and then import it in the file in which you mock fetch function
import { jest } from '@jest/globals'