A
A
alex4answ2020-09-23 17:51:49
Node.js
alex4answ, 2020-09-23 17:51:49

How to test jest + sequelize?

Good afternoon, I'm groaning trying to write integration tests.
When it came to the database, there were problems, because sequelize.sync({ force: true });sometimes it works, sometimes it doesn’t, randomly.

Now the test files look like this:

const CategoryModel = require('../CategoryModel'); // sequelize model

beforeAll(async () => {
  await CategoryModel.sync({ force: true });
  await CategoryModel.bulkCreate([ /* массив данных для вставки */ ]);
});

// далее сами тесты ...

But sequelize is behaving very strangely, randomly outputting these errors:
1. bulkCreate() - [SequelizeDatabaseError]: Table 'shop_test.categories' does not exist
2. bulkCreate() - [SequelizeUniqueConstraintError]: Validation error. (Duplicate entry 'spoons' for key 'categories.slug')

How is this possible? after all , the .sync method with force: true deletes the table, then creates it according to the

jest scheme, I run it with flags:
"test": "jest --runInBand --detectOpenHandles"

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question