Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 模拟从导出函数返回的类的方法_Javascript_Unit Testing_Mocking_Jestjs - Fatal编程技术网

Javascript 模拟从导出函数返回的类的方法

Javascript 模拟从导出函数返回的类的方法,javascript,unit-testing,mocking,jestjs,Javascript,Unit Testing,Mocking,Jestjs,我想模拟从模块导出返回的类的方法,但我很难设置Jest测试来实现这一点 以下是我所拥有的: // src/jwks.js const jwksClient = require('jwks-rsa') const client = jwksClient({...}) module.exports = client 我得到以下错误: TypeError:jwksClient.getSigningKey不是函数 2 | 3 | function getKey (header, callb

我想模拟从模块导出返回的类的方法,但我很难设置Jest测试来实现这一点

以下是我所拥有的:

// src/jwks.js

const jwksClient = require('jwks-rsa')
const client = jwksClient({...})
module.exports = client
我得到以下错误: TypeError:jwksClient.getSigningKey不是函数

  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)
我该如何正确地做这件事?我尝试了很多不同的变体,但没有一个适合我。

您没有正确地模拟/jwks模块。以下是解决方案:

auth.js:

const jwksClient=require'/jwks'; 函数getKeyheader,回调{ jwksClient.getSigningKeyheader.kid,函数错误,键{ console.log'insidegetSigningKey'; var signingKey=key.publicKey | | key.rsappublickey; callbackerr,signingKey; }; } exports.getKey=getKey; jwks.js:

auth.test.js:

const{getKey}=require'/auth'; const jwksClient=require'/jwks'; jest.mock'/jwks',=>{ 返回{getSigningKey:jest.fn}; }; 描述'62544352',=>{ 它“应该通过”,=>{ jwksClient.getSigningKey.mockImplementationOncekey,回调=>{ const key={publicKey:'publicKey'}; callbacknull,key; }; const mCallback=jest.fn; getKey{kid:'someKey'},mCallback; expectjwksClient.getSigningKey.toBeCalledWith'someKey',expect.anyFunction; expectmCallback.toBeCalledWithnull,“publicKey”; }; }; 单元测试结果和覆盖率报告:

通过stackoverflow/62544352/auth.test.js 10.232s 62544352 ✓ 应该超过28毫秒 console.log 内部getSigningKey 在stackoverflow/62544352/auth.js:5:13 -----|-----|-----|-----|-----|---------- 文件|%Stmts |%Branch |%Funcs |%Lines |未覆盖的行 -----|-----|-----|-----|-----|---------- 所有文件| 100 | 50 | 100 | 100 | auth.js | 100 | 50 | 100 | 100 | 6 -----|-----|-----|-----|-----|---------- 测试套件:1个通过,共1个 测试:1项通过,共1项 快照:共0个 时间:11点65分
啊,接得好!工作得很有魅力。非常感谢你。
// 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)
    })
}
  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)
// whatever