lundi 15 juillet 2019

Jest mocking: mockImplementation is not a function

I have a function that uses pg. I tried using the following setup to mock calls to the library:

const pg   = require('pg')

jest.genMockFromModule('pg')
jest.mock('pg')

const mockPgPool = {
  query: jest.fn()
}

pg.mockImplementation(() => mockPgPool)

But it fails on with

TypeError: pg.mockImplementation is not a function

// package.json
{
  "name": "conversion-tracker",
  "version": "0.0.1",
  "dependencies": {
    "babel-runtime": "^6.26.0",
    "cors": "^2.8.5",
    "jest": "^24.8.0",
    "jest-plugin-context": "^2.9.0",
    "jest-plugin-set": "^2.9.0",
    "pg": "^7.11.0"
  },
  "scripts": {
    "test": "jest",
    "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand"
  },
  "jest": {
    "verbose": true,
    "moduleDirectories": [
      "node_modules"
    ],
    "setupFiles": [
      "jest-plugin-context/setup",
      "jest-plugin-set/setup"
    ]
  }
}

Any insights on how to make this approach to mocking pg working?

Aucun commentaire:

Enregistrer un commentaire