I'm wanting to mock a method of a class returned from a module's exports, and I'm having a hard time setting up a Jest test to do so.
Here's what I have:
// src/jwks.js
const jwksClient = require('jwks-rsa')
const client = jwksClient({...})
module.exports = client
// src/auth.js
const jwksClient = require('./jwks')
function getKey (header, callback) {
jwksClient.getSigningKey(header.kid, function (err, key) {
console.log("inside getSigningKey")
var signingKey = key.publicKey || key.rsaPublicKey
callback(err, signingKey)
})
}
exports.getKey = getKey
// src/auth.test.js
const jwksClient = require('./jwks')
const { getKey } = require('./auth')
const mockGetSigningKey = jest.fn(() => {
console.log("mockGetSigningKey called")
return {
publickey: "the-key"
}
})
jest.mock('./jwks', () => {
return jest.fn().mockImplementation(() => {
return {
getSigningKey: mockGetSigningKey
}
})
})
test('test mocked behavior', () => {
getKey("somekey", (err, key) => {
console.log("received key: " + key)
})
}
I get the following error: TypeError: jwksClient.getSigningKey is not a function
2 |
3 | function getKey (header, callback) {
> 4 | jwksClient.getSigningKey(header.kid, function (err, key) {
| ^
5 | console.log("inside getSigningKey")
6 | var signingKey = key.publicKey || key.rsaPublicKey
7 | callback(err, signingKey)
How do I go about doing this properly? I've tried a lot of different variations and none of them are working for me.