簡體   English   中英

帶Postgres測試異步錯誤的續集

[英]Sequelize w/Postgres testing async errors

我一直在用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:關系“用戶”不存在”。 另外,期望方法也永遠不會被擊中,因為我得到了期望的斷言錯誤:“期望一個斷言被調用,但是收到了零個斷言調用。”

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進行斷言。 因此,我的直覺是使用promise處理異步性質(同樣,我檢查了Jest文檔)。 但是它仍然會引發此錯誤,並且看起來好像在Sequelize從我的Postgres數據庫中獲取數據之前返回了Promise。 如果有人能闡明我遇到的這個問題,我將非常感激!

有點晚了,但是我登陸這里后解決了一些非常相似的問題! 這可能是環境問題。 當你運行測試套件的玩笑會去尋找那是關系到你的數據庫test ENV不再是你的dev包膜。 就我而言,我使用的是postgres和knex,而不同的env配置位於knexFile中。 我可以想象續集是相似的! 尋找它的方法是運行psql myDBinWhateverEnv並查看您是否具有“用戶”關系

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM