我正在使用mookingose做一些测试,但即使在控制台上显示一些错误,它们也总是通过的。
这是其中一个测试的示例
import mockingoose from 'mockingoose';
import { getUserById, insertUser } from '../controller/user';
import User from '../models/users';
import fetch from '../__mocks__/fetchnode';
describe('Test the user mongoose model', () => {
beforeEach(() => {
mockingoose.resetAll();
jest.clearAllMocks();
});
it('should return a valid user with findById user', () => {
mockingoose(User).toReturn(expectDoc, 'find');
getUserById('507f191e810c19729de860ea').then(res => {
expect(res.nickName).toBe(expectDoc.nickName);
});
});
it('should return the user doc with Save user', () => {
mockingoose(User).toReturn(expectDoc, 'save');
insertUser(expectDoc).then(res => {
expect(res.nickName).toBe(expectDoc.nickName);
});
});
it('should return error message with invalid user doc to save user', () => {
const OnlyAvatar = { avatar: expectDoc.avatar };
mockingoose(User).toReturn(OnlyAvatar, 'save');
insertUser(OnlyAvatar).catch(res => {
expect(res.message).toBe(
'AAAusers validation failed: name: Path `name` is required., nickName: Path `nickName` is required., email: Path `email` is required., password: Path `password` is required.',
);
});
});
});现在我在控制台上遇到了类似这样的错误:
Expected: "AAAusers validation failed: name: Path `name` is required., nickName: Path `nickName` is required., email: Path `email` is required., password: Path `password` i
s required."
Received: "users validation failed: name: Path `name` is required., nickName: Path `nickName` is required., email: Path `email` is required., password: Path `password` is
required."
(node:30984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or
by rejecting a promise which was not handled with .catch(). (rejection id: 2)
PASS src/routes/user.test.js
Test Suites: 5 passed, 5 total
Tests: 23 passed, 23 total
Snapshots: 0 total
Time: 3.057s
Ran all test suites matching /src\/routes\/category.test.js|src\/routes\/project.test.js|src\/routes\/task.test.js|src\/routes\/taskSearch.test.js|src\/routes\/user.test.
js/i.测试应该失败,但通过了
发布于 2019-09-03 10:27:49
您将获得一个UnhandledPromiseRejectionWarning。
这意味着Promise正在拒绝,但拒绝并未得到处理。
下面是一个高度简化的示例,演示了这个问题:
test('a promise', () => {
Promise.resolve().then(() => {
expect(1).toBe(2); // <= causes UnhandledPromiseRejectionWarning
});
})由于测试并不等待Promise解析,因此在expect有机会运行之前,测试运行到完成并通过。
then回调稍后运行,expect失败,导致Promise为rejected...but。测试已经完成,没有任何东西处理拒绝。
Node检测到未处理的Promise拒绝并显示警告。
您总是需要返回let Jest know when your test is asynchronous,或者返回Promise
test('a promise', () => {
return Promise.resolve().then(() => {
expect(1).toBe(2); // <= fails as expected
});
})对async测试函数执行...use操作并对Promise执行await操作
test('a promise', async () => {
await Promise.resolve().then(() => {
expect(1).toBe(2); // <= fails as expected
});
})使用done的...or
test('a promise', done => {
Promise.resolve().then(() => {
expect(1).toBe(2); // <= fails as expected
done();
});
})在您的示例中,最简单的修复方法是返回Promise
it('should return error message with invalid user doc to save user', () => {
const OnlyAvatar = { avatar: expectDoc.avatar };
mockingoose(User).toReturn(OnlyAvatar, 'save');
return insertUser(OnlyAvatar).catch(res => { // <= return the Promise
expect(res.message).toBe(
'AAAusers validation failed: name: Path `name` is required., nickName: Path `nickName` is required., email: Path `email` is required., password: Path `password` is required.',
);
});
});https://stackoverflow.com/questions/57764028
复制相似问题