gpt4 book ai didi

node.js - Sequelize w/Postgres 测试异步错误

转载 作者:行者123 更新时间:2023-11-29 12:24:42 26 4
gpt4 key购买 nike

我一直在使用 Postgres 数据库为 Sequelize 编写单元测试。为了进行测试,我目前正在使用 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:关系“Users”不存在”。此外,expect 方法也永远不会被命中,因为我得到了预期的断言错误:“预期调用一个断言但收到零个断言调用。”

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);
});
});

我唯一的猜测是,因为构建实际上并没有保存到数据库中,所以它会及时返回一个值,以便 Jest 做出断言。所以我的直觉是使用 promises 来处理异步性质(另外,我检查了 Jest 文档)。但它仍然会抛出此错误,并且似乎在 Sequelize 完成从我的 Postgres 数据库中获取数据之前返回了 promise 。如果有人能阐明我遇到的这个问题,我将不胜感激!

最佳答案

有点晚了,但我在登陆这里后刚刚解决了一些非常相似的问题!。这可能是环境问题。当你运行你的测试套件时,jest 会去寻找与你的 test 环境相关的数据库,而不是你的 dev 环境。在我的例子中,我使用的是 postgres 和 knex,不同的环境配置在 knexFile 中。我想 sequelize 是相似的!找到它的方法是运行 psql myDBinWhateverEnv 并查看是否有关系“Users”

关于node.js - Sequelize w/Postgres 测试异步错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46868814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com