用Postgrestestingasynchronous错误

我一直在用Postgres数据库编写Sequelize的unit testing。 为了testing,我正在使用Jest。 现在,我可以得到这个代码工作:

test('email field only takes valid emails', () => { expect.assertions(2); let user = Users.build({ email: 'notAProperEmail', password: 'abc123' }); return user.validate().catch(err => { expect(err).toHaveProperty('errors'); expect(err.errors[0].message).toEqual('Validation isEmail on email failed'); }); }) 

但由于某种原因,这会抛出错误“SequelizeDatabaseError:关系”用户“不存在”。 此外,预期的方法也永远不会被击中,因为我得到了预期的断言错误:“期望的一个断言被调用,但接收到零断言调用”。

 describe('Password is encrypted', () => { beforeAll(() => { Users.create({ email: 'test1@gmail.com', password: 'testPassword123' }) }); test('password is not plain text', () => { expect.assertions(1); return Users.findOne({where: { email: 'test1@gmail.com' }}).then(response => { expect(2).toEqual(2) console.log('console log here: ', response); }); }); 

我唯一的猜测是,因为构build实际上并没有保存到数据库中,所以它会及时返回Jest进行断言的值。 所以我的直觉是使用承诺来处理asynchronous性质(另外,我检查了Jest文档)。 但它仍然会抛出这个错误,好像在Sequelize完成从Postgres数据库中获取数据之前就已经返回了。 如果有人能解释我遇到的这个问题,我真的很感激!