从 async/await 函数抛出时,Jest 不会捕获错误
Jest doesn't catch error, when thrown from async/await function
我正在使用 MongoDB 和 Mongoose 测试一些功能。对于测试,我使用 Jest 和 mongo-memory-server。函数 createUser(arg1, arg2)
应该 抛出 TypeError 如果数据库中已经存在具有该不一致 ID 的用户。
createUser(arg1, arg2)
async function createUser(disc_id, moodleToken) {
const profileData = await fetchUser(disc_id);
if (profileData) {
throw TypeError("The account already exist in the database!");
}
const newUser = new profileModel({
discord_id: disc_id,
moodle_token: moodleToken,
})
await newUser.save();
return {
userId: newUser._id
};
}
fetchUser(disc_id)
returns 是否找到相同discord id的用户
在使用 Jest 进行测试时,我创建了以下测试,该测试通过得很好:
it("Should create a new user", async () => {
const discord_id = "3452357445";
const moodle_token = "34ffDSE8439Ujfe8f3jj";
const { userId } = await createUser(discord_id, moodle_token);
const user = await profileSchema.findById(userId);
expect(user.discord_id).toEqual(discord_id);
expect(user.moodle_token).toEqual(moodle_token);
})
现在我想测试在尝试创建新用户时是否抛出 TypeError,具有相同的 discord id,我尝试了以下但没有成功:
describe("Error when", () => {
it("an existing matches discord id", async () => {
const discord_id = "3452357445";
const moodle_token = "34ffDSE8439Ujfe8f3jj";
await createUser(discord_id, moodle_token)
await expect(async () => {
createUser(discord_id, moodle_token);
}).rejects.toThrow(TypeError("The account already exist in the database!"))
})
})
当 运行 测试时,这是控制台的输出:
FAIL test/manageUserDB.test.js
Create new user
✓ Should create a new user (66 ms)
Error when
✕ an existing matches discord id (3 ms)
● Create new user › Error when › an existing matches discord id
TypeError: The account already exist in the database!
11 |
12 | if (profileData) {
> 13 | throw TypeError("The account already exist in the database!");
| ^
14 | }
15 | const newUser = new profileModel({
16 | discord_id: disc_id,
at createUser (BoodleDB/manageUserDB.js:13:15)
at Object.<anonymous> (test/manageUserDB.test.js:29:13)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 0.6 s, estimated 1 s
Ran all test suites matching /.\/test/i.
编辑
function fetchUser (disc_id) {
return profileModel.findOne({ discord_id: disc_id });
}
调用createUser时需要等待
await expect(async () => {
await createUser(discord_id, moodle_token);
}).rejects.toThrow(TypeError("The account already exist in the database!"))
我正在使用 MongoDB 和 Mongoose 测试一些功能。对于测试,我使用 Jest 和 mongo-memory-server。函数 createUser(arg1, arg2)
应该 抛出 TypeError 如果数据库中已经存在具有该不一致 ID 的用户。
createUser(arg1, arg2)
async function createUser(disc_id, moodleToken) {
const profileData = await fetchUser(disc_id);
if (profileData) {
throw TypeError("The account already exist in the database!");
}
const newUser = new profileModel({
discord_id: disc_id,
moodle_token: moodleToken,
})
await newUser.save();
return {
userId: newUser._id
};
}
fetchUser(disc_id)
returns 是否找到相同discord id的用户
在使用 Jest 进行测试时,我创建了以下测试,该测试通过得很好:
it("Should create a new user", async () => {
const discord_id = "3452357445";
const moodle_token = "34ffDSE8439Ujfe8f3jj";
const { userId } = await createUser(discord_id, moodle_token);
const user = await profileSchema.findById(userId);
expect(user.discord_id).toEqual(discord_id);
expect(user.moodle_token).toEqual(moodle_token);
})
现在我想测试在尝试创建新用户时是否抛出 TypeError,具有相同的 discord id,我尝试了以下但没有成功:
describe("Error when", () => {
it("an existing matches discord id", async () => {
const discord_id = "3452357445";
const moodle_token = "34ffDSE8439Ujfe8f3jj";
await createUser(discord_id, moodle_token)
await expect(async () => {
createUser(discord_id, moodle_token);
}).rejects.toThrow(TypeError("The account already exist in the database!"))
})
})
当 运行 测试时,这是控制台的输出:
FAIL test/manageUserDB.test.js
Create new user
✓ Should create a new user (66 ms)
Error when
✕ an existing matches discord id (3 ms)
● Create new user › Error when › an existing matches discord id
TypeError: The account already exist in the database!
11 |
12 | if (profileData) {
> 13 | throw TypeError("The account already exist in the database!");
| ^
14 | }
15 | const newUser = new profileModel({
16 | discord_id: disc_id,
at createUser (BoodleDB/manageUserDB.js:13:15)
at Object.<anonymous> (test/manageUserDB.test.js:29:13)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 0.6 s, estimated 1 s
Ran all test suites matching /.\/test/i.
编辑
function fetchUser (disc_id) {
return profileModel.findOne({ discord_id: disc_id });
}
调用createUser时需要等待
await expect(async () => {
await createUser(discord_id, moodle_token);
}).rejects.toThrow(TypeError("The account already exist in the database!"))