我正在尝试编写单元测试,我需要在其中模拟来自认知服务的方法的响应 - CognitoIdentityServiceProvider
我有以下工作代码调用 adminInitiateAuth
操作
import * from AWS from 'aws-sdk'
const cognito = new AWS.CognitoIdentityServiceProvider();
const response = await cognito.adminInitiateAuth(expectedParams).promise();
// main functionality I want to test
并且我想要一个规范,我尝试将此服务作为先决条件进行模拟
const mockResponse = {
AuthenticationResult: {
AccessToken: 'expected-token'
}
}
jest.mock('aws-sdk', () => {
return {
CognitoIdentityServiceProvider: {
adminInitiateAuth: () => {
return mockResponse;
}
}
}
});
这会返回一个错误
AWS.CognitoIdentityServiceProvider is not a constructor
这怎么不能是构造函数? 你有什么想法如何模拟它吗?
答案 0 :(得分:0)
我想通了。可能对某人有用
jest.mock('aws-sdk', () => {
return {
CognitoIdentityServiceProvider: class {
adminInitiateAuth() {
return this;
}
promise() {
return Promise.resolve(mockResponse);
}
}
}
});