下面可以在 https://repl.it/@SandraSchlichti/jest-playground-3#getStatusCode.test.js 现场试用
最初下面的测试有 status: 200
,但我试图将它更改为 301
以查看我是否理解它是如何工作的。
describe('when the server returns 200', () => {
let result;
//??? The desired purpose of this test is to have a mis-match between expected and
//??? returned status code so getStatusCode() returns 0.
//??? Trying to figure out how this test works.
//??? Here I have changed status to 301 and expected the test to fail, but it didn't
//??? From my understanding mockResponseData contains status 301, and status below
//??? should override that? But that doesn't seam to be the case for some reason.
beforeAll(async () => {
axios.get.mockResolvedValue({
status: 301,
data: mockResponseData
});
result = await getStatusCode();
});
it('should return 0', () => {
expect(result).toEqual(0)
})
});
问题
当我将其更改为 status: 301
时,我曾预计测试会失败,因为 getStatusCode()
应该返回 1
状态代码匹配。
- 为什么测试没有失败?
- 是否可以修改测试以使其更清楚预期和返回的状态代码不匹配?
status
和data
的顺序由于某种原因并不重要。这是为什么?status: 301
不是应该覆盖mockResponseData
中的status
吗?- 如果我在
mockResponseData.json
中将status
更改为9999
,那么所有测试都不会受到影响。我感觉mockResponseData.json
中的内容根本没有被使用?
这些是在本地试用所需的文件:
mockedResponseData.json
{
"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"
}
}
getStatusCode.test.js
const axios = require('axios');
const getStatusCode = require('./getStatusCode');
const mockResponseData = require('./mockResponseData.json');
jest.mock("axios");
describe("getStatusCode ", () => {
describe('when the server returns status code matching options.statusCode', () => {
let result;
beforeAll(async () => {
axios.get.mockResolvedValue({
status: 200,
data: mockResponseData
})
result = await getStatusCode({
statusCode: 200
})
});
it('should return 1', () => {
expect(result).toEqual(1)
})
});
describe('when the server returns 200', () => {
let result;
//??? The desired purpose of this test is to have a mis-match between expected and returned status code,
//??? getStatusCode() returns 0.
//??? Trying to figure out how this test works.
//??? Here I have changed status to 301 and expected the test to fail, but it didn't
//??? From my understanding mockResponseData contains status 301, and status below should override that?
//??? But that doesn't seam to be the case for some reason.
beforeAll(async () => {
axios.get.mockResolvedValue({
status: 301,
data: mockResponseData
});
result = await getStatusCode();
});
it('should return 0', () => {
expect(result).toEqual(0)
})
});
describe("when expected and returned status code doesn't match", () => {
let result;
beforeAll(async () => {
// mockRejected value returns rejected promise
// which will be handled by the try/catch
axios.get.mockRejectedValue({
status: 200,
data: mockResponseData
})
result = await getStatusCode();
})
it('should return -1', () => {
expect(result).toEqual(-1)
})
});
});
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;
}
});
return (response.status === options.statusCode) ? 1 : 0;
} catch (error) {
return -1;
}
};
最佳答案
当您不向 result = await getStatusCode();
传递任何参数时,它将默认为零。如果你不想要那样,那就去做
describe('when the server returns 200', () => {
let result;
beforeAll(async () => {
axios.get.mockResolvedValue({
status: 301,
data: mockResponseData
});
result = await getStatusCode({
statusCode: 200 // <-----------
});
});
it('should return 0', () => {
expect(result).toEqual(0)
})
});
关于javascript - 为什么这个 Jest 测试没有失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64855209/