admin管理员组文章数量:1323342
How do i test if the function createTempUsageStatisticsTable(athenaExpress)
throws an error and also to test if createTempUsageStatisticsTable(athenaExpress)
throws error because the function athenaExpress.query(athenaQueryParam)
throws an error (Using Jest) ? (Assume filename to be index.js
)
async function createTempUsageStatisticsTable(athenaExpress) {
let athenaQueryParam = {
sql: getSqlQueries.CREATE_DEVICE_USAGE_STATS_TEMP_TABLE_QUERY,
db: "testdb"
};
await athenaExpress.query(athenaQueryParam);
}
exportFunctions={createTempUsageStatisticsTable:createTempUsageStatisticsTable}
module.exports=exportFunctions
Now,I want to write a test to test if createTempUsageStatisticsTable(athenaExpress)
throws an error when athenaExpress.query(athenaQueryParam)
throws an error or rejects a promise in a mock implementation whichever is suitable or works,so i did
const confError = new Error('network error');
athenaExpress.query = jest.fn().mockImplementationOnce(() => {
throw new Error(confError); // tried this
promise.reject(confError);
})
index.configureAthenaExpress();
expect(index.configureAthenaExpress).toThrow();
However tests do not seem to pass please help
Thanks to James i got it working,However i slightly tweaked his code as i was getting some error due to strict equal,The code is as follows:
test("createTempUsageStatisticsTable throws an exception if
athenaExpress.query fails()", async () => {
const creaError=new Error("network error")
athenaExpress=configureAthenaExpress();
athenaExpress.query.mockRejectedValueOnce(creaError);
await expect(createTempUsageStatisticsTable(athenaExpress)).rejects.toBe(creaError);
});
How do i test if the function createTempUsageStatisticsTable(athenaExpress)
throws an error and also to test if createTempUsageStatisticsTable(athenaExpress)
throws error because the function athenaExpress.query(athenaQueryParam)
throws an error (Using Jest) ? (Assume filename to be index.js
)
async function createTempUsageStatisticsTable(athenaExpress) {
let athenaQueryParam = {
sql: getSqlQueries.CREATE_DEVICE_USAGE_STATS_TEMP_TABLE_QUERY,
db: "testdb"
};
await athenaExpress.query(athenaQueryParam);
}
exportFunctions={createTempUsageStatisticsTable:createTempUsageStatisticsTable}
module.exports=exportFunctions
Now,I want to write a test to test if createTempUsageStatisticsTable(athenaExpress)
throws an error when athenaExpress.query(athenaQueryParam)
throws an error or rejects a promise in a mock implementation whichever is suitable or works,so i did
const confError = new Error('network error');
athenaExpress.query = jest.fn().mockImplementationOnce(() => {
throw new Error(confError); // tried this
promise.reject(confError);
})
index.configureAthenaExpress();
expect(index.configureAthenaExpress).toThrow();
However tests do not seem to pass please help
Thanks to James i got it working,However i slightly tweaked his code as i was getting some error due to strict equal,The code is as follows:
test("createTempUsageStatisticsTable throws an exception if
athenaExpress.query fails()", async () => {
const creaError=new Error("network error")
athenaExpress=configureAthenaExpress();
athenaExpress.query.mockRejectedValueOnce(creaError);
await expect(createTempUsageStatisticsTable(athenaExpress)).rejects.toBe(creaError);
});
Share
Improve this question
edited Jan 25, 2020 at 3:49
Ali Khan
asked Jan 24, 2020 at 18:22
Ali KhanAli Khan
1342 gold badges4 silver badges25 bronze badges
1
-
1
use
try{......}catch(e){......}
– Pranav C Balan Commented Jan 24, 2020 at 18:24
1 Answer
Reset to default 4Depending on how athenaExpress
is exported, you can mock query
to throw and then test for the existence of said by leveraging rejects
e.g.
const createTempUsageStatisticsTable = require("./createTempUsageStatisticsTable");
const athenaExpress = require("./athenaExpress");
jest.mock("./athenaExpress");
test("createTempUsageStatisticsTable throws if query fails", async () => {
athenaExpress.query.mockRejectedValueOnce(new Error("network error"));
await expect(createTempUsageStatisticsTable(athenaExpress)).rejects.toMatchObject({ message: "network error" });
});
本文标签: javascriptHow to throw error in an async function in jestStack Overflow
版权声明:本文标题:javascript - How to throw error in an async function in jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135391a2422345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论