我正在尝试使用jest为axios post请求编写一个单元测试。这是我的实际功能-
exports.getAccessToken = function (urlToCall, scope, basicAuthToken) {
return new Promise(function (resolve, reject) {
let axios = require("axios");
let qs = require("qs");
let data = qs.stringify({
grant_type: "client_credentials",
scope: scope,
});
let config = {
method: "post",
url: urlToCall,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: "Basic " + basicAuthToken,
},
data: data,
};
axios(config)
.then(function (response) {
resolve(response.data);
})
.catch(function (error) {
console.log(
"error occurred while getting access token for the scope - ",
scope,
" and the error is - ",
error
);
});
});
};
这是我的单元测试代码-
const processUtils = require('../src/utils/process-utils')
const axios = require('axios')
jest.mock("axios")
describe("when getAccessToken API is successful", () => {
test('should return access token', async () => {
const expectedResponse = JSON.stringify({
"access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImMxZDY2OTF",
"issued_token_type": "token-type:access_token",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "consumer_profile:read:"
})
axios.post.mockResolvedValueOnce(() => Promise.resolve(expectedResponse))
// axios.post.mockImplementationOnce(() => Promise.resolve(expectedResponse));
let urlToCall = 'https://somehost.com/access_token/v1'
let scope = jest.fn
let basicAuthToken = jest.fn
const response = await processUtils.getAccessToken(urlToCall, scope, basicAuthToken)
expect(mockAxios.post).toHaveBeenCalledWith(urlToCall)
expect(response).toEqual(expectedResponse)
});
});
这是运行jest -
TypeError: Cannot read properties of undefined (reading 'then')
> axios(config)
.then(function (response) {
resolve(response.data);
})
https://i.stack.imgur.com/NZiVp.png我是节点和笑话的新手。有人能告诉我我在这里遗漏了什么吗?
2条答案
按热度按时间fkaflof61#
这个问题是由于你的代码没有直接调用
axios
模块示例上的post
函数,而是通过config
隐式调用,而你的测试模拟是直接调用axios.post
。有两种方法可以解决这个问题。1.将隐式
post
调用更改为显式调用:发件人:
收件人:
这将使用
axios.post.mockResolvedValueOnce
调用的结果。1.在测试套件设置中模拟axios post调用:
发件人:
结束日期
这将通过partialmock模拟隐式
post
调用,但是您将无法直接访问post
方法,因此您将无法侦听其调用。另一个小注意事项,当
then
被调用时,axios.post.mockResolvedValueOnce(() => Promise.resolve(expectedResponse))
会将一个函数传递到response
参数中。我认为您需要使用mockedAxios.post.mockResolvedValueOnce(expectedResponse)
。另外,expectedResponse
应该被 Package 在data
属性中,如下所示:oogrdqng2#
我试图张贴的解决方案,使它能帮助别人。解决方案是根据Ovidijus Parsiunas的回应找到的。实际功能:
我的单元测试用例: