Javascript 如何将正确的响应数据插入Jest?
基于此,我试图在类似的函数上实现JestJS 问题: 问题在于如何在Javascript 如何将正确的响应数据插入Jest?,javascript,node.js,ecmascript-6,jestjs,Javascript,Node.js,Ecmascript 6,Jestjs,基于此,我试图在类似的函数上实现JestJS 问题: 问题在于如何在jest.mock()中插入模拟响应数据。下面我描述并包括了我使用过的所有文件 有人能找出我做错了什么吗 这里有一个实时版本可以试用 背景 首先,我通过 const getStatusCode = require('./getStatusCode'); getStatusCode({ url: 'https://google.com', statusCode: 200, timeout: 1000, maxRe
jest.mock()
中插入模拟响应数据。下面我描述并包括了我使用过的所有文件
有人能找出我做错了什么吗
这里有一个实时版本可以试用
背景
首先,我通过
const getStatusCode = require('./getStatusCode');
getStatusCode({
url: 'https://google.com',
statusCode: 200,
timeout: 1000,
maxRedirects: 0
});
getStatusCode.js
const axios = require('axios');
const qs = require('qs');
module.exports = async (options) => {
options = options || {};
options.url = options.url || {};
options.statusCode = options.statusCode || 0;
options.timeout = options.timeout || 1000;
options.maxRedirects = options.maxRedirects || 0;
try {
const response = await axios.get(options.url, {
timeout: options.timeout,
maxRedirects: options.maxRedirects,
// make all http status codes a success
validateStatus: function (status) {
return true;
}
});
console.log(response); // here just to get the response data for Jest
return (response.status === options.statusCode) ? 1 : 0;
} catch (error) {
return -1;
}
};
const axios = require('axios');
const getStatusCode = require('./getStatusCode');
// whenever Axios is called from getStatusCode() it won't make a network
// connection, but return the following instead
jest.mock("axios", () => {
return {
get: jest.fn().mockResolvedValue({
data:
{
status: 301,
statusText: 'Moved Permanently',
headers: {
location: 'https://www.google.com/',
'content-type': 'text/html; charset=UTF-8',
date: 'Thu, 12 Nov 2020 10:09:41 GMT',
expires: 'Sat, 12 Dec 2020 10:09:41 GMT',
'cache-control': 'public, max-age=2592000',
server: 'gws',
'content-length': '220',
'x-xss-protection': '0',
'x-frame-options': 'SAMEORIGIN',
'alt-svc': 'h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
connection: 'close'
}
}
})
};
});
describe("test getStatusCode ", () => {
// pretend to get data from the network connection
let response;
beforeAll(async () => {
response = await getStatusCode({
url: 'https://google.com',
statusCode: 200,
timeout: 1000,
maxRedirects: 0
});
});
// compare returned data with expected
it("fetches raw response but we are only interested in the statuscode", async () => {
// ensure the mocked Axios function has been called
expect(axios.get).toHaveBeenCalled();
expect(response.status).toBeGreaterThan(0);
});
});
这会产生非常大的输出:
{
status: 301,
statusText: 'Moved Permanently',
headers: {
location: 'https://www.google.com/',
'content-type': 'text/html; charset=UTF-8',
date: 'Thu, 12 Nov 2020 10:48:21 GMT',
expires: 'Sat, 12 Dec 2020 10:48:21 GMT',
'cache-control': 'public, max-age=2592000',
server: 'gws',
'content-length': '220',
'x-xss-protection': '0',
'x-frame-options': 'SAMEORIGIN',
'alt-svc': 'h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
connection: 'close'
},
config: {
...
因为我只对status
的值感兴趣,所以我只需将第一部分复制/粘贴到我的getStatusCode.test.js
文件的data
getStatusCode.test.js
const axios = require('axios');
const qs = require('qs');
module.exports = async (options) => {
options = options || {};
options.url = options.url || {};
options.statusCode = options.statusCode || 0;
options.timeout = options.timeout || 1000;
options.maxRedirects = options.maxRedirects || 0;
try {
const response = await axios.get(options.url, {
timeout: options.timeout,
maxRedirects: options.maxRedirects,
// make all http status codes a success
validateStatus: function (status) {
return true;
}
});
console.log(response); // here just to get the response data for Jest
return (response.status === options.statusCode) ? 1 : 0;
} catch (error) {
return -1;
}
};
const axios = require('axios');
const getStatusCode = require('./getStatusCode');
// whenever Axios is called from getStatusCode() it won't make a network
// connection, but return the following instead
jest.mock("axios", () => {
return {
get: jest.fn().mockResolvedValue({
data:
{
status: 301,
statusText: 'Moved Permanently',
headers: {
location: 'https://www.google.com/',
'content-type': 'text/html; charset=UTF-8',
date: 'Thu, 12 Nov 2020 10:09:41 GMT',
expires: 'Sat, 12 Dec 2020 10:09:41 GMT',
'cache-control': 'public, max-age=2592000',
server: 'gws',
'content-length': '220',
'x-xss-protection': '0',
'x-frame-options': 'SAMEORIGIN',
'alt-svc': 'h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
connection: 'close'
}
}
})
};
});
describe("test getStatusCode ", () => {
// pretend to get data from the network connection
let response;
beforeAll(async () => {
response = await getStatusCode({
url: 'https://google.com',
statusCode: 200,
timeout: 1000,
maxRedirects: 0
});
});
// compare returned data with expected
it("fetches raw response but we are only interested in the statuscode", async () => {
// ensure the mocked Axios function has been called
expect(axios.get).toHaveBeenCalled();
expect(response.status).toBeGreaterThan(0);
});
});
运行npm运行测试给我
FAIL ./getStatusCode.test.js
● Console
console.log
{
data: {
status: 301,
statusText: 'Moved Permanently',
headers: {
location: 'https://www.google.com/',
'content-type': 'text/html; charset=UTF-8',
date: 'Thu, 12 Nov 2020 10:09:41 GMT',
expires: 'Sat, 12 Dec 2020 10:09:41 GMT',
'cache-control': 'public, max-age=2592000',
server: 'gws',
'content-length': '220',
'x-xss-protection': '0',
'x-frame-options': 'SAMEORIGIN',
'alt-svc': 'h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
connection: 'close'
}
}
}
后来
● test getStatusCode › fetches raw response but we are only interested in the statuscode
expect(received).toBeGreaterThan(expected)
Matcher error: received value must be a number or bigint
Received has value: undefined
很明显,我没有从axios.get()
正确复制响应
我做错了什么?您的函数getStatusCode
检查响应并返回-1或0
因此,您不能期望它具有属性status
你可以检查它是否为零
expect(响应)。toBeGreaterThanOrEqual(0)
编辑:如果要断言模拟函数已返回使用具有数据状态的对象解析的承诺,可以使用jest模拟函数,如下所示:
expect(wait axios.get.mock.results[0].value).toHaveProperty('data.status',301)
//其中axios.get.mock.results是一个数组,其中包含每个调用的结果
//而“[0].value”是第一次调用的结果
//这将是对promise对象的引用,因此是wait
但是通常您希望测试您的实现,而不是模拟函数,因为您的实现会根据选项检查响应.status
。statusCode
如果您有两个场景,最好是一个不匹配,另一个匹配