Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError when mocking request with jest

I'm relatively new to node and am having issues trying to mock request using jest.

If my file to be tested has require('request'), and I try to run npm test, I get this error:

FAIL  __tests__/sum-test.js (0.291s)
● sum › it adds 1 + 2 to equal 3
  - TypeError: The super constructor to `inherits` must have a prototype.
        at Object.exports.inherits (util.js:756:11)
        at Object.<anonymous> (node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/private-key.js:44:6)
        at Object.<anonymous> (node_modules/request/node_modules/http-signature/node_modules/sshpk/lib/utils.js:16:18)

Here's my package.json, if that helps:

{
  "name": "jesttest",
  "version": "1.0.0",
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "jest-cli": "^12.0.2"
  },
  "dependencies": {
    "request": "^2.72.0"
  }
}

Anyone know why this might be happening?

like image 871
Manu Avatar asked Mar 05 '26 02:03

Manu


1 Answers

add jest.unmock('request') in your test file.

Jest will mock a fake require object when you require something in your file.In this situation, request is not the real request.So tell jext not mock request.

like image 89
Ganker Avatar answered Mar 07 '26 15:03

Ganker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!