2

I've been writing unit tests for Sequelize with a Postgres database. For testing, I'm currently using Jest. Right now, I can get this code to work:

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

But for some reason this throws the error "SequelizeDatabaseError: relation "Users" does not exist". Also, the expect method also never gets hit because I get the expected assertion error: "Expected one assertion to be called but received zero assertion calls."

describe('Password is encrypted', () => {
beforeAll(() => {
  Users.create({
    email: '[email protected]',
    password: 'testPassword123'
  })
});

test('password is not plain text', () => {
  expect.assertions(1);
  return Users.findOne({where: { email: '[email protected]' }}).then(response => {
    expect(2).toEqual(2)
    console.log('console log here: ', response);
  });
});

My only guess is that because build doesn't actually save to the database, it returns a value in time for Jest to make an assertion. So my intuition is to use promises to handle the async nature (also, I checked the Jest docs). But it still throws this error and it seems like the promises are being returned before Sequelize is done fetching data from my Postgres Database. I'd really appreciate it if someone could shed light on this issue I'm having!

1 Answer 1

2

A bit late but I just solved something very similar after landing here!. This is possibly an environment issue. When you run your test suite jest will go looking for the DB that is related to your test env no longer your dev env. In my case i'm using postgres and knex and the different env configs are in the knexFile. I would imagine sequelize is similar! The way to hunt it down is to run psql myDBinWhateverEnv and see if you have a relation "Users"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.