为什么我的 jest.mock 中的 Promise reject() 转到 then() 而不是 catch()? [英] Why is the Promise reject() in my jest.mock going to a then() rather than a catch()?
问题描述
我有两个文件,用于进行 API 调用的 getItemInfo.js
和分别是 Jest 测试文件的 getItemInfo.test.js
.
I have two files, getItemInfo.js
to make API calls and getItemInfo.test.js
which is the respective Jest test file.
在测试文件中,我模拟了由节点模块request-promise
触发的http调用.
On the test file, I am mocking the http calling triggered by node module request-promise
.
问题在第二个代码块上,由 ************
包围.基本上为什么 reject()
错误仍然会出现在第二个单元测试中的 then()
块中?
The question is on the second code block, surrounded by *********
's.
Basically why is the reject()
error still going to a then()
block in the second unit test?
// getItemInfo.js
const rp = require('request-promise');
const getItemInfo = (id) => {
const root = 'https://jsonplaceholder.typicode.com/posts/';
const requestOptions = {
uri: `${root}/${id}`,
method: 'GET',
json: true
}
return rp(requestOptions)
.then((result) => {
return result;
})
.catch((err) => {
return err;
});
};
module.exports = {
getItemInfo: getItemInfo
};
这是我的 Jest 单元测试文件.
And here is my Jest unit test file.
// getItemInfo.test.js
const ItemService = require('./getItemInfo');
jest.mock('request-promise', () => (options) => {
const id = Number.parseInt(options.uri.substring(options.uri.lastIndexOf('/') + 1));
return new Promise((resolve, reject) => {
if (id === 12) {
return resolve({
id: id,
userId: 1,
title: '',
body: ''
});
} else {
return reject('something went wrong'); // <-- HERE IS THE REJECT
}
})
});
describe('getItemInfo', () => {
it('can pass', done => {
const TEST_ID = 12
ItemService.getItemInfo(TEST_ID).then((result) => {
console.log('result:',result);
expect(result.id).toBe(TEST_ID);
expect(result.userId).toBeDefined();
expect(result.title).toBeDefined();
expect(result.body).toBeDefined();
done();
});
});
it('can fail', (done) => {
const TEST_ID = 13;
ItemService.getItemInfo(TEST_ID)
.catch((err) => {
// *************
// This "catch" block never runs
// even if the jest.mock above Promise.rejects
// Why is that???
// *************
console.log('catch():', err);
done();
})
.then((result) => {
// this block runs instead.
// and it returns "then: something went wrong"
console.log('then():', result);
done();
});
});
});
这是单元测试的输出.命令只是jest
.最后一行应该从 catch()
语句运行,而不是 then()
:
This is the unit test's output. The command is simply jest
. The last line should be run from the catch()
statement, not the then()
:
PASS ./getItemInfo.test.js
getItemInfo
✓ can pass (9ms)
✓ can fail (1ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 0.703s, estimated 1s
Ran all test suites.
----------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------------|----------|----------|----------|----------|----------------|
All files | 100 | 100 | 100 | 100 | |
getItemInfo.js | 100 | 100 | 100 | 100 | |
----------------|----------|----------|----------|----------|----------------|
console.log getItemInfo.test.js:25
result: { id: 12, userId: 1, title: '', body: '' }
console.log getItemInfo.test.js:48
then(): something went wrong
我做错了什么?
推荐答案
为什么我的 jest.mock 中的 Promise reject() 会转到 then() 而不是 catch()?
Why is the Promise reject() in my jest.mock going to a then() rather than a catch()?
您的 .catch()
处理程序正在将被拒绝的承诺转换为已解决的承诺,因此只会调用外部 .then()
处理程序.
Your .catch()
handler is converting a rejected promise into a resolved promise and thus only the outer .then()
handler is called.
当你像这样使用 .catch()
时:
When you use .catch()
like this:
.catch((err) => {
return err;
});
并且不要重新抛出错误或返回被拒绝的承诺,然后拒绝被视为已处理"并且返回的承诺变为已解决,而不是被拒绝.这就像使用 try/catch 一样.在 catch 处理程序中,除非再次抛出异常,否则会处理异常.
and do not rethrow the error or return a rejected promise, then the rejection is considered "handled" and the returned promise becomes resolved, not rejected. This is just like using a try/catch. In a catch handler, the exception is handled unless you throw it again.
你可以在这个简单的片段中看到:
You can see that in this simple snippet:
new Promise((resolve, reject) => {
reject(new Error("reject 1"));
}).catch(err => {
// returning a normal value here (anything other than a rejected promise)
// causes the promise chain to flip to resolved
return err;
}).then(val => {
console.log("Promise is now resolved, not rejected");
}).catch(err => {
console.log("Don't get here");
});
这两者中的任何一个都没有任何理由:
There's really no reason for either of these:
.then((result) => {
return result;
})
.catch((err) => {
return err;
});
您可以删除它们..then()
处理程序只是多余的代码,而 .catch()
处理程序吃掉拒绝并将其转换为已解决的承诺.
You can just remove both of them. The .then()
handler is just superfluous code and the .catch()
handler eats the rejection and turns it into a resolved promise.
如果您想保留 .catch()
处理程序,但允许拒绝向上传播,那么您需要重新抛出.
If you want to keep the .catch()
handler, but allow the rejection to propagate upwards, then you need to rethrow.
.catch((err) => {
console.log(err);
throw err; // keep promise rejected so reject will propagate upwards
});
这篇关于为什么我的 jest.mock 中的 Promise reject() 转到 then() 而不是 catch()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!